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

Feature: Improve Agenda Queue Performance #2545

Merged
merged 1 commit into from
Oct 27, 2024
Merged
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
8 changes: 8 additions & 0 deletions config/agendaQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ module.exports.agendaQueue = {
{
name: 'RaidMintRetryJob',
fnName: 'raidservice.mintRetryJob'
},
{
name: 'MoveCompletedJobsToHistory',
fnName: 'agendaqueueservice.moveCompletedJobsToHistory',
schedule: {
method: 'every',
intervalOrSchedule: '5 minutes'
}
}
]
};
23 changes: 21 additions & 2 deletions typescript/api/services/AgendaQueueService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export module Services {
'now',
'jobs',
'sampleFunctionToDemonstrateHowToDefineAJobFunction',
'defineJob'
'defineJob',
'moveCompletedJobsToHistory'
];

protected agenda: Agenda;
Expand Down Expand Up @@ -128,7 +129,7 @@ export module Services {
if (method == 'now') {
this.now(job.name, data)
} else if (method == 'every') {
this.every(job.name, intervalOrSchedule, data, opts);
this.every(job.name, intervalOrSchedule, data, opts);
} else if (method == 'schedule') {
this.schedule(job.name, intervalOrSchedule, data);
} else {
Expand Down Expand Up @@ -185,6 +186,24 @@ export module Services {

}

/**
*
* There are significant slowdowns with Agenda when the jobs collection grows large. This function moves all completed jobs to a history collection so we still have the data but it doesn't affect the peformance of the job processor.
*
* @param job
*/
public async moveCompletedJobsToHistory(job:any) {
const dbManager = User.getDatastore().manager;
const collectionName = _.get(sails.config.agendaQueue, 'collection', 'agendaJobs');
await dbManager.collection(collectionName).find({ nextRunAt: null }).forEach(async (doc) => {
await dbManager.collection(`${collectionName}History`).insertOne(doc);
await dbManager.collection(collectionName).deleteOne({ _id: doc._id });
});
Comment on lines +198 to +201
Copy link
Contributor

@cofiem cofiem Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this load all the documents only to write them back?

I don't know how many documents there will be, so this might not matter, but is there a way to do this entirely on the mongodb side?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it does. I did look at using a MongoDB aggregation pipeline but I had concerns about concurrency. It should only be a few dozen records at a time at most. Will backlog a ticket to make it more efficient if we find it isn't enough.


sails.log.verbose(`moveCompletedJobsToHistory:: Moved completed jobs to history`);
}


private setOptionIfDefined(agendaOpts, optionName, optionVal) {
if (!_.isEmpty(optionVal)) {
_.set(agendaOpts, optionName, optionVal);
Expand Down