Skip to content

Commit 648d848

Browse files
committed
Fix cron execution by replacing node-cron with croner.
1 parent 3b52a88 commit 648d848

5 files changed

Lines changed: 46 additions & 26 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11

2+
## v0.3.1
3+
4+
- Fix cron execution by replacing `node-cron` with `croner`.
5+
26
## v0.3.0
37

48
- Fixed the connected-peers metric so it now reports the real-time value.

lib/index.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const Debugger = require('debug');
66
const EventEmitter = require('node:events');
77
const assert = require('node:assert');
88
const { performance } = require('node:perf_hooks');
9-
const cron = require('node-cron');
9+
const { Cron } = require('croner');
1010

1111
// Remote peer index pointers in the peerStats array for fast access (avoiding map lookup of objects, ...)
1212
// [LAST_PATCH_AT_TIMESTAMP, LAST_SEQUENCE_ID, GUARANTEED_CONTIGUOUS_PATCH_AT_TIMESTAMP, GUARANTEED_CONTIGUOUS_SEQUENCE_ID, LAST_MESSAGE_TIMESTAMP]
@@ -116,10 +116,12 @@ const SQLiteOnSteroid = (db, myPeerId = null, options) => {
116116
heartbeatInterval = setInterval(_heartBeat, HEARTBEAT_INTERVAL_MS);
117117
}
118118
if (DATABASE_BACKUP_CRON) {
119-
if (!cron.validate(DATABASE_BACKUP_CRON) ) {
120-
throw new Error(`Invalid database backup cron syntax: ${DATABASE_BACKUP_CRON}`);
119+
try {
120+
backupTask = new Cron(DATABASE_BACKUP_CRON, { paused : true, protect : true }, backupDatabase);
121+
}
122+
catch (err) {
123+
throw new Error(`Invalid database backup cron syntax: ${err.message}`);
121124
}
122-
backupTask = cron.createTask(DATABASE_BACKUP_CRON, backupDatabase, { noOverlap : true });
123125
}
124126

125127
/**
@@ -133,6 +135,9 @@ const SQLiteOnSteroid = (db, myPeerId = null, options) => {
133135
* @param {Function} callback
134136
*/
135137
function backupDatabase (trigger = 'scheduled', callback) {
138+
if (typeof trigger !== 'string') {
139+
trigger = 'scheduled'; // Cron send the cron object as first argument
140+
}
136141
DATABASE_BACKUP_ABSOLUTE_PATH_FN(trigger, (_backupFileName) => {
137142
if (!_backupFileName) {
138143
debug('No backup for ${trigger} trigger');
@@ -756,11 +761,11 @@ const SQLiteOnSteroid = (db, myPeerId = null, options) => {
756761
if (amITheLeaderCached !== _amITheLeader) {
757762
if (_amITheLeader === true) {
758763
debug('became leader');
759-
backupTask?.start(); // manage backup cron
764+
backupTask?.resume(); // manage backup cron
760765
}
761766
else {
762767
debug('lost leader role');
763-
backupTask?.stop();
768+
backupTask?.pause();
764769
}
765770
}
766771
amITheLeaderCached = _amITheLeader;

package-lock.json

Lines changed: 23 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "replic-sqlite",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"description": "Simple, Fast, Multi-writer, Convergent, Replicated SQLite with 800 LOC only",
55
"main": "lib/index.js",
66
"repository": {
@@ -35,7 +35,7 @@
3535
},
3636
"dependencies": {
3737
"better-sqlite3": "=12.6.2",
38-
"debug": "=4.4.3",
39-
"node-cron": "=4.2.1"
38+
"croner": "=10.0.1",
39+
"debug": "=4.4.3"
4040
}
4141
}

test/test.main.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,14 @@ describe('main', function () {
269269

270270
it('should detect the leader (and start backup cron or not)', function (done) {
271271
assert.strictEqual(app.amITheLeader(), true);
272-
assert.strictEqual(app.backupTask.stateMachine.state, 'idle');
272+
assert.strictEqual(app.backupTask.isRunning(), true);
273273
app.addRemotePeer(100, fakePeerSockets[100], { ip : '127.0.0.1', port : 10000 });
274274
app.addRemotePeer(101, fakePeerSockets[101], { ip : '127.0.0.1', port : 10001 });
275275
app.addRemotePeer(110, fakePeerSockets[110], { ip : '127.0.0.1', port : 10002 });
276276
assert.strictEqual(extractNbConnectedPeersFromMetrics( app.metrics()), 3);
277277
assert.strictEqual(app.amITheLeader(), false);
278-
assert.strictEqual(app.backupTask.stateMachine.state, 'stopped');
278+
assert.strictEqual(app.backupTask.isRunning(), false);
279+
assert.strictEqual(app.backupTask.isStopped(), false);
279280
app.closeRemotePeer(101);
280281
assert.strictEqual(extractNbConnectedPeersFromMetrics( app.metrics()), 2);
281282
assert.strictEqual(app.amITheLeader(), false);
@@ -290,7 +291,7 @@ describe('main', function () {
290291
// should be the leader after 500ms (disconnection tolerance)
291292
setTimeout(() => {
292293
assert.strictEqual(app.amITheLeader(), true);
293-
assert.strictEqual(app.backupTask.stateMachine.state, 'idle');
294+
assert.strictEqual(app.backupTask.isRunning(), true);
294295
assert.strictEqual(extractNbConnectedPeersFromMetrics( app.metrics()), 1);
295296
done();
296297
}, 500);
@@ -1364,7 +1365,7 @@ describe('main', function () {
13641365
finishTestIfReady();
13651366
});
13661367
// No callback to backupDatabase, so events must fire
1367-
_app.backupDatabase('scheduled');
1368+
_app.backupDatabase({}); // send the cron object as first argument to verify that the trigger is set to 'scheduled'
13681369
// Helper to call done when both events are received
13691370
function finishTestIfReady () {
13701371
if (gotCompleted && gotProgress) {

0 commit comments

Comments
 (0)