Skip to content

Commit

Permalink
feat: add schedular architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Feb 21, 2022
1 parent fc7e294 commit 8b5c2c5
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 51 deletions.
22 changes: 11 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"socket.io": "^4.4.0",
"socket.io-client": "^4.4.0",
"sqlite3": "4.1.1",
"toad-scheduler": "^1.6.0",
"updeep": "^1.2.1",
"uuidv4": "^6.2.12"
},
Expand Down
5 changes: 0 additions & 5 deletions src/datalayer/syncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ const startDataLayerUpdatePolling = async () => {
);
}),
);
} else {
logUpdate(`Polling For Updates ${frames[Math.floor(Math.random() * 3)]}`);
}

// after all the updates are complete, check again in a bit
setTimeout(() => startDataLayerUpdatePolling(), POLLING_INTERVAL);
};

const syncDataLayerStoreToClimateWarehouse = async (storeId, rootHash) => {
Expand Down
37 changes: 2 additions & 35 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import http from 'http';
import { Server } from 'socket.io';
import Debug from 'debug';
import { connection } from './websocket';
import datalayer from './datalayer';
import { pullPickListValues } from './utils/data-loaders';
import { Organization } from './models';
import scheduler from './tasks';

const debug = Debug('climate-warehouse:server');

Expand Down Expand Up @@ -49,42 +47,11 @@ function onError(error) {
* Event listener for HTTP server "listening" event.
*/

const syncPickList = () => {
console.log('Syncing PickLists');
pullPickListValues();
};

const subscribeToOrganizations = () => {
console.log('Subscribing to default organizations');
Organization.subscribeToDefaultOrganizations();
};

function onListening() {
syncPickList();
setInterval(async () => {
syncPickList();
}, 86400000 /* 1 day */);

if (process.env.USE_SIMULATOR === 'false') {
try {
subscribeToOrganizations();
} catch (error) {
console.log(error);
}
setInterval(async () => {
try {
subscribeToOrganizations();
} catch (error) {
console.log(error);
}
}, 86400000 /* 1 day */);
}

scheduler.start();
const addr = server.address();
const bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port;
debug('Listening on ' + bind);
}

datalayer.startDataLayerUpdatePolling();

export default rootRouter;
32 changes: 32 additions & 0 deletions src/tasks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ToadScheduler } from 'toad-scheduler';

import syncDataLayer from './sync-datalayer';
import syncOrganizations from './sync-organizations';
import syncPickLists from './sync-picklists';

const scheduler = new ToadScheduler();

const jobRegistry = {};

const addJobToScheduler = (job) => {
jobRegistry[job.id] = job;
scheduler.addSimpleIntervalJob(job);
};

const start = () => {
// add default jobs
const defaultJobs = [syncDataLayer, syncOrganizations, syncPickLists];
defaultJobs.forEach((defaultJob) => {
jobRegistry[defaultJob.id] = defaultJob;
scheduler.addSimpleIntervalJob(defaultJob);
});
};

const getJobStatus = () => {
return Object.keys(jobRegistry).reduce((status, key) => {
status[key] = jobRegistry[key].getStatus();
return status;
}, {});
};

export default { start, addJobToScheduler, jobRegistry, getJobStatus };
18 changes: 18 additions & 0 deletions src/tasks/sync-datalayer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import logUpdate from 'log-update';
import { SimpleIntervalJob, Task } from 'toad-scheduler';
import datalayer from '../datalayer';
const frames = ['-', '\\', '|', '/'];

const task = new Task('sync-datalayer', () => {
logUpdate(`Polling For Updates ${frames[Math.floor(Math.random() * 3)]}`);

datalayer.startDataLayerUpdatePolling();
});

const job = new SimpleIntervalJob(
{ seconds: 5, runImmediately: true },
task,
'sync-datalayer',
);

export default job;
17 changes: 17 additions & 0 deletions src/tasks/sync-organizations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { SimpleIntervalJob, Task } from 'toad-scheduler';
import { Organization } from '../models';

const task = new Task('sync-organizations', () => {
console.log('Subscribing to default organizations');
if (process.env.USE_SIMULATOR === 'false') {
Organization.subscribeToDefaultOrganizations();
}
});

const job = new SimpleIntervalJob(
{ days: 1, runImmediately: true },
task,
'sync-organizations',
);

export default job;
15 changes: 15 additions & 0 deletions src/tasks/sync-picklists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { SimpleIntervalJob, Task } from 'toad-scheduler';
import { pullPickListValues } from '../utils/data-loaders';

const task = new Task('sync-picklist', () => {
console.log('Syncing Picklist Values');
pullPickListValues();
});

const job = new SimpleIntervalJob(
{ days: 1, runImmediately: true },
task,
'sync-picklist',
);

export default job;

0 comments on commit 8b5c2c5

Please sign in to comment.