Skip to content

Commit

Permalink
Merge pull request #108 from crownheightsaid/send-error-notification-#90
Browse files Browse the repository at this point in the history
Send error notification to slack
  • Loading branch information
jorganisak authored Jul 18, 2020
2 parents 64a37a4 + 5673b91 commit 241804f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
39 changes: 39 additions & 0 deletions src/lib/slack/errorNotification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const slackapi = require("~slack/webApi");
const { TECH_CHANNEL } = require("~slack/constants");
const { findChannelByName } = require("~slack/channels");

const sentErrors = [];

module.exports = async function sendErrorNotification(error) {
/**
*
*/
const now = new Date();

const twoHoursAgo = new Date(now.getTime() - 120 * 60000);

let shouldSend = true;

for (const sentError of sentErrors) {
// dont send if error occured within last 2 hours
if (
sentError[0] === error.message &&
sentError[1].getTime() > twoHoursAgo.getTime()
) {
shouldSend = false;
}
}

if (shouldSend) {
let errText = `${error.stack}`;
errText = `<!here|here> - We've got an error!\n\`\`\`${errText}\`\`\``;

const wgTechChannel = await findChannelByName(TECH_CHANNEL);
await slackapi.chat.postMessage({
channel: wgTechChannel.id,
text: errText,
});

sentErrors.push([error.message, now]);
}
};
2 changes: 1 addition & 1 deletion src/lib/strings/locales/en/slackapp.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"intakeVolunteers": "intake_volunteers",
"deliveryVolunteers": "delivery_volunteers",
"flyering": "flyer_squad",
"tech": "tech",
"tech": "wg_tech",
"communityReimbursements": "community_reimbursement",
"hatHolders": "hat_holders"
},
Expand Down
11 changes: 9 additions & 2 deletions src/workers/airtable-sync/paymentWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ const {
paymentRequestsFields,
paymentRequestsSensitiveFields,
} = require("~airtable/tables/paymentRequests");
const sendErrorNotification = require("~slack/errorNotification");
const newExternalDonorPayment = require("./actions/payments/newExternalDonorPayment");
const newPaymentRequest = require("./actions/payments/newPaymentRequest");
const updateReimbursementMessage = require("./actions/payments/updateReimbursementStatus");

const defaultInterval = 10000;

const errFunc = async (error) => {
sendErrorNotification(error);
};

function startWorker(interval) {
let pollInterval = interval;
if (pollInterval < defaultInterval) {
Expand Down Expand Up @@ -50,7 +55,8 @@ function startWorker(interval) {
}
});
return Promise.all(promises);
}
},
errFunc
);

const donorSignupChanges = new ChangeDetector(donorPaymentsTable, {
Expand Down Expand Up @@ -82,7 +88,8 @@ function startWorker(interval) {
}
});
return Promise.all(promises);
}
},
errFunc
);
}

Expand Down
8 changes: 7 additions & 1 deletion src/workers/airtable-sync/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ const {
fields: requestFields,
SENSITIVE_FIELDS: sensitiveRequestFields,
} = require("~airtable/tables/requests");
const sendErrorNotification = require("~slack/errorNotification");
const updateMessageContent = require("./actions/updateMessageContent");
const notifyManyc = require("./actions/notifyManyc");

const defaultInterval = 10000;

const errFunc = async (error) => {
await sendErrorNotification(error);
};

function startWorker(interval) {
let pollInterval = interval;
if (pollInterval < defaultInterval) {
Expand Down Expand Up @@ -57,7 +62,8 @@ function startWorker(interval) {
}
});
return Promise.all(promises);
}
},
errFunc
);
}

Expand Down

0 comments on commit 241804f

Please sign in to comment.