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

Add top level disable and enable #1109

Merged
merged 8 commits into from
Aug 4, 2021

Conversation

pdfowler
Copy link
Contributor

@pdfowler pdfowler commented Aug 20, 2020

Use Case

We have a job which sends out invites in a staged manner over a period of time: "sendInvites". Once enough people have responded to the invite, we don't want to send out any more invites. If someone later cancels, we want to continue sending out invites in a tiered manner.

Workaround

The same effect can be accomplished by querying any outstanding "sendInvites" jobs, and iterating over them and calling job.disable(), and doing the same thing to enable with job.enable().

Alternatively the this._collection.updateMany based implementation can be patched to a local instance via prototype injection.


Proposed Implementation

Following the example of agenda.cancel() and its relationship with job.remove(), this PR adds the following two methods:

disable(mongodb-native query)

Disables any jobs matching the passed mongodb-native query, preventing any matching jobs from being run by the Job Processor.

const numDisabled = await agenda.disable({name: 'sendInvites'});

Similar to agenda.cancel(), this functionality can be acheived with a combination of agenda.jobs() and job.disable()

enable(mongodb-native query)

Enables any jobs matching the passed mongodb-native query, allowing any matching jobs to be run by the Job Processor.

const numEnabled = await agenda.enable({name: 'sendInvites'});

Similar to agenda.cancel(), this functionality can be acheived with a combination of agenda.jobs() and job.enable()


PR Checks:

  • Add Test Coverage
  • Add documentation to README.md
  • Update HTML documentation

pdfowler and others added 6 commits August 7, 2020 14:59
* Similar to the mapping between `job.remove` and `agenda.cancel`, these methods alter the `disabled` property for all jobs matching the supplied query
* Tests covering the new logic have been added to `/test/agenda.js`
Copy link
Contributor

@koresar koresar left a comment

Choose a reason for hiding this comment

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

Code is good. Some code comments needs rewording though.

const debug = require('debug')('agenda:enable');

/**
* Enables any jobs matching the passed MongoDB query, and removes them from the database.
Copy link
Contributor

Choose a reason for hiding this comment

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

"and removes them from the database" ?
I don't see that happening in the code. :) Perhaps, the comment needs an update?

* @name Agenda#enable
* @function
* @param {Object} query MongoDB query to use when enabling
* @caller client code, Agenda.purge(), Job.remove()
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this comment correct? I don't see that in the PR changes.

@pdfowler
Copy link
Contributor Author

@koresar Thanks for the feedback and callouts on the comments/docs. I've updated my code with the upstream master and migrated my code to Typescript.

Only remaining task is the HTML docs, which I believe are handled as a part of the release process via the docs script. Let me know if there's anything else I can do on this PR

@koresar koresar merged commit 8daa43b into agenda:master Aug 4, 2021
@koresar
Copy link
Contributor

koresar commented Aug 4, 2021

Quality PR! Thanks for that.

@koresar
Copy link
Contributor

koresar commented Aug 5, 2021

Published in v4.2.0.
Thanks for your contribution!

@pdfowler pdfowler deleted the add_top_level_disable_and_enable branch April 26, 2022 20:03
pdfowler added a commit to pdfowler/agenda that referenced this pull request May 24, 2023
* this code has been migrated over from agenda#1109
* migrates old methods from individual files to the top level Agenda Class
* test suite updated with original test suite content from original fork
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants