Skip to content

Commit

Permalink
check for both config.env and config.environment when setting api env
Browse files Browse the repository at this point in the history
  • Loading branch information
aflanagan committed Sep 4, 2024
1 parent 901e6f4 commit a58d2ff
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
26 changes: 13 additions & 13 deletions lib/cronitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function Cronitor(apiKey, config = {}) {
const path = config.config || process.env.CRONITOR_CONFIG;
const version = config.apiVersion || process.env.CRONITOR_API_VERSION || null;
const timeout = config.timeout || process.env.CRONITOR_TIMEOUT || 10000;
const env = config.env || process.env.CRONITOR_ENV || null;
const env = config.environment || config.env || process.env.CRONITOR_ENVIRONMENT || process.env.CRONITOR_ENV || null;
const headers = {
'User-Agent': 'cronitor-js',
'Authorization': 'Basic ' + new Buffer.from(apiKey + ':').toString('base64'),
Expand Down Expand Up @@ -45,7 +45,7 @@ function Cronitor(apiKey, config = {}) {
this.Monitor._api = this._api;
this.Event._api = this._api;

this.generateConfig = async ({path=this.path, group=null}={}) => {
this.generateConfig = async ({ path = this.path, group = null } = {}) => {
let url = 'https://cronitor.io/api/monitors.yaml';

if (!path) {
Expand All @@ -66,18 +66,18 @@ function Cronitor(apiKey, config = {}) {
};


this.applyConfig = async function({ path = this.path, rollback = false } = {}) {
this.applyConfig = async function ({ path = this.path, rollback = false } = {}) {
if (!path) throw new Errors.ConfigError('Must include a path to config file e.g. cronitor.applyConfig({path: \'./cronitor.yaml\'})');

try {
config = await this.readConfig({ path, output: true});
config = await this.readConfig({ path, output: true });
} catch (err) {
console.error('Error reading config:', err);
return false
}

try {
await Monitor.put(config, {rollback, format: Monitor.requestType.YAML});
await Monitor.put(config, { rollback, format: Monitor.requestType.YAML });
console.log(`Cronitor config ${rollback ? 'validated' : 'applied'} successfully.`)
return true
} catch (err) {
Expand All @@ -86,11 +86,11 @@ function Cronitor(apiKey, config = {}) {
}
};

this.validateConfig = async ({ path = this.path} = {}) => {
this.validateConfig = async ({ path = this.path } = {}) => {
return this.applyConfig({ path, rollback: true });
};

this.readConfig = async function({path = null, output = false}={}) {
this.readConfig = async function ({ path = null, output = false } = {}) {
if (!path) throw new Errors.ConfigError('Must include a path to config file e.g. cronitor.readConfig({path: \'./cronitor.yaml\'})');
if (!this.path) this.path = path;

Expand All @@ -106,9 +106,9 @@ function Cronitor(apiKey, config = {}) {
};


this.wrap = function(key, callback) {
this.wrap = function (key, callback) {
const _cronitor = this;
return async function(args) {
return async function (args) {
const monitor = new _cronitor.Monitor(key);
const series = _cronitor.newSeries();
await monitor.ping({ state: _cronitor.Monitor.State.RUN, series });
Expand All @@ -123,20 +123,20 @@ function Cronitor(apiKey, config = {}) {
};
};

this.wraps = function(lib) {
this.wraps = function (lib) {
// https://github.com/node-cron/node-cron
if (lib.schedule) {
this.schedule = function(key, schedule, cb, options) {
this.schedule = function (key, schedule, cb, options) {
const job = this.wrap(key, cb);
return lib.schedule(schedule, job, options);
};
} else if (lib.job) { // https://github.com/kelektiv/node-cron
this.job = function(key, schedule, cb) {
this.job = function (key, schedule, cb) {
const wrapped = this.wrap(key, cb);
return lib.job(schedule, wrapped);
};

this.schedule = function(key, schedule, cb) {
this.schedule = function (key, schedule, cb) {
const job = this.job(key, schedule, cb);
job.start();
};
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cronitor",
"version": "2.4.0",
"version": "2.4.1",
"description": "A simple library for reliably monitoring cron jobs, control-loops, or other important system events with Cronitor.",
"main": "lib/cronitor.js",
"repository": {
Expand Down Expand Up @@ -38,4 +38,4 @@
"sinon-chai": "^3.7.0",
"sinon-stub-promise": "^4.0.0"
}
}
}

0 comments on commit a58d2ff

Please sign in to comment.