Skip to content
This repository was archived by the owner on Apr 1, 2019. It is now read-only.

Commit d6c9ffb

Browse files
authored
Merge pull request #63 from nhsuk/feature/enable-disable
✋ expose env var to disable scheduler
2 parents 03b51a9 + 0aeb645 commit d6c9ffb

File tree

7 files changed

+84
-10
lines changed

7 files changed

+84
-10
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ i.e. `export ETL_SCHEDULE='25 15 * * *'` to start the processing a 3:25pm. Note:
2727

2828
Further details available [here](https://www.npmjs.com/package/node-schedule)
2929

30+
The scheduler can be completely disabled by setting the `DISABLE_SCHEDULER` variable to `true`. This sets the run date to run once in the future on Jan 1st, 2100.
31+
3032
A successful scrape will result in the file `gp-data.json` being written to the `output` folder.
3133
Two files are also written to Azure storage: `gp-data.json` and a time stamped version of the same data named `gp-data-YYYYMMDD.json` where `YYYYMMDD` is the current year, day and month. Summary JSON is available at `output/summary.json`.
3234

@@ -53,6 +55,23 @@ No facility is provided to interrogate the contents of the blob storage. However
5355
all uploaded files are available at the address `https://nhsukgpdataetl.blob.core.windows.net/etl-output`,
5456
i.e. [https://nhsukgpdataetl.blob.core.windows.net/etl-output/gp-data.json](https://nhsukgpdataetl.blob.core.windows.net/etl-output/gp-data.json).
5557

58+
## Environment variables
59+
60+
Environment variables are expected to be managed by the environment in which
61+
the application is being run. This is best practice as described by
62+
[twelve-factor](https://12factor.net/config).
63+
64+
| Variable | Description | Default | Required |
65+
| :--------------------------------- | :------------------------------------------------------------------- | ----------------------- | :--------- |
66+
| `NODE_ENV` | node environment | development | |
67+
| `LOG_LEVEL` | [log level](https://github.com/trentm/node-bunyan#levels) | Depends on `NODE_ENV` | |
68+
| `SYNDICATION_API_KEY` | API key to access syndication | | yes |
69+
| `AZURE_STORAGE_CONNECTION_STRING` | Azure storage connection string | | yes |
70+
| `AZURE_TIMEOUT_MINUTES` | Maximum wait time when uploading file to Azure | 5 | |
71+
| `CONTAINER_NAME` | Azure storage container name | etl-output | |
72+
| `UPDATE_SCHEDULE` | time of day to run the upgrade | * 23 * * * (11:00 pm) | |
73+
| `DISABLE_SCHEDULER` | set to 'true' to disable the scheduler | false | |
74+
5675
## Architecture Decision Records
5776

5877
This repo uses

app/lib/scheduleConfig.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const defaultSchedule = '0 23 * * *';
2+
const farFutureDate = new Date(2100, 0, 1, 0, 0);
3+
4+
function schedulerDisabled() {
5+
return process.env.DISABLE_SCHEDULER === 'true';
6+
}
7+
8+
function getSchedule() {
9+
return schedulerDisabled() ? farFutureDate : process.env.ETL_SCHEDULE || defaultSchedule;
10+
}
11+
12+
module.exports = {
13+
defaultSchedule,
14+
farFutureDate,
15+
getSchedule
16+
};

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ services:
88
environment:
99
SYNDICATION_API_KEY: ${SYNDICATION_API_KEY}
1010
ETL_SCHEDULE: ${ETL_SCHEDULE}
11+
DISABLE_SCHEDULER: ${DISABLE_SCHEDULER}
1112
AZURE_STORAGE_CONNECTION_STRING: '${AZURE_STORAGE_CONNECTION_STRING}'
1213
AZURE_TIMEOUT_MINUTES: ${AZURE_TIMEOUT_MINUTES}
1314
CONTAINER_NAME: ${CONTAINER_NAME}

rancher-config/docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ services:
77
environment:
88
NODE_ENV: production
99
SYNDICATION_API_KEY: ${SYNDICATION_API_KEY}
10+
DISABLE_SCHEDULER: ${DISABLE_SCHEDULER}
1011
ETL_SCHEDULE: ${ETL_SCHEDULE}
1112
AZURE_STORAGE_CONNECTION_STRING: '${AZURE_STORAGE_CONNECTION_STRING}'
1213
AZURE_TIMEOUT_MINUTES: ${AZURE_TIMEOUT_MINUTES}

schedule.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
const schedule = require('node-schedule');
2-
2+
const scheduleConfig = require('./app/lib/scheduleConfig');
33
const etl = require('./app/lib/etl');
44
const log = require('./app/lib/utils/logger');
55

6-
const rule = process.env.ETL_SCHEDULE || '0 23 * * *';
7-
8-
log.info(`Scheduling job with rule '${rule}'`);
9-
schedule.scheduleJob(rule, () => {
6+
log.info(`Scheduling job with rule '${scheduleConfig.getSchedule()}'`);
7+
schedule.scheduleJob(scheduleConfig.getSchedule(), () => {
108
etl.start();
119
});

scripts/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ Below is a list of scripts available, along with a simple description of
77
what each one does. The details of what they are doing is available within the
88
script.
99

10-
[`pre-bootstrap`](pre-bootstrap)
11-
Directs towards base development machine setup.
10+
[`deploy`](deploy)
11+
Clone [ci-deployment](https://github.com/nhsuk/ci-deployment.git) repo and
12+
execute `deploy` script.
1213

1314
[`bootstrap`](bootstrap)
1415
Installs project's direct dependencies e.g. npm packages.
1516

17+
[`pre-bootstrap`](pre-bootstrap)
18+
Directs towards base development machine setup.
19+
1620
[`start`](start)
1721
Starts the application a Docker container.
1822

@@ -25,6 +29,3 @@ Starts a Docker container specifically for continually running tests.
2529
[`test-ci`](test-ci)
2630
Runs the tests in a Docker container once so that an exit code is reported and
2731
can be used by the CI server.
28-
29-
[`ci-deployment`](ci-deployment)
30-
Infrastructure related work.

test/unit/scheduleConfig.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const chai = require('chai');
2+
const config = require('../../app/lib/scheduleConfig');
3+
4+
const expect = chai.expect;
5+
6+
const disableScheduler = process.env.DISABLE_SCHEDULER;
7+
const etlSchedule = process.env.ETL_SCHEDULE;
8+
9+
function resetEnv() {
10+
process.env.disableScheduler = disableScheduler;
11+
process.env.ETL_SCHEDULE = etlSchedule;
12+
}
13+
14+
describe('getSchedule', () => {
15+
it('should return a date far in the future when scheduler disabled', () => {
16+
process.env.DISABLE_SCHEDULER = 'true';
17+
expect(config.getSchedule()).to.be.a('Date');
18+
expect(config.getSchedule()).to.equal(config.farFutureDate);
19+
expect(config.getSchedule().getFullYear()).to.equal(2100);
20+
});
21+
22+
it('should return default cron string when scheduler enabled and no ETL_SCHEDULE set', () => {
23+
process.env.DISABLE_SCHEDULER = 'false';
24+
delete process.env.ETL_SCHEDULE;
25+
expect(config.getSchedule()).to.equal(config.defaultSchedule);
26+
});
27+
28+
it('should return the ETL_SCHEDULE cron string when scheduler enabled and ETL_SCHEDULE set', () => {
29+
const customSchedule = '0 7 * * *';
30+
process.env.DISABLE_SCHEDULER = 'false';
31+
process.env.ETL_SCHEDULE = customSchedule;
32+
expect(config.getSchedule()).to.equal(customSchedule);
33+
});
34+
35+
afterEach(() => {
36+
resetEnv();
37+
});
38+
});

0 commit comments

Comments
 (0)