Skip to content

Commit ec5606a

Browse files
committed
[Tech Debt] Job scheduling with the Bree library
1 parent 1d8694f commit ec5606a

File tree

12 files changed

+430
-149
lines changed

12 files changed

+430
-149
lines changed

deploy/api.sh

Lines changed: 0 additions & 6 deletions
This file was deleted.

deploy/cron.js

Lines changed: 0 additions & 120 deletions
This file was deleted.

deploy/daily.sh

Lines changed: 0 additions & 6 deletions
This file was deleted.

deploy/hourly.sh

Lines changed: 0 additions & 5 deletions
This file was deleted.

deploy/publisher.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
if (process.env.NODE_ENV !== "production") {
2+
require("dotenv").config();
3+
}
4+
5+
if (process.env.NEW_RELIC_APP_NAME) {
6+
require("newrelic");
7+
}
8+
9+
const logger = require("../src/logger").initialize();
10+
logger.info("===================================");
11+
logger.info("=== STARTING ANALYTICS-REPORTER ===");
12+
logger.info(" Running /deploy/publisher.js");
13+
logger.info("===================================");
14+
15+
// Job Scheduler
16+
const Bree = require("bree");
17+
const bree = new Bree({
18+
logger,
19+
jobs: [
20+
// Runs `../jobs/realtime.js` 1 millisecond after the process starts and
21+
// then every 15 minutes going forward.
22+
{
23+
name: "realtime",
24+
timeout: "1",
25+
interval: "15m",
26+
},
27+
// Runs `../jobs/daily.js` 1 minute after the process starts and then at
28+
// 10:01 AM every day going forward.
29+
{
30+
name: "daily",
31+
timeout: "1m",
32+
interval: "at 10:01 am",
33+
},
34+
// Runs `../jobs/api.js` 2 minutes after the process starts and then at
35+
// 10:02 AM every day going forward.
36+
{
37+
name: "api",
38+
timeout: "2m",
39+
interval: "at 10:02 am",
40+
},
41+
],
42+
});
43+
44+
(async () => {
45+
await bree.start();
46+
})();

deploy/realtime.sh

Lines changed: 0 additions & 5 deletions
This file was deleted.

jobs/api.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
process.env.ANALYTICS_REPORTS_PATH = "reports/api.json";
2+
process.env.ANALYTICS_SCRIPT_NAME = "api.js";
3+
4+
const { runQueuePublish } = require("../index.js");
5+
const options = {
6+
publish: true,
7+
frequency: "daily",
8+
slim: true,
9+
debug: true,
10+
csv: true,
11+
json: true,
12+
agenciesFile: `${process.env.ANALYTICS_ROOT_PATH}/deploy/agencies.json`,
13+
};
14+
const logger = require("../src/logger.js").initialize();
15+
16+
(async () => {
17+
logger.info(`Beginning job: ${process.env.ANALYTICS_SCRIPT_NAME}`);
18+
19+
try {
20+
await runQueuePublish(options);
21+
logger.info(`Job completed: ${process.env.ANALYTICS_SCRIPT_NAME}`);
22+
} catch (e) {
23+
logger.error(`Job exited with error: ${process.env.ANALYTICS_SCRIPT_NAME}`);
24+
logger.error(e);
25+
throw e;
26+
}
27+
})();

jobs/daily.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
process.env.ANALYTICS_REPORTS_PATH = "reports/usa.json";
2+
process.env.ANALYTICS_SCRIPT_NAME = "daily.js";
3+
4+
const { runQueuePublish } = require("../index.js");
5+
const options = {
6+
publish: true,
7+
frequency: "daily",
8+
slim: true,
9+
debug: true,
10+
csv: true,
11+
json: true,
12+
agenciesFile: `${process.env.ANALYTICS_ROOT_PATH}/deploy/agencies.json`,
13+
};
14+
const logger = require("../src/logger.js").initialize();
15+
16+
(async () => {
17+
logger.info(`Beginning job: ${process.env.ANALYTICS_SCRIPT_NAME}`);
18+
19+
try {
20+
await runQueuePublish(options);
21+
logger.info(`Job completed: ${process.env.ANALYTICS_SCRIPT_NAME}`);
22+
} catch (e) {
23+
logger.error(`Job exited with error: ${process.env.ANALYTICS_SCRIPT_NAME}`);
24+
logger.error(e);
25+
throw e;
26+
}
27+
})();

jobs/realtime.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
process.env.ANALYTICS_REPORTS_PATH = "reports/usa.json";
2+
process.env.ANALYTICS_SCRIPT_NAME = "realtime.js";
3+
4+
const { runQueuePublish } = require("../index.js");
5+
const options = {
6+
publish: true,
7+
frequency: "realtime",
8+
slim: true,
9+
debug: true,
10+
csv: true,
11+
json: true,
12+
agenciesFile: `${process.env.ANALYTICS_ROOT_PATH}/deploy/agencies.json`,
13+
};
14+
const logger = require("../src/logger.js").initialize();
15+
16+
(async () => {
17+
logger.info(`Beginning job: ${process.env.ANALYTICS_SCRIPT_NAME}`);
18+
19+
try {
20+
await runQueuePublish(options);
21+
logger.info(`Job completed: ${process.env.ANALYTICS_SCRIPT_NAME}`);
22+
} catch (e) {
23+
logger.error(`Job exited with error: ${process.env.ANALYTICS_SCRIPT_NAME}`);
24+
logger.error(e);
25+
throw e;
26+
}
27+
})();

manifest.publisher.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ applications:
99
health-check-type: process
1010
buildpacks:
1111
- nodejs_buildpack
12-
command: node deploy/cron.js
12+
command: node deploy/publisher.js
1313
env:
1414
ANALYTICS_DEBUG: 'true'
1515
ANALYTICS_LOG_LEVEL: ${ANALYTICS_LOG_LEVEL}

0 commit comments

Comments
 (0)