-
Notifications
You must be signed in to change notification settings - Fork 33
implement local event handling #79
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
Closed
imcaizheng
wants to merge
7
commits into
topcoder-platform:dev
from
imcaizheng:implement-event-handlers
Closed
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4d5a0b2
implement local event handling
imcaizheng d96200f
`cancelled` jobcandidate instead of `reject`
imcaizheng 98105a2
update swagger - add `cancelled` status value to JobCandidate
imcaizheng f7f1808
pull data from db instead of directly extract data from event payload
imcaizheng 40b1f93
revert the change made to app-routes.js
imcaizheng fdf0571
Merge branch 'dev' into implement-event-handlers
imcaizheng 4adca39
Revert "Merge branch 'dev' into implement-event-handlers"
imcaizheng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Implement an event dispatcher that handles events synchronously. | ||
*/ | ||
|
||
const handlers = [] | ||
|
||
/** | ||
* Handle event. | ||
* | ||
* @param {String} topic the topic name | ||
* @param {Object} payload the message payload | ||
* @returns {undefined} | ||
*/ | ||
async function handleEvent (topic, payload) { | ||
for (const handler of handlers) { | ||
await handler.handleEvent(topic, payload) | ||
} | ||
} | ||
|
||
/** | ||
* Register to the dispatcher. | ||
* | ||
* @param {Object} handler the handler containing the `handleEvent` function | ||
* @returns {undefined} | ||
*/ | ||
function register (handler) { | ||
handlers.push(handler) | ||
} | ||
|
||
module.exports = { | ||
handleEvent, | ||
register | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Handle events for Job. | ||
*/ | ||
|
||
const { Op } = require('sequelize') | ||
const models = require('../models') | ||
const logger = require('../common/logger') | ||
const helper = require('../common/helper') | ||
const JobCandidateService = require('../services/JobCandidateService') | ||
const ResourceBookingService = require('../services/ResourceBookingService') | ||
|
||
/** | ||
* Cancel all related resource bookings and all related candidates when a job is cancelled. | ||
* | ||
* @param {Object} payload the event payload | ||
* @returns {undefined} | ||
*/ | ||
async function cancelJob (payload) { | ||
if (payload.status !== 'cancelled') { | ||
logger.info({ | ||
component: 'JobEventHandler', | ||
context: 'cancelJob', | ||
message: `not interested job - status: ${payload.status}` | ||
}) | ||
return | ||
} | ||
const candidates = await models.JobCandidate.findAll({ | ||
where: { | ||
jobId: payload.id, | ||
status: { | ||
[Op.not]: 'cancelled' | ||
}, | ||
deletedAt: null | ||
} | ||
}) | ||
const resourceBookings = await models.ResourceBooking.findAll({ | ||
where: { | ||
projectId: payload.projectId, | ||
status: { | ||
[Op.not]: 'cancelled' | ||
}, | ||
deletedAt: null | ||
} | ||
}) | ||
await Promise.all([ | ||
...candidates.map(candidate => JobCandidateService.partiallyUpdateJobCandidate( | ||
helper.authUserAsM2M(), | ||
candidate.id, | ||
{ status: 'cancelled' } | ||
).then(result => { | ||
logger.info({ | ||
component: 'JobEventHandler', | ||
context: 'cancelJob', | ||
message: `id: ${result.id} candidate got cancelled.` | ||
}) | ||
})), | ||
...resourceBookings.map(resource => ResourceBookingService.partiallyUpdateResourceBooking( | ||
helper.authUserAsM2M(), | ||
resource.id, | ||
{ status: 'cancelled' } | ||
).then(result => { | ||
logger.info({ | ||
component: 'JobEventHandler', | ||
context: 'cancelJob', | ||
message: `id: ${result.id} resource booking got cancelled.` | ||
}) | ||
})) | ||
]) | ||
} | ||
|
||
/** | ||
* Process job update event. | ||
* | ||
* @param {Object} payload the event payload | ||
* @returns {undefined} | ||
*/ | ||
async function processUpdate (payload) { | ||
await cancelJob(payload) | ||
} | ||
|
||
module.exports = { | ||
processUpdate | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Handle events for ResourceBooking. | ||
*/ | ||
|
||
const { Op } = require('sequelize') | ||
const models = require('../models') | ||
const logger = require('../common/logger') | ||
const helper = require('../common/helper') | ||
const JobService = require('../services/JobService') | ||
const JobCandidateService = require('../services/JobCandidateService') | ||
|
||
/** | ||
* When ResourceBooking's status is changed to `assigned` | ||
* the corresponding JobCandidate record (with the same userId and jobId) | ||
* should be updated with status `selected` | ||
* | ||
* @param {Object} payload the event payload | ||
* @returns {undefined} | ||
*/ | ||
async function selectJobCandidate (payload) { | ||
if (payload.status !== 'assigned') { | ||
logger.info({ | ||
component: 'ResourceBookingEventHandler', | ||
context: 'selectJobCandidate', | ||
message: `not interested resource booking - status: ${payload.status}` | ||
}) | ||
return | ||
} | ||
const candidates = await models.JobCandidate.findAll({ | ||
where: { | ||
jobId: payload.jobId, | ||
userId: payload.userId, | ||
status: { | ||
[Op.not]: 'selected' | ||
}, | ||
deletedAt: null | ||
} | ||
}) | ||
await Promise.all(candidates.map(candidate => JobCandidateService.partiallyUpdateJobCandidate( | ||
helper.authUserAsM2M(), | ||
candidate.id, | ||
{ status: 'selected' } | ||
).then(result => { | ||
logger.info({ | ||
component: 'ResourceBookingEventHandler', | ||
context: 'selectJobCandidate', | ||
message: `id: ${result.id} candidate got selected.` | ||
}) | ||
}))) | ||
} | ||
|
||
/** | ||
* Update the status of the Job to assigned when it positions requirement is fullfilled. | ||
* | ||
* @param {Object} payload the event payload | ||
* @returns {undefined} | ||
*/ | ||
async function assignJob (payload) { | ||
if (payload.status !== 'assigned') { | ||
logger.info({ | ||
component: 'ResourceBookingEventHandler', | ||
context: 'assignJob', | ||
message: `not interested resource booking - status: ${payload.status}` | ||
}) | ||
return | ||
} | ||
const job = await models.Job.findOne({ | ||
where: { | ||
projectId: payload.projectId, | ||
deletedAt: null | ||
} | ||
}) | ||
if (job.status === 'assigned') { | ||
logger.info({ | ||
component: 'ResourceBookingEventHandler', | ||
context: 'assignJob', | ||
message: `job with projectId ${payload.projectId} is already assigned` | ||
}) | ||
return | ||
} | ||
const resourceBookings = await models.ResourceBooking.findAll({ | ||
where: { | ||
status: 'assigned', | ||
deletedAt: null | ||
} | ||
}) | ||
logger.debug({ | ||
component: 'ResourceBookingEventHandler', | ||
context: 'assignJob', | ||
message: `the number of assigned resource bookings is ${resourceBookings.length} - the numPositions of the job is ${job.numPositions}` | ||
}) | ||
if (job.numPositions === resourceBookings.length) { | ||
await JobService.partiallyUpdateJob(helper.authUserAsM2M(), job.id, { status: 'assigned' }) | ||
logger.info({ component: 'ResourceBookingEventHandler', context: 'assignJob', message: `job with projectId ${payload.projectId} is assigned` }) | ||
} | ||
} | ||
|
||
/** | ||
* Process resource booking update event. | ||
* | ||
* @param {Object} payload the event payload | ||
* @returns {undefined} | ||
*/ | ||
async function processUpdate (payload) { | ||
await selectJobCandidate(payload) | ||
await assignJob(payload) | ||
} | ||
|
||
module.exports = { | ||
processUpdate | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* The entry of event handlers. | ||
*/ | ||
|
||
const config = require('config') | ||
const eventDispatcher = require('../common/eventDispatcher') | ||
const JobEventHandler = require('./JobEventHandler') | ||
const ResourceBookingEventHandler = require('./ResourceBookingEventHandler') | ||
const logger = require('../common/logger') | ||
|
||
const TopicOperationMapping = { | ||
[config.TAAS_JOB_UPDATE_TOPIC]: JobEventHandler.processUpdate, | ||
[config.TAAS_RESOURCE_BOOKING_UPDATE_TOPIC]: ResourceBookingEventHandler.processUpdate | ||
} | ||
|
||
/** | ||
* Handle event. | ||
* | ||
* @param {String} topic the topic name | ||
* @param {Object} payload the message payload | ||
* @returns {undefined} | ||
*/ | ||
async function handleEvent (topic, payload) { | ||
if (!TopicOperationMapping[topic]) { | ||
logger.info({ component: 'eventHanders', context: 'handleEvent', message: `not interested event - topic: ${topic}` }) | ||
return | ||
} | ||
logger.debug({ component: 'eventHanders', context: 'handleEvent', message: `handling event - topic: ${topic} - payload: ${JSON.stringify(payload)}` }) | ||
try { | ||
await TopicOperationMapping[topic](payload) | ||
} catch (err) { | ||
logger.error({ component: 'eventHanders', context: 'handleEvent', message: 'failed to handle event' }) | ||
// throw error so that it can be handled by the app | ||
throw err | ||
} | ||
logger.info({ component: 'eventHanders', context: 'handleEvent', message: 'event successfully handled' }) | ||
} | ||
|
||
/** | ||
* Attach the handlers to the event dispatcher. | ||
* | ||
* @returns {undefined} | ||
*/ | ||
function init () { | ||
eventDispatcher.register({ | ||
handleEvent | ||
}) | ||
} | ||
|
||
module.exports = { | ||
init | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.