Skip to content
Permalink
Browse files

Updates scheduler

  • Loading branch information
davidmerfield committed Nov 24, 2019
1 parent b711d32 commit 21c4c12f1d7044dd9d9222f253137e232c7e90f0
Showing with 198 additions and 5 deletions.
  1. +79 −0 app/scheduler/backup.js
  2. +2 −5 app/scheduler/daily/newsletter-subscribers.js
  3. +117 −0 app/scheduler/index.js
@@ -0,0 +1,79 @@
var config = require("config");
var redis = require("redis").createClient();
var helper = require("helper");
var upload = helper.upload;
var encrypt = helper.encrypter.encrypt;
var fs = require("fs-extra");
var DB_PATH = require("path").resolve(__dirname + "/../../db/dump.rdb");

var tmp = helper.tempDir();
var Moment = require("moment");
var joinpath = require("path").join;

// called from command line
if (require.main === module) {
var extraTag = process.argv[2];

backUP(extraTag);
}

function backUP(extraString) {
saveDBToDisk(function() {
var ds = Moment.utc().format("YYYY-MM-DD-HH-mm-ss");

var clone_name = ds;

if (extraString) clone_name += "-" + extraString;

clone_name += "-encrypted-dump.rdb";

var encrypted_clone_path = joinpath(tmp, clone_name);

encrypt(DB_PATH, encrypted_clone_path, function(err) {
if (err) throw err;

var options = {
bucket: config.backup.bucket,
remote: clone_name
};

upload(encrypted_clone_path, options, function(err) {
if (err) {
console.log("Backup error :(");
console.log(err);
}

if (!err) {
console.log("Backup complete!");
}

fs.remove(encrypted_clone_path);
});
});
});
}

function saveDBToDisk(callback) {
redis.lastsave(function(err, last_save_time) {
console.log("Backup: Saving database to disk at " + last_save_time);

redis.bgsave(function(err, stat) {
console.log("Backup: " + stat);

(function checkLast() {
redis.lastsave(function(err, latest_save_time) {
if (latest_save_time !== last_save_time) {
console.log("Backup: DB saved to disk in background");
return callback();
}

checkLast();
});
})();
});
});
}

module.exports = {
now: backUP
};
@@ -1,11 +1,8 @@
if (require.main === module) {
main(function(err, subscribers) {
main(function(err, res) {
if (err) throw err;

console.log(subscribers.join(" "));

console.log();
console.log(subscribers.length + " subscribed to the newsletter");
console.log(res.newsletter_subscribers + " subscribed to the newsletter");
process.exit();
});
}
@@ -0,0 +1,117 @@
var Entries = require("entries");
var Entry = require("entry");
var User = require("user");
var async = require("async");
var Blog = require("blog");
var backup = require("./backup");
var dailyUpdate = require("./daily");
var helper = require("helper");
var email = helper.email;

var schedule = require("node-schedule").scheduleJob;

module.exports = function() {
// Bash the cache for scheduled posts
cacheScheduler(function(stat) {
console.log(stat);
});

// Warn users about impending subscriptions
User.getAllIds(function(err, uids) {
async.each(uids, User.scheduleSubscriptionEmail, function(err) {
if (err) {
console.error("Error scheduling subscription emails:", err);
} else {
console.log("Scheduled emails for renewals and expiries!");
}
});
});

console.log("Scheduled daily check of storage disk usage for 6am!");
schedule({ hour: 10, minute: 0 }, function() {
console.log("Scheduler: Checking available disk space");

require("child_process").exec("df -h", function(err, stdout) {
if (err) throw err;

var disk = stdout
.split("\n")[1]
.replace(/\s+/g, " ")
.split(" ");
var usage = disk[4];
var available = disk[3];

if (parseInt(usage) < 90) {
console.log(
"Scheduler: Disk usage check passed! Usage:",
usage,
"Space available:",
available
);
return;
}

console.log(
"Scheduler: Disk usage check failed! Usage:",
usage,
"Space available:",
available
);

email.WARNING_LOW_DISK_SPACE(
null,
{ usage: usage, available: available },
function(err) {
if (err) console.log(err);
}
);
});
});

console.log("Scheduled daily backups for 3am!");
schedule({ hour: 11, minute: 0 }, function() {
// Start the backup daemon
console.log("Backup: It is 1am, time to start!");
backup.now();
});

// At some point I should check this doesnt consume too
// much memory
console.log("Scheduled daily update email for 6:05am!");
schedule({ hour: 10, minute: 5 }, function() {
console.log("Generating daily update email...");
dailyUpdate(function() {
console.log("Daily update email update was sent.");
});
});
};

function cacheScheduler(callback) {
var totalScheduled = 0;

Blog.getAllIDs(function(err, blogIDs) {
async.each(
blogIDs,
function(blogID, nextBlog) {
Entries.get(blogID, { lists: ["scheduled"] }, function(err, list) {
async.each(
list.scheduled,
function(futureEntry, nextEntry) {
totalScheduled++;

// Saving empty updates will call the entry scheduler
// and ensure the entry is rebuilt again in future
Entry.set(blogID, futureEntry.path, {}, nextEntry);
},
function() {
nextBlog();
}
);
});
},
function() {
callback("Scheduled " + totalScheduled + " posts to clear the cache.");
}
);
});
}

0 comments on commit 21c4c12

Please sign in to comment.
You can’t perform that action at this time.