Skip to content

Commit

Permalink
Add db maintenance routine. Move cleanup functions to maintenance mod…
Browse files Browse the repository at this point in the history
…ule.
  • Loading branch information
pfoltyn committed Feb 8, 2014
1 parent 657a078 commit c96657c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 21 deletions.
1 change: 1 addition & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require('./db');
var express = require('express');
var routes = require('./routes');
var upload = require('./upload');
var maintenance = require('./maintenance');

var http = require('http');
var path = require('path');
Expand Down
60 changes: 60 additions & 0 deletions maintenance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
This file is part of infragram-js.
infragram-js is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
infragram-js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with infragram-js. If not, see <http://www.gnu.org/licenses/>.
*/

var mongoose = require('mongoose');
var Image = mongoose.model('Image');
var upload = require('./upload');
var fs = require('fs');
var async = require('async');

var CLEANUP_INTERVAL = 300000; // 5 minutes in ms
var CLEANUP_TIME_THRESHOLD = 60000; //1 minute in ms
var CLEANUP_SIZE_THRESHOLD = 1024; // 1kB

// Clean up damaged files.
setInterval(function () {
var time = Date.now();
fs.readdir(upload.UPLOAD_PREFIX, function (err, list) {
if (err) return console.log(err);
async.forEach(list, function (diritem, cb) {
fs.lstat(upload.UPLOAD_PREFIX + diritem, function (err, stats) {
if (err) return console.log(err);
var timeDiff = time - stats.ctime.getTime();
if ((stats.size < CLEANUP_SIZE_THRESHOLD) && (timeDiff > CLEANUP_TIME_THRESHOLD)) {
fs.unlink(upload.UPLOAD_PREFIX + diritem, function () {});
}
});
});
});
},
CLEANUP_INTERVAL
);

// Clean up damaged db records.
setInterval(function () {
Image
.find()
.where('filename').equals('')
.select('_id')
.exec(function (err, images) {
async.forEach(images, function (image, cb) {
Image.remove({ _id: image._id }, function (err, image) {});
});
});
},
CLEANUP_INTERVAL
);
21 changes: 0 additions & 21 deletions upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ var FILE_SIZE_LIMIT = 5242880; // 5MB
var FILE_CHUNK_SIZE = 131072; // 128kB
var FILE_CHUNK_BASE64_SIZE = FILE_CHUNK_SIZE * 2;

var CLEANUP_INTERVAL = 300000; // 5 minutes in ms
var CLEANUP_TIME_THRESHOLD = 60000; //1 minute in ms
var CLEANUP_SIZE_THRESHOLD = 1024; // 1kB

getValue = function (data, key, maxLen) {
var value = '';
if (key in data) {
Expand Down Expand Up @@ -131,23 +127,6 @@ exports.onConnection = function (socket) {
});
};

// Clean up damaged files.
setInterval(function () {
var time = Date.now();
fs.readdir(UPLOAD_PREFIX, function (err, list) {
async.forEach(list, function (diritem, cb) {
fs.lstat(UPLOAD_PREFIX + diritem, function (err, stats) {
var timeDiff = time - stats.ctime.getTime();
if ((stats.size < CLEANUP_SIZE_THRESHOLD) && (timeDiff > CLEANUP_TIME_THRESHOLD)) {
fs.unlink(UPLOAD_PREFIX + diritem, function () {});
}
});
});
});
},
CLEANUP_INTERVAL
);

exports.UPLOAD_PREFIX = UPLOAD_PREFIX;

exports.THUMBNAIL_SUFIX = THUMBNAIL_SUFIX;

0 comments on commit c96657c

Please sign in to comment.