Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add top level disable and enable (#1109)
* Add top-level `agenda.disable` and `agenda.enable` methods * 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` * Revert local changes * Cleanup test logging * Add documentation for the new `agenda.disable()` and `agenda.enable()` methods * Change sample job title * Fix linting errors * Port the global enable and disable functions over to typescript
- Loading branch information
Showing
5 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,28 @@ | ||
import createDebugger from "debug"; | ||
import { FilterQuery } from "mongodb"; | ||
import { Agenda } from "."; | ||
const debug = createDebugger("agenda:disable"); | ||
|
||
/** | ||
* Disables any jobs matching the passed MongoDB query by setting the `disabled` flag to `true` | ||
* @name Agenda#disable | ||
* @function | ||
* @param query MongoDB query to use when enabling | ||
* @returns {Promise<number>} Resolved with the number of disabled job instances. | ||
*/ | ||
export const disable = async function ( | ||
this: Agenda, | ||
query: FilterQuery<unknown> = {} | ||
): Promise<number> { | ||
debug("attempting to disable all jobs matching query", query); | ||
try { | ||
const { result } = await this._collection.updateMany(query, { | ||
$set: { disabled: true }, | ||
}); | ||
debug("%s jobs disabled", result.n); | ||
return result.n; | ||
} catch (error) { | ||
debug("error trying to mark jobs as `disabled`"); | ||
throw error; | ||
} | ||
}; |
This file contains 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,29 @@ | ||
import createDebugger from "debug"; | ||
import { FilterQuery } from "mongodb"; | ||
import { Agenda } from "."; | ||
const debug = createDebugger("agenda:enable"); | ||
|
||
/** | ||
* Enables any jobs matching the passed MongoDB query by setting the `disabled` flag to `false` | ||
* @name Agenda#enable | ||
* @function | ||
* @param query MongoDB query to use when enabling | ||
* @caller client code, Agenda.purge(), Job.remove() | ||
* @returns {Promise<Number>} A promise that contains the number of removed documents when fulfilled. | ||
*/ | ||
export const enable = async function ( | ||
this: Agenda, | ||
query: FilterQuery<unknown> = {} | ||
): Promise<number> { | ||
debug("attempting to enable all jobs matching query", query); | ||
try { | ||
const { result } = await this._collection.updateMany(query, { | ||
$set: { disabled: false }, | ||
}); | ||
debug("%s jobs enabled", result.n); | ||
return result.n; | ||
} catch (error) { | ||
debug("error trying to mark jobs as `enabled`"); | ||
throw error; | ||
} | ||
}; |
This file contains 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 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