Skip to content

Commit e2cadd4

Browse files
authored
Merge pull request #939 from 18F/feature/replace_cron_script_with_scheduler
[Tech Debt] Job scheduling with the Bree library
2 parents 2bb77e4 + 929e242 commit e2cadd4

File tree

13 files changed

+430
-152
lines changed

13 files changed

+430
-152
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ The process for adding features to this project is described in
1919

2020
## Local development setup
2121

22-
### Prerequistites
22+
### Prerequisites
2323

24-
* NodeJS > v20.x
24+
* NodeJS > v22.x
2525
* A postgres DB running and/or docker installed
2626

2727
### Install dependencies
@@ -144,7 +144,7 @@ This file is ignored in the `.gitignore` file and should not be checked in to th
144144
npm start
145145

146146
# running the app with dotenv-cli
147-
dotenv -e .env npm start
147+
npx dotenv -e .env npm start
148148
```
149149

150150
## Configuration

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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
frequency: "daily",
7+
debug: true,
8+
"write-to-database": true,
9+
agenciesFile: `${process.env.ANALYTICS_ROOT_PATH}/deploy/agencies.json`,
10+
};
11+
const logger = require("../src/logger.js").initialize();
12+
13+
(async () => {
14+
logger.info(`Beginning job: ${process.env.ANALYTICS_SCRIPT_NAME}`);
15+
16+
try {
17+
await runQueuePublish(options);
18+
logger.info(`Job completed: ${process.env.ANALYTICS_SCRIPT_NAME}`);
19+
} catch (e) {
20+
logger.error(`Job exited with error: ${process.env.ANALYTICS_SCRIPT_NAME}`);
21+
logger.error(e);
22+
throw e;
23+
}
24+
})();

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)