Switched from setTimeout to crontab for watch-mode.

This commit is contained in:
2018-07-01 23:16:03 +02:00
parent 23f9ac660a
commit 5b023adf30
4 changed files with 13 additions and 13 deletions

View File

@@ -4,6 +4,7 @@ const config = require('config');
const Snoowrap = require('snoowrap');
const exiftool = require('node-exiftool');
const exiftoolBin = require('dist-exiftool');
const cron = require('node-cron');
const { format } = require('date-fns');
require('array.prototype.flatten').shim();
@@ -40,12 +41,6 @@ function fetchSavePosts(userPosts, ep) {
}
async function initApp() {
function watch() {
console.log(`[${format(new Date(), 'YYYY-MM-DD HH:mm:ss')}] Watch-mode enabled, checking for new posts ${config.fetch.watch.interval} minutes from now.`);
setTimeout(initApp, Math.ceil(config.fetch.watch.interval) * 1000 * 60);
}
const usersProvided = args.users && args.users.length;
const postIdsProvided = args.posts && args.posts.length;
@@ -62,17 +57,17 @@ async function initApp() {
await ep.close();
if (args.watch) {
watch();
console.log(`[${format(new Date(), 'YYYY-MM-DD HH:mm:ss')}] Watch-mode enabled, checking again for new posts according to crontab '${config.fetch.watch.schedule}'.`);
}
} catch (error) {
console.error(error);
if (args.watch && config.fetch.watch.ignoreErrors) {
watch();
}
}
return true;
}
initApp();
if (args.watch) {
cron.schedule(config.fetch.watch.schedule, initApp);
}