diff --git a/src/cron.js b/src/cron.js index 0f72e66..a3d835a 100644 --- a/src/cron.js +++ b/src/cron.js @@ -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(); @@ -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(); } diff --git a/src/domain/reporter/index.js b/src/domain/reporter/index.js new file mode 100644 index 0000000..8be9183 --- /dev/null +++ b/src/domain/reporter/index.js @@ -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; \ No newline at end of file diff --git a/src/domain/reporter/slack/index.js b/src/domain/reporter/slack/index.js new file mode 100644 index 0000000..0b62268 --- /dev/null +++ b/src/domain/reporter/slack/index.js @@ -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 }; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 903de46..fd9821c 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,6 @@ const config = require('../config'); const logger = require('./infra/logger'); +const Logger = require('./domain/reporter'); const app = require('./app'); diff --git a/src/infra/ucan.js b/src/infra/ucan.js index 14c1d65..55e751b 100644 --- a/src/infra/ucan.js +++ b/src/infra/ucan.js @@ -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; } diff --git a/src/interface/cron/events/addedCollaborator.js b/src/interface/cron/events/contractEvents/addedCollaborator.js similarity index 82% rename from src/interface/cron/events/addedCollaborator.js rename to src/interface/cron/events/contractEvents/addedCollaborator.js index cb36e7d..bc15008 100644 --- a/src/interface/cron/events/addedCollaborator.js +++ b/src/interface/cron/events/contractEvents/addedCollaborator.js @@ -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; @@ -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({}); @@ -101,3 +100,5 @@ function updateAddedCollaboratorCheckpoint(newCheckpoint) { { upsert: true } ); } + +module.exports = addedCollaboratorHandler; diff --git a/src/interface/cron/events/addedFiles.js b/src/interface/cron/events/contractEvents/addedFiles.js similarity index 80% rename from src/interface/cron/events/addedFiles.js rename to src/interface/cron/events/contractEvents/addedFiles.js index 22f6a86..06cfc88 100644 --- a/src/interface/cron/events/addedFiles.js +++ b/src/interface/cron/events/contractEvents/addedFiles.js @@ -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 ); @@ -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({}); @@ -102,3 +100,6 @@ function updateAddedFilesCheckpoint(newCheckpoint) { { upsert: true } ); } + + +module.exports = addedFileHandler; \ No newline at end of file diff --git a/src/interface/cron/events/completeNotificationAction.js b/src/interface/cron/events/contractEvents/completeNotificationAction.js similarity index 78% rename from src/interface/cron/events/completeNotificationAction.js rename to src/interface/cron/events/contractEvents/completeNotificationAction.js index 02aed5a..622a923 100644 --- a/src/interface/cron/events/completeNotificationAction.js +++ b/src/interface/cron/events/contractEvents/completeNotificationAction.js @@ -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 }); diff --git a/src/interface/cron/events/createNotification.js b/src/interface/cron/events/contractEvents/createNotification.js similarity index 73% rename from src/interface/cron/events/createNotification.js rename to src/interface/cron/events/contractEvents/createNotification.js index cd57f40..766e183 100644 --- a/src/interface/cron/events/createNotification.js +++ b/src/interface/cron/events/contractEvents/createNotification.js @@ -1,4 +1,4 @@ -const Notification = require('../../../domain/notification'); +const Notification = require('../../../../domain/notification'); async function createNotification(data) { const createdNotification = await Notification.create(data); diff --git a/src/interface/cron/events/editedFiles.js b/src/interface/cron/events/contractEvents/editedFiles.js similarity index 80% rename from src/interface/cron/events/editedFiles.js rename to src/interface/cron/events/contractEvents/editedFiles.js index dfcf11e..01d5f49 100644 --- a/src/interface/cron/events/editedFiles.js +++ b/src/interface/cron/events/contractEvents/editedFiles.js @@ -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 ); @@ -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() { @@ -105,3 +102,5 @@ function updateEditedFilesCheckpoint(newCheckpoint) { { upsert: true } ); } + +module.exports = editedFilesHandler; \ No newline at end of file diff --git a/src/interface/cron/events/mint.js b/src/interface/cron/events/contractEvents/mint.js similarity index 80% rename from src/interface/cron/events/mint.js rename to src/interface/cron/events/contractEvents/mint.js index 5ac96a4..a8560c7 100644 --- a/src/interface/cron/events/mint.js +++ b/src/interface/cron/events/contractEvents/mint.js @@ -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; @@ -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({}); @@ -94,3 +92,5 @@ function updateMintCheckpoint(newCheckpoint) { { upsert: true } ); } + +module.exports = mintHandler; \ No newline at end of file diff --git a/src/interface/cron/events/process.js b/src/interface/cron/events/contractEvents/process.js similarity index 79% rename from src/interface/cron/events/process.js rename to src/interface/cron/events/contractEvents/process.js index 8c385b9..df6b6c4 100644 --- a/src/interface/cron/events/process.js +++ b/src/interface/cron/events/contractEvents/process.js @@ -1,12 +1,12 @@ -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); @@ -14,12 +14,13 @@ agenda.define(jobs.PROCESS, async (job, done) => { 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({}); @@ -62,3 +63,5 @@ async function processStoredEvents(events) { const data = await Promise.all(allPromises); return data; } + +module.exports = process; \ No newline at end of file diff --git a/src/interface/cron/events/processEvent.js b/src/interface/cron/events/contractEvents/processEvent.js similarity index 97% rename from src/interface/cron/events/processEvent.js rename to src/interface/cron/events/contractEvents/processEvent.js index fb8ff90..ce18abc 100644 --- a/src/interface/cron/events/processEvent.js +++ b/src/interface/cron/events/contractEvents/processEvent.js @@ -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"); @@ -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) { diff --git a/src/interface/cron/events/registeredCollaboratorKey.js b/src/interface/cron/events/contractEvents/registeredCollaboratorKey.js similarity index 82% rename from src/interface/cron/events/registeredCollaboratorKey.js rename to src/interface/cron/events/contractEvents/registeredCollaboratorKey.js index ccc1794..1b715df 100644 --- a/src/interface/cron/events/registeredCollaboratorKey.js +++ b/src/interface/cron/events/contractEvents/registeredCollaboratorKey.js @@ -1,21 +1,20 @@ -const config = require("../../../../config"); -const constants = require("../../../constants"); -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 config = require("../../../../../config"); +const constants = require("../../../../constants"); +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 = "registeredCollaboratorKeys"; const BATCH_SIZE = constants.CRON.BATCH_SIZE; -agenda.define(jobs.REGISTERED_COLLABORATOR_KEY, async (job, done) => { - let registeredCollaboratorKey = []; +async function registeredCollaboratorKeyHandler() { try { const registeredCollaboratorKeyCheckpoint = await fetchRegisteredCollaboratorKeyCheckpoint(); const batchSize = BATCH_SIZE; - registeredCollaboratorKey = + const registeredCollaboratorKey = await fetchRegisteredCollaboratorKeyEvents( registeredCollaboratorKeyCheckpoint, batchSize, @@ -30,14 +29,14 @@ agenda.define(jobs.REGISTERED_COLLABORATOR_KEY, async (job, done) => { if (lastEventCheckpont) { await updateRegisteredCollaboratorKeyCheckpoint(lastEventCheckpont); } - done(); } catch (err) { + await Reporter().alert(jobs.REGISTERED_COLLABORATOR_KEY + "::" + err.message, err.stack); console.error("Error in job", jobs.REGISTERED_COLLABORATOR_KEY, err.message); - done(err); } finally { console.log("Job done", jobs.REGISTERED_COLLABORATOR_KEY); } -}); + +} async function fetchRegisteredCollaboratorKeyCheckpoint() { const eventProcessed = await EventProcessor.findOne({}); @@ -107,3 +106,5 @@ function updateRegisteredCollaboratorKeyCheckpoint(newCheckpoint) { { upsert: true } ); } + +module.exports = registeredCollaboratorKeyHandler; \ No newline at end of file diff --git a/src/interface/cron/events/removedCollaborator.js b/src/interface/cron/events/contractEvents/removedCollaborator.js similarity index 80% rename from src/interface/cron/events/removedCollaborator.js rename to src/interface/cron/events/contractEvents/removedCollaborator.js index 4521021..c476423 100644 --- a/src/interface/cron/events/removedCollaborator.js +++ b/src/interface/cron/events/contractEvents/removedCollaborator.js @@ -1,22 +1,22 @@ -const config = require("../../../../config"); -const constants = require("../../../constants"); -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 config = require("../../../../../config"); +const constants = require("../../../../constants"); +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 = "removedCollaborators"; const BATCH_SIZE = constants.CRON.BATCH_SIZE; -agenda.define(jobs.REMOVED_COLLABORATOR, async (job, done) => { - let removedCollaborators = []; + +async function removedCollaboratorHandler() { try { const removedCollaboratorCheckpoint = await fetchRemovedCollaboratorCheckpoint(); const batchSize = BATCH_SIZE; - removedCollaborators = await fetchRemovedCollaboratorEvents( + const removedCollaborators = await fetchRemovedCollaboratorEvents( removedCollaboratorCheckpoint, batchSize, ); @@ -30,14 +30,13 @@ agenda.define(jobs.REMOVED_COLLABORATOR, async (job, done) => { if (lastEventCheckpont) { await updateRemovedCollaboratorCheckpoint(lastEventCheckpont); } - done(); } catch (err) { + await Reporter().alert(jobs.REMOVED_COLLABORATOR + "::" + err.message, err.stack); console.error("Error in job", jobs.REMOVED_COLLABORATOR, err.message); - done(err); } finally { console.log("Job done", jobs.REMOVED_COLLABORATOR); } -}); +} async function fetchRemovedCollaboratorCheckpoint() { const eventProcessed = await EventProcessor.findOne({}); @@ -102,3 +101,5 @@ function updateRemovedCollaboratorCheckpoint(newCheckpoint) { { upsert: true } ); } + +module.exports = removedCollaboratorHandler; \ No newline at end of file diff --git a/src/interface/cron/events/updatedPortalMetadata.js b/src/interface/cron/events/contractEvents/updatedPortalMetadata.js similarity index 81% rename from src/interface/cron/events/updatedPortalMetadata.js rename to src/interface/cron/events/contractEvents/updatedPortalMetadata.js index 4e7caeb..ecf092a 100644 --- a/src/interface/cron/events/updatedPortalMetadata.js +++ b/src/interface/cron/events/contractEvents/updatedPortalMetadata.js @@ -1,9 +1,9 @@ -const config = require("../../../../config"); -const constants = require("../../../constants"); -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 config = require("../../../../../config"); +const constants = require("../../../../constants"); +const { EventProcessor, Event } = require("../../../../infra/database/models"); +const EventUtil = require("../utils"); +const jobs = require("../../jobs"); const axios = require("axios"); const processEvent = require('./processEvent'); @@ -11,13 +11,12 @@ const API_URL = config.SUBGRAPH_API; const EVENT_NAME = "updatedPortalDatas"; const BATCH_SIZE = constants.CRON.BATCH_SIZE; -agenda.define(jobs.UPDATED_PORTAL_METADATA, async (job, done) => { - let updatedPortalMetadatas = []; +async function updatePortalMetadataHandler() { try { const updatedPortalMetadataCheckpoint = await fetchUpdatedPortalMetadataCheckpoint(); const batchSize = BATCH_SIZE; - updatedPortalMetadatas = await fetchUpdatedPortalMetadataEvents( + const updatedPortalMetadatas = await fetchUpdatedPortalMetadataEvents( updatedPortalMetadataCheckpoint, batchSize, ); @@ -31,14 +30,13 @@ agenda.define(jobs.UPDATED_PORTAL_METADATA, async (job, done) => { if (lastEventCheckpont) { await updateUpdatedPortalMetadataCheckpoint(lastEventCheckpont); } - done(); } catch (err) { + await Reporter().alert(jobs.UPDATED_PORTAL_METADATA + "::" + err.message, err.stack); console.error("Error in job", jobs.UPDATED_PORTAL_METADATA, err.message); - done(err); } finally { console.log("Job done", jobs.UPDATED_PORTAL_METADATA); } -}); +} async function fetchUpdatedPortalMetadataCheckpoint() { const eventProcessed = await EventProcessor.findOne({}); @@ -106,3 +104,5 @@ function updateUpdatedPortalMetadataCheckpoint(newCheckpoint) { { upsert: true } ); } + +module.exports = updatePortalMetadataHandler; \ No newline at end of file diff --git a/src/interface/cron/events/events.js b/src/interface/cron/events/events.js new file mode 100644 index 0000000..c7abc13 --- /dev/null +++ b/src/interface/cron/events/events.js @@ -0,0 +1,29 @@ +const jobs = require("../jobs"); +const agenda = require("../index"); +const mintHandler = require('./contractEvents/mint'); +const addedFileHandler = require('./contractEvents/addedFiles'); +const editedFilesHandler = require('./contractEvents/editedFiles'); +const addedCollaboratorHandler = require('./contractEvents/addedCollaborator'); +const removedCollaboratorHandler = require('./contractEvents/removedCollaborator'); +const updatedPortalMetadataHandler = require('./contractEvents/updatedPortalMetadata'); +const registeredCollaboratorKeyHandler = require('./contractEvents/registeredCollaboratorKey'); + +agenda.define(jobs.CONTRACT_EVENTS, async (job, done) => { + console.log("Running job", jobs.CONTRACT_EVENTS); + try { + await mintHandler(); + await addedFileHandler(); + await editedFilesHandler(); + await addedCollaboratorHandler(); + await removedCollaboratorHandler(); + await updatedPortalMetadataHandler(); + await registeredCollaboratorKeyHandler(); + done(); + } catch (err) { + await Reporter().alert(jobs.CONTRACT_EVENTS + "::" + err.message, err.stack); + console.error("Error in job", jobs.CONTRACT_EVENTS, err.message); + done(err); + } finally { + console.log("Job done", jobs.CONTRACT_EVENTS); + } +}); \ No newline at end of file diff --git a/src/interface/cron/events/publicPortalIndex.js b/src/interface/cron/events/publicPortalIndex.js index a049486..776cb84 100644 --- a/src/interface/cron/events/publicPortalIndex.js +++ b/src/interface/cron/events/publicPortalIndex.js @@ -1,6 +1,8 @@ +const Reporter = require('../../../domain/reporter'); const config = require("../../../../config"); const agenda = require("../index"); const jobs = require("../jobs"); +const axios = require("axios"); const xApiKey = config.STORAGE_SECRET_KEY ? config.STORAGE_SECRET_KEY : ""; const storageEndpoint = config.STORAGE_ENDPOINT @@ -20,17 +22,13 @@ agenda.define(jobs.PORTAL_INDEX, async (job, done) => { }; try { - fetch(url, requestOptions) - .then((response) => response.text()) - .then((result) => { - console.log(result); - }) - .catch((error) => { - console.error(error); - }); - + let response = await fetch(url, requestOptions); + if (response.status !== 200) { + throw new Error("Failed to trigger portal index"); + } done(); } catch (err) { + await Reporter().alert(jobs.PORTAL_INDEX + "::" + err.message, err.stack); console.error("Error in job", jobs.PORTAL_INDEX, err.message); done(err); } finally { diff --git a/src/interface/cron/jobs.js b/src/interface/cron/jobs.js index 1c5d31f..9f1476e 100644 --- a/src/interface/cron/jobs.js +++ b/src/interface/cron/jobs.js @@ -8,6 +8,7 @@ const jobs = { MINT: 'MINT', PROCESS: 'PROCESS', PORTAL_INDEX: 'PORTAL_INDEX', + CONTRACT_EVENTS: 'CONTRACT_EVENTS', }; module.exports = jobs;