Skip to content

Commit

Permalink
[Tech Debt] Job scheduling with the Bree library
Browse files Browse the repository at this point in the history
  • Loading branch information
levinmr committed Jan 9, 2025
1 parent 1d8694f commit ec5606a
Show file tree
Hide file tree
Showing 12 changed files with 430 additions and 149 deletions.
6 changes: 0 additions & 6 deletions deploy/api.sh

This file was deleted.

120 changes: 0 additions & 120 deletions deploy/cron.js

This file was deleted.

6 changes: 0 additions & 6 deletions deploy/daily.sh

This file was deleted.

5 changes: 0 additions & 5 deletions deploy/hourly.sh

This file was deleted.

46 changes: 46 additions & 0 deletions deploy/publisher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}

if (process.env.NEW_RELIC_APP_NAME) {
require("newrelic");
}

const logger = require("../src/logger").initialize();
logger.info("===================================");
logger.info("=== STARTING ANALYTICS-REPORTER ===");
logger.info(" Running /deploy/publisher.js");
logger.info("===================================");

// Job Scheduler
const Bree = require("bree");
const bree = new Bree({
logger,
jobs: [
// Runs `../jobs/realtime.js` 1 millisecond after the process starts and
// then every 15 minutes going forward.
{
name: "realtime",
timeout: "1",
interval: "15m",
},
// Runs `../jobs/daily.js` 1 minute after the process starts and then at
// 10:01 AM every day going forward.
{
name: "daily",
timeout: "1m",
interval: "at 10:01 am",
},
// Runs `../jobs/api.js` 2 minutes after the process starts and then at
// 10:02 AM every day going forward.
{
name: "api",
timeout: "2m",
interval: "at 10:02 am",
},
],
});

(async () => {
await bree.start();
})();
5 changes: 0 additions & 5 deletions deploy/realtime.sh

This file was deleted.

27 changes: 27 additions & 0 deletions jobs/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
process.env.ANALYTICS_REPORTS_PATH = "reports/api.json";
process.env.ANALYTICS_SCRIPT_NAME = "api.js";

const { runQueuePublish } = require("../index.js");
const options = {
publish: true,
frequency: "daily",
slim: true,
debug: true,
csv: true,
json: true,
agenciesFile: `${process.env.ANALYTICS_ROOT_PATH}/deploy/agencies.json`,
};
const logger = require("../src/logger.js").initialize();

(async () => {
logger.info(`Beginning job: ${process.env.ANALYTICS_SCRIPT_NAME}`);

try {
await runQueuePublish(options);
logger.info(`Job completed: ${process.env.ANALYTICS_SCRIPT_NAME}`);
} catch (e) {
logger.error(`Job exited with error: ${process.env.ANALYTICS_SCRIPT_NAME}`);
logger.error(e);
throw e;
}
})();
27 changes: 27 additions & 0 deletions jobs/daily.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
process.env.ANALYTICS_REPORTS_PATH = "reports/usa.json";
process.env.ANALYTICS_SCRIPT_NAME = "daily.js";

const { runQueuePublish } = require("../index.js");
const options = {
publish: true,
frequency: "daily",
slim: true,
debug: true,
csv: true,
json: true,
agenciesFile: `${process.env.ANALYTICS_ROOT_PATH}/deploy/agencies.json`,
};
const logger = require("../src/logger.js").initialize();

(async () => {
logger.info(`Beginning job: ${process.env.ANALYTICS_SCRIPT_NAME}`);

try {
await runQueuePublish(options);
logger.info(`Job completed: ${process.env.ANALYTICS_SCRIPT_NAME}`);
} catch (e) {
logger.error(`Job exited with error: ${process.env.ANALYTICS_SCRIPT_NAME}`);
logger.error(e);
throw e;
}
})();
27 changes: 27 additions & 0 deletions jobs/realtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
process.env.ANALYTICS_REPORTS_PATH = "reports/usa.json";
process.env.ANALYTICS_SCRIPT_NAME = "realtime.js";

const { runQueuePublish } = require("../index.js");
const options = {
publish: true,
frequency: "realtime",
slim: true,
debug: true,
csv: true,
json: true,
agenciesFile: `${process.env.ANALYTICS_ROOT_PATH}/deploy/agencies.json`,
};
const logger = require("../src/logger.js").initialize();

(async () => {
logger.info(`Beginning job: ${process.env.ANALYTICS_SCRIPT_NAME}`);

try {
await runQueuePublish(options);
logger.info(`Job completed: ${process.env.ANALYTICS_SCRIPT_NAME}`);
} catch (e) {
logger.error(`Job exited with error: ${process.env.ANALYTICS_SCRIPT_NAME}`);
logger.error(e);
throw e;
}
})();
2 changes: 1 addition & 1 deletion manifest.publisher.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ applications:
health-check-type: process
buildpacks:
- nodejs_buildpack
command: node deploy/cron.js
command: node deploy/publisher.js
env:
ANALYTICS_DEBUG: 'true'
ANALYTICS_LOG_LEVEL: ${ANALYTICS_LOG_LEVEL}
Expand Down
Loading

0 comments on commit ec5606a

Please sign in to comment.