Skip to content
This repository has been archived by the owner on Aug 9, 2021. It is now read-only.

Add Logging and reduce Overnight Posting #27

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 79 additions & 23 deletions announcer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,26 @@ const Airtable = require('airtable');
const hook_url = "https://hooks.slack.com/services/" + process.env.SLACK_TOKEN;
const CronJob = require('cron').CronJob;
const Slack = require('node-slack');
const { createLogger, format, transports } = require('winston');
const { combine, timestamp, label, prettyPrint } = format;
const slack = new Slack(hook_url);
const base = new Airtable({
apiKey: process.env.AIRTABLE_API_KEY
}).base(process.env.AIRTABLE_BASE);

const logger = createLogger({
format: combine(
timestamp(),
prettyPrint()
),
transports: [
new transports.File({ filename: 'error.log', level: 'error' }),
new transports.File({ filename: 'combined.log' }),
new transports.Console(),
],
level: 'info'
});

var airtableCronJobs = [];

function refreshCronTable () {
Expand All @@ -25,69 +40,110 @@ function refreshCronTable () {
}).eachPage(function page(records, fetchNextPage) {

// This function (`page`) will get called for each page of records.

records.forEach(function (record) {

var name = record.get('Name');
var sec = record.get('Second');
var min = record.get('Minute');
var hor = record.get('Hour');
var dom = record.get('Day of Month');
var mon = record.get('Month');
var dow = record.get('Day of Week');
var fan = record.get('Force At Night');
fan = (fan === true) ? true: false;

if (min > 1 && min < 5) {
min = 5;
console.log(`job ${name} was modified to run outside of the CRON table refresh window`);
logger.log({
level: 'info',
message: `job ${name} was modified to run outside of the CRON table refresh window`
})
}

logger.log({
level: 'info',
message: `Forcing at Night ${fan}`
});

var airtable_cron = (sec + ' ' + min + ' ' + hor + ' ' + dom + ' ' + mon + ' ' + dow);

airtableCronJobs.push(new CronJob(airtable_cron, function () {
console.log(`Running job ${name}`);

// See what channels are associated with this entry.
record.get('Channels').forEach(function (channel) {

slack.send({
text: record.get('Text'),
channel: channel.toString(),
username: record.get('Announcer Name')
});

})
//Makes the bot not post overnight unless the post is forced.
var OutsideNormalHours = ((new Date().getUTCHours() >= 6 && new Date().getUTCHours() <= 13)) ? true : false;
logger.log({
level: 'debug',
message: `Current local server date ${new Date().getUTCHours()}`
});
if(!OutsideNormalHours || fan) {
record.get('Channels').forEach(function (channel) {
logger.log({
level: 'info',
message: `Successfully sent '` +record.get('Text').substring(0,30) + `' message to channel ${channel}`
})
try {
slack.send({
text: record.get('Text'),
channel: channel.toString(),
username: record.get('Announcer Name')
});
logger.log({
level: 'info',
message: `Successfully sent '` +record.get('Text').substring(0,30) + `' message to channel ${channel}`
})
} catch (ex) {
logger.log({
level: 'error',
message: `Unable to run job ${name}: ${ex}`
})
}

})
}
}, null, true, 'America/Los_Angeles'));

});

}, null, true, 'America/Los_Angeles'));

// To fetch the next page of records, call `fetchNextPage`.
// If there are more records, `page` will get called again.
// If there are no more records, `done` will get called.
fetchNextPage();

}, function done(error) {
if (error) {
console.log(error);
logger.log({
level: 'crit',
message: `Error Ocurred: ${error}`
})
}
});
}
});
};

// Refresh the CRON table immediately upon npm start
try {
refreshCronTable();
} catch (ex) {
console.log(`Error refreshing Cron Table: ${ex}`);
logger.log({
level: 'crit',
message: `Error refreshing Cron Table: ${ex}`
})
}

function OnComplete() {

}
// and then flush and reload the CRON table at 3 minutes and 3 seconds past every hour
// This is specifically offset from 5, 10, 15 minute intervals to ensure that
// a CRON job is not set to fire whe the CRON table is being refreshed

const update_cron = '3 3 * * * *';

try {

new CronJob(update_cron, refreshCronTable, null, true, 'America/Los_Angeles');
} catch (ex) {
console.log(`Invalid Cron Pattern: ${ex}`);
logger.log({
level: 'crit',
message: `Invalid Cron Pattern: ${ex}`
})
}

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"dependencies": {
"airtable": "^0.4.3",
"cron": "^1.2.1",
"node-slack": "0.0.7"
"node-slack": "0.0.7",
"winston": "3.1.0"
},
"devDependencies": {
"dotenv": "^2.0.0",
Expand Down