From 00de0d43f586d74ae84061f1d6d7fa46755b8cbd Mon Sep 17 00:00:00 2001 From: DebaucheryLibrarian Date: Wed, 11 Sep 2024 05:16:56 +0200 Subject: [PATCH] Switched from setTimeout to crontab for watch-mode. --- config/default.js | 3 +-- package-lock.json | 5 +++++ package.json | 1 + src/app.js | 17 ++++++----------- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/config/default.js b/config/default.js index 280888f..471e927 100644 --- a/config/default.js +++ b/config/default.js @@ -46,8 +46,7 @@ module.exports = { avoidDuplicates: true, retries: 3, watch: { - interval: 30, - ignoreErrors: true, + schedule: '*/30 * * * *', }, archives: { search: false, diff --git a/package-lock.json b/package-lock.json index ed600f6..46157c2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1523,6 +1523,11 @@ "integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=", "dev": true }, + "node-cron": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/node-cron/-/node-cron-1.2.1.tgz", + "integrity": "sha1-jJC8XccjpWKJsHhmVatKHEy2A2g=" + }, "node-exiftool": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/node-exiftool/-/node-exiftool-2.3.0.tgz", diff --git a/package.json b/package.json index 37fed52..387a89e 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "fs-extra": "^5.0.0", "js-yaml": "^3.12.0", "mime-types": "^2.1.18", + "node-cron": "^1.2.1", "node-exiftool": "^2.3.0", "node-fetch": "^2.1.2", "object.omit": "^3.0.0", diff --git a/src/app.js b/src/app.js index c0ad0a5..4b025db 100644 --- a/src/app.js +++ b/src/app.js @@ -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); +}