Skip to content
This repository has been archived by the owner on Oct 9, 2020. It is now read-only.

Commit

Permalink
Replace cron with interval
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Cucer committed Feb 22, 2018
1 parent 49ed9b1 commit d607798
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 31 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "titime",
"productName": "TiTime - Keeping the time for you",
"version": "1.4.2",
"version": "1.4.3",
"description": "Tool for Instant Time Tracking",
"main": "src/index.js",
"scripts": {
Expand Down Expand Up @@ -59,7 +59,6 @@
},
"dependencies": {
"axios": "^0.17.1",
"cron": "^1.3.0",
"d3-timelines": "^1.3.1",
"desktop-idle": "^1.1.1",
"diskdb": "^0.1.17",
Expand Down
2 changes: 1 addition & 1 deletion src/config/app.cfg.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default {
level: 'info',
maxDays: 30,
},
remoteSyncCron: '* */10 * * * *', // Cron to run remote sync every 10 minutes (e.g. Redmine)
remoteSyncCron: 10 * 60 * 1000, // Cron to run remote sync every 10 minutes (e.g. Redmine)
archiveByYear: true, // Archive previous year entries
archiveResetKey: 'timeSegments', // Reset loged time after a year
maxIdleBeforeAsk: 60 * 5, // 5 minutes idle time allowed
Expand Down
60 changes: 33 additions & 27 deletions src/cron.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { CronJob } from 'cron';
import Logger from './logger';

export default class Cron {
Expand All @@ -18,36 +17,43 @@ export default class Cron {
return this.jobs[name] || null;
}

add(name, cronTime, job, start = false) {
async add(name, interval, job, start = false) {
if (this.exists(name)) {
throw new Error(`Cron job "${name}" already exists`);
}

Logger.debug(`Add cron job "${name}" with pattern "${cronTime}"`);

let running = false;

const onTick = () => {
if (running) {
return Promise.resolve();
}

Logger.info(`Run "${name}" cron job`);

return job()
.then((result) => {
running = false;

return Promise.resolve(result);
})
.catch((error) => {
running = false;

return Promise.reject(error);
});
Logger.debug(`Add cron job "${name}" with pattern "${interval}"`);

const instance = {
clock: null,
running() {
return !!this.clock;
},
async start() {
if (this.running()) {
return;
}

this.clock = setInterval(async () => {
Logger.info(`Run "${name}" cron job`);
await job();
}, interval);

await job();
},
stop() {
if (!this.running()) {
return;
}

clearInterval(this.clock);
this.clock = null;
},
};

const instance = new CronJob({ cronTime, onTick, start });
if (start) {
await instance.start();
}

this.jobs[name] = instance;

Expand All @@ -60,7 +66,7 @@ export default class Cron {
this.list().forEach((name) => {
const job = this.get(name);

if (!job.running) {
if (!job.running()) {
job.start();
}
});
Expand All @@ -74,7 +80,7 @@ export default class Cron {
this.list().forEach((name) => {
const job = this.get(name);

if (job.running) {
if (job.running()) {
job.stop();
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const createProviderSyncCommand = (providerName, createProvider) => async (...ar
const provider = createProvider(...args);

if (!cron.exists(providerName)) {
cron.add(
await cron.add(
providerName,
registry.config().get('remoteSyncCron'),
() => provider.synchronize()
Expand Down

0 comments on commit d607798

Please sign in to comment.