Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slack alert #69

Open
wants to merge 16 commits into
base: main
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
21 changes: 6 additions & 15 deletions src/cron.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const agenda = require('./interface/cron');
const jobs = require('./interface/cron/jobs');
const Reporter = require('./domain/reporter');

async function graceful() {
await agenda.stop();
Expand All @@ -10,26 +11,16 @@ async function graceful() {
(async function () {
try {
await agenda.start();
require('./interface/cron/events/addedFiles');
require('./interface/cron/events/editedFiles');
require('./interface/cron/events/addedCollaborator');
require('./interface/cron/events/registeredCollaboratorKey');
require('./interface/cron/events/removedCollaborator');
require('./interface/cron/events/updatedPortalMetadata');
require('./interface/cron/events/mint');
require('./interface/cron/events/process');
require('./interface/cron/events/contractEvents/process');
require('./interface/cron/events/publicPortalIndex');
require('./interface/cron/events/events');

await agenda.every('20 seconds', jobs.ADDED_FILE);
await agenda.every('20 seconds', jobs.EDITED_FILE);
await agenda.every('20 seconds', jobs.ADDED_COLLABORATOR);
await agenda.every('20 seconds', jobs.REGISTERED_COLLABORATOR_KEY);
await agenda.every('20 seconds', jobs.REMOVED_COLLABORATOR);
await agenda.every('20 seconds', jobs.UPDATED_PORTAL_METADATA);
await agenda.every('20 seconds', jobs.MINT);
await agenda.every('20 seconds', jobs.CONTRACT_EVENTS);
await agenda.every('10 seconds', jobs.PROCESS);
await agenda.every('10 seconds', jobs.PORTAL_INDEX);

} catch (err) {
await Reporter().alert(err.message, err.stack);
console.log(err.stack);
await graceful();
}
Expand Down
13 changes: 13 additions & 0 deletions src/domain/reporter/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const Slack = require('./slack');

// write a function that takes in a logger type and returns the corresponding logger
// if the logger type is not supported, return null
const Reporter = (loggerType) => {
if (loggerType === 'slack' || loggerType === undefined) {
return Slack;
}

throw new Error('Logger type not supported');
};

module.exports = Reporter;
21 changes: 21 additions & 0 deletions src/domain/reporter/slack/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const axios = require('axios');

const webhookUrl = process.env.SLACK_WEBHOOK_URL;
const channel = process.env.SLACK_CHANNEL;
const environment = process.env.DEPLOYMENT;

async function alert(error, detailedLogs) {
if (!webhookUrl || !channel || !environment) {
throw new Error('Missing required environment variables');
}

const message = {
channel: channel,
text: `*Environment*: ${environment}\n*Error*: ${error}\n*Detailed Logs*: ${detailedLogs}`,
mrkdwn: true
};

await axios.post(webhookUrl, message);
};

module.exports = { alert };
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const config = require('../config');
const logger = require('./infra/logger');
const Logger = require('./domain/reporter');

const app = require('./app');

Expand Down
1 change: 0 additions & 1 deletion src/infra/ucan.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ let verify = (req, res, next) => {
],
})
.then((result) => {
console.log("result of the request verifications", result);
if (result.ok) {
req.isAuthenticated = true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const constants = require("../../../constants");
const config = require("../../../../config");
const { EventProcessor, Event } = require("../../../infra/database/models");
const EventUtil = require("./utils");
const agenda = require("../index");
const jobs = require("../jobs");
const Reporter = require('../../../../domain/reporter');
const constants = require("../../../../constants");
const config = require("../../../../../config");
const { EventProcessor, Event } = require("../../../../infra/database/models");
const EventUtil = require("../utils");
const jobs = require("../../jobs");
const axios = require("axios");

const API_URL = config.SUBGRAPH_API;
const EVENT_NAME = "addedCollaborators";
const BATCH_SIZE = constants.CRON.BATCH_SIZE;

agenda.define(jobs.ADDED_COLLABORATOR, async (job, done) => {
async function addedCollaboratorHandler() {
try {
const addedCollaboratorCheckpoint = await fetchAddedCollaboratorCheckpoint();
const batchSize = BATCH_SIZE;
Expand All @@ -28,14 +28,13 @@ agenda.define(jobs.ADDED_COLLABORATOR, async (job, done) => {
if (lastEventCheckpont) {
await updateAddedCollaboratorCheckpoint(lastEventCheckpont);
}
done();
} catch (err) {
await Reporter().alert(jobs.ADDED_COLLABORATOR + "::" + err.message, err.stack);
console.error("Error in job", jobs.ADDED_COLLABORATOR, err.message);
done(err);
} finally {
console.log("Job done", jobs.ADDED_COLLABORATOR);
}
});
}

async function fetchAddedCollaboratorCheckpoint() {
const eventProcessed = await EventProcessor.findOne({});
Expand Down Expand Up @@ -101,3 +100,5 @@ function updateAddedCollaboratorCheckpoint(newCheckpoint) {
{ upsert: true }
);
}

module.exports = addedCollaboratorHandler;
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
const config = require("../../../../config");
const constants = require("../../../constants");
const { EventProcessor, Event } = require("../../../infra/database/models");
const agenda = require("../index");
const jobs = require("../jobs");
const Reporter = require('../../../../domain/reporter');
const config = require("../../../../../config");
const constants = require("../../../../constants");
const { EventProcessor, Event } = require("../../../../infra/database/models");
const jobs = require("../../jobs");
const axios = require("axios");
const EventUtil = require("./utils");
const EventUtil = require("../utils");

const API_URL = config.SUBGRAPH_API;
const EVENT_NAME = "addedFiles";
const BATCH_SIZE = constants.CRON.BATCH_SIZE;

agenda.define(jobs.ADDED_FILE, async (job, done) => {
let addedFiles = [];
async function addedFileHandler() {
try {
const addedFilesCheckpoint = await fetchAddedFilesCheckpoint();
const batchSize = BATCH_SIZE;
addedFiles = await fetchAddedFilesEvents(
const addedFiles = await fetchAddedFilesEvents(
addedFilesCheckpoint,
batchSize
);
Expand All @@ -25,14 +24,13 @@ agenda.define(jobs.ADDED_FILE, async (job, done) => {
if (lastEventCheckpont) {
await updateAddedFilesCheckpoint(lastEventCheckpont);
}
done();
} catch (err) {
await Reporter().alert(jobs.ADDED_FILE + "::" + err.message, err.stack);
console.error("Error in job", jobs.ADDED_FILE, err.message);
done(err);
} finally {
console.log("Job done", jobs.ADDED_FILE);
}
});
}

async function fetchAddedFilesCheckpoint() {
const eventProcessed = await EventProcessor.findOne({});
Expand Down Expand Up @@ -102,3 +100,6 @@ function updateAddedFilesCheckpoint(newCheckpoint) {
{ upsert: true }
);
}


module.exports = addedFileHandler;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Notification = require('../../../domain/notification');
const Notification = require('../../../../domain/notification');

async function completeNotification({ portalAddress, forAddress, type }) {
const complete = await Notification.markComplete({ portalAddress, forAddress, type });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Notification = require('../../../domain/notification');
const Notification = require('../../../../domain/notification');

async function createNotification(data) {
const createdNotification = await Notification.create(data);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
const config = require("../../../../config");
const constants = require("../../../constants");
const Reporter = require('../../../../domain/reporter');
const config = require("../../../../../config");
const constants = require("../../../../constants");

const { EventProcessor, Event } = require("../../../infra/database/models");
const agenda = require("../index");
const jobs = require("../jobs");
const { EventProcessor, Event } = require("../../../../infra/database/models");
const jobs = require("../../jobs");
const axios = require("axios");
const EventUtil = require("./utils");
const EventUtil = require("../utils");


const API_URL = config.SUBGRAPH_API;
const EVENT_NAME = "editedFiles";
const BATCH_SIZE = constants.CRON.BATCH_SIZE;


agenda.define(jobs.EDITED_FILE, async (job, done) => {
let editedFiles = [];
async function editedFilesHandler() {
try {
const editedFilesCheckpoint = await fetchEditedFilesCheckpoint();
const batchSize = BATCH_SIZE;
editedFiles = await fetchEditedFilesEvents(
const editedFiles = await fetchEditedFilesEvents(
editedFilesCheckpoint,
batchSize
);
Expand All @@ -28,14 +26,13 @@ agenda.define(jobs.EDITED_FILE, async (job, done) => {
if (lastEventCheckpont) {
await updateEditedFilesCheckpoint(lastEventCheckpont);
}
done();
} catch (err) {
await Reporter().alert(jobs.EDITED_FILE + "::" + err.message, err.stack);
console.error("Error in job", jobs.EDITED_FILE, err.message);
done(err);
} finally {
console.log("Job done", jobs.EDITED_FILE);
}
});
}


async function fetchEditedFilesCheckpoint() {
Expand Down Expand Up @@ -105,3 +102,5 @@ function updateEditedFilesCheckpoint(newCheckpoint) {
{ upsert: true }
);
}

module.exports = editedFilesHandler;
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
const config = require("../../../../config");
const constants = require("../../../constants");
const Reporter = require('../../../../domain/reporter');
const config = require("../../../../../config");
const constants = require("../../../../constants");

const { EventProcessor, Event } = require("../../../infra/database/models");
const agenda = require("../index");
const jobs = require("../jobs");
const { EventProcessor, Event } = require("../../../../infra/database/models");
const jobs = require("../../jobs");
const axios = require("axios");
const EventUtil = require("./utils");
const EventUtil = require("../utils");

const API_URL = config.SUBGRAPH_API;
const EVENT_NAME = "mints";
const BATCH_SIZE = constants.CRON.BATCH_SIZE;

agenda.define(jobs.MINT, async (job, done) => {
let mints = [];
async function mintHandler() {
try {
const mintCheckpoint = await fetchMintCheckpoint();
const batchSize = BATCH_SIZE;
Expand All @@ -23,14 +22,13 @@ agenda.define(jobs.MINT, async (job, done) => {
if (lastEventCheckpont) {
await updateMintCheckpoint(lastEventCheckpont);
}
done();
} catch (err) {
await Reporter().alert(jobs.MINT + "::" + err.message, err.stack);
console.error("Error in job", jobs.MINT, err.message);
done(err);
} finally {
console.log("Job done", jobs.MINT);
}
});
}

async function fetchMintCheckpoint() {
const eventProcessed = await EventProcessor.findOne({});
Expand Down Expand Up @@ -94,3 +92,5 @@ function updateMintCheckpoint(newCheckpoint) {
{ upsert: true }
);
}

module.exports = mintHandler;
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
const { EventProcessor, Event } = require("../../../infra/database/models");
const agenda = require("../index");
const jobs = require("../jobs");
const Reporter = require('../../../../domain/reporter');
const { EventProcessor, Event } = require("../../../../infra/database/models");
const jobs = require("../../jobs");
const processEvent = require('./processEvent');
const constants = require("../../../constants");
const constants = require("../../../../constants");

const FetchEventCount = constants.CRON.PROCESS_LIMIT;

agenda.define(jobs.PROCESS, async (job, done) => {
async function process() {
try {
const minCheckpoint = await fetchMinCheckpoint();
const events = await fetchEvents(minCheckpoint, FetchEventCount);
console.log("Received entries", jobs.PROCESS, events.length);
await processStoredEvents(events);
done();
} catch (err) {
await Reporter().alert(jobs.PROCESS + "::" + err.message, err.stack);
console.error("Error in job", jobs.PROCESS, err.message);
done(err);
} finally {
console.log("Job done", jobs.PROCESS);
}
});
}

async function fetchMinCheckpoint() {
const eventProcessed = await EventProcessor.findOne({});
Expand Down Expand Up @@ -62,3 +63,5 @@ async function processStoredEvents(events) {
const data = await Promise.all(allPromises);
return data;
}

module.exports = process;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Common = require("../../../domain/common");
const Portal = require("../../../domain/portal");
const Common = require("../../../../domain/common");
const Portal = require("../../../../domain/portal");
const createNotification = require("./createNotification");
const completeNotificationAction = require("./completeNotificationAction");

Expand Down Expand Up @@ -183,18 +183,18 @@ async function processRemovedCollaboratorEvent({

async function getNotificationTypeFromFileDataType(fileDataType, created = false) {
if (fileDataType === 'dPage') {
return created ? 'dPagePublish': 'dPageEdit';
return created ? 'dPagePublish' : 'dPageEdit';
}
if (fileDataType === 'whiteboard') {
return created ? 'whiteboardPublish': 'whiteboardEdit';
return created ? 'whiteboardPublish' : 'whiteboardEdit';
}
if (fileDataType === 'dDoc') {
return created ? 'dDocPublish': 'dDocEdit';
return created ? 'dDocPublish' : 'dDocEdit';
}
if (fileDataType === 'file') {
return created ? 'addFile': 'editFile';
return created ? 'addFile' : 'editFile';
}
return created ? 'addFile': 'editFile';
return created ? 'addFile' : 'editFile';
}

async function getFileTypeText(fileType) {
Expand Down
Loading