Skip to content

Commit

Permalink
Update documentation to use plugin classes, add Plugins export (#479)
Browse files Browse the repository at this point in the history
* Add missing export to DelayQueueLock class

* Add Plugins index module

* Export Plugins object

* Use Plugin references when declaring plugins

* Update documentation to use Plugin class names
  • Loading branch information
glensc authored Dec 5, 2020
1 parent 8c17ff4 commit b2882dc
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 32 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ You can read the API docs for Node Resque @ [node-resque.actionherojs.com](https
I learn best by examples:

```javascript
import { Worker, Scheduler, Queue } from "node-resque";
import { Worker, Plugins, Scheduler, Queue } from "node-resque";

async function boot() {
// ////////////////////////
Expand All @@ -49,7 +49,7 @@ async function boot() {

const jobs = {
add: {
plugins: ["JobLock"],
plugins: [Plugins.JobLock],
pluginOptions: {
JobLock: { reEnqueue: true },
},
Expand Down Expand Up @@ -469,7 +469,7 @@ And then your plugin can be invoked within a job like this:
```javascript
const jobs = {
add: {
plugins: ["MyPlugin"],
plugins: [MyPlugin],
pluginOptions: {
MyPlugin: { thing: "stuff" },
},
Expand All @@ -485,7 +485,7 @@ const jobs = {

- You need to return `true` or `false` on the before hooks. `true` indicates that the action should continue, and `false` prevents it. This is called `toRun`.
- If you are writing a plugin to deal with errors which may occur during your resque job, you can inspect and modify `this.worker.error` in your plugin. If `this.worker.error` is null, no error will be logged in the resque error queue.
- There are a few included plugins, all in the lib/plugins/\* directory. You can rewrite you own and include it like this:
- There are a few included plugins, all in the `src/plugins/*` directory. You can write your own and include it like this:

```javascript
const jobs = {
Expand Down
6 changes: 3 additions & 3 deletions __tests__/plugins/delayedQueueLock.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import specHelper from "../utils/specHelper";
import { Plugin, Queue, Worker } from "../../src";
import { Plugin, Queue, Plugins, Worker } from "../../src";

let queue;
const jobDelay = 100;

const jobs = {
slowAdd: {
plugins: ["JobLock"],
plugins: [Plugins.JobLock],
pluginOptions: { jobLock: {} },
perform: async (a, b) => {
const answer = a + b;
Expand All @@ -17,7 +17,7 @@ const jobs = {
},
},
uniqueJob: {
plugins: ["DelayQueueLock"],
plugins: [Plugins.DelayQueueLock],
pluginOptions: { queueLock: {}, delayQueueLock: {} },
perform: async (a, b) => {
const answer = a + b;
Expand Down
8 changes: 4 additions & 4 deletions __tests__/plugins/jobLock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import specHelper from "../utils/specHelper";
import { Queue, Worker } from "../../src";
import { Queue, Plugins, Worker } from "../../src";

let queue;
const jobDelay = 1000;
Expand All @@ -8,7 +8,7 @@ let worker2;

const jobs = {
slowAdd: {
plugins: ["JobLock"],
plugins: [Plugins.JobLock],
pluginOptions: { jobLock: {} },
perform: async (a, b) => {
const answer = a + b;
Expand All @@ -19,7 +19,7 @@ const jobs = {
},
},
withoutReEnqueue: {
plugins: ["JobLock"],
plugins: [Plugins.JobLock],
pluginOptions: { JobLock: { reEnqueue: false } },
perform: async () => {
await new Promise((resolve) => {
Expand Down Expand Up @@ -117,7 +117,7 @@ describe("plugins", () => {

const functionJobs = {
jobLockAdd: {
plugins: ["JobLock"],
plugins: [Plugins.JobLock],
pluginOptions: {
JobLock: {
key: function () {
Expand Down
6 changes: 3 additions & 3 deletions __tests__/plugins/noop.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import specHelper from "../utils/specHelper";
import { Scheduler, Queue, Worker } from "../../src";
import { Scheduler, Plugins, Queue, Worker } from "../../src";

let queue;
let scheduler;
let loggedErrors = [];

const jobs = {
brokenJob: {
plugins: ["Noop"],
plugins: [Plugins.Noop],
pluginOptions: {
Noop: {
logger: (error) => {
Expand All @@ -20,7 +20,7 @@ const jobs = {
},
},
happyJob: {
plugins: ["Noop"],
plugins: [Plugins.Noop],
pluginOptions: {
Noop: {
logger: (error) => {
Expand Down
6 changes: 3 additions & 3 deletions __tests__/plugins/queueLock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import specHelper from "../utils/specHelper";
import { Plugin, Queue, Worker } from "../../src";
import { Plugin, Plugins, Queue, Worker } from "../../src";

let queue;

Expand All @@ -23,12 +23,12 @@ class NeverRunPlugin extends Plugin {

const jobs = {
uniqueJob: {
plugins: ["QueueLock"],
plugins: [Plugins.QueueLock],
pluginOptions: { queueLock: {}, delayQueueLock: {} },
perform: (a, b) => a + b,
},
blockingJob: {
plugins: ["QueueLock", NeverRunPlugin],
plugins: [Plugins.QueueLock, NeverRunPlugin],
perform: (a, b) => a + b,
},
};
Expand Down
10 changes: 5 additions & 5 deletions __tests__/plugins/retry.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import specHelper from "../utils/specHelper";
import { Scheduler, Queue, Worker } from "../../src";
import { Scheduler, Plugins, Queue, Worker } from "../../src";

let queue;
let scheduler;

const jobs = {
brokenJob: {
plugins: ["Retry"],
plugins: [Plugins.Retry],
pluginOptions: {
Retry: {
retryLimit: 3,
Expand All @@ -18,7 +18,7 @@ const jobs = {
},
},
happyJob: {
plugins: ["Retry"],
plugins: [Plugins.Retry],
pluginOptions: {
Retry: {
retryLimit: 3,
Expand Down Expand Up @@ -137,7 +137,7 @@ describe("plugins", () => {
test("can have a retry count set", async (done) => {
const customJobs = {
jobWithRetryCount: {
plugins: ["Retry"],
plugins: [Plugins.Retry],
pluginOptions: {
Retry: {
retryLimit: 5,
Expand Down Expand Up @@ -192,7 +192,7 @@ describe("plugins", () => {
test("can have custom retry times set", async (done) => {
const customJobs = {
jobWithBackoffStrategy: {
plugins: ["Retry"],
plugins: [Plugins.Retry],
pluginOptions: {
Retry: {
retryLimit: 5,
Expand Down
6 changes: 3 additions & 3 deletions examples/example-mock.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env ts-node

import { Queue, Scheduler, Worker } from "../src";
import { Queue, Plugins, Scheduler, Worker } from "../src";
/* In your projects:
import { Queue, Scheduler, Worker } from "node-resque";
import { Queue, Plugins, Scheduler, Worker } from "node-resque";
*/

import * as RedisMock from "ioredis-mock";
Expand All @@ -24,7 +24,7 @@ async function boot() {

const jobs = {
add: {
plugins: ["JobLock"],
plugins: [Plugins.JobLock],
pluginOptions: {
JobLock: {},
},
Expand Down
6 changes: 3 additions & 3 deletions examples/example.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env ts-node

import { Queue, Scheduler, Worker } from "../src";
import { Queue, Plugins, Scheduler, Worker } from "../src";
/* In your projects:
import { Queue, Scheduler, Worker } from "node-resque";
import { Queue, Plugins, Scheduler, Worker } from "node-resque";
*/

async function boot() {
Expand All @@ -29,7 +29,7 @@ async function boot() {

const jobs = {
add: {
plugins: ["JobLock"],
plugins: [Plugins.JobLock],
pluginOptions: {
JobLock: {},
},
Expand Down
6 changes: 3 additions & 3 deletions examples/retry.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env ts-node

import { Queue, Scheduler, Worker } from "../src";
import { Queue, Plugins, Scheduler, Worker } from "../src";
/* In your projects:
import { Queue, Scheduler, Worker } from "node-resque";
import { Queue, Plugins, Scheduler, Worker } from "node-resque";
*/

// ////////////////////////
Expand All @@ -26,7 +26,7 @@ const connectionDetails = {

const jobs = {
add: {
plugins: ["Retry"],
plugins: [Plugins.Retry],
pluginOptions: {
Retry: {
retryLimit: 3,
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export { Scheduler } from "./core/scheduler";
export { Worker } from "./core/worker";
export { MultiWorker } from "./core/multiWorker";
export { Plugin } from "./core/plugin";
export { default as Plugins } from "./plugins";

export { ConnectionOptions } from "./types/options";
export { Job, JobEmit } from "./types/job";
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/DelayQueueLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { Plugin } from "..";

class DelayQueueLock extends Plugin {
export class DelayQueueLock extends Plugin {
async beforeEnqueue() {
const timestamps = await this.queueObject.scheduledAt(
this.queue,
Expand Down
13 changes: 13 additions & 0 deletions src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { DelayQueueLock } from "./DelayQueueLock";
import { JobLock } from "./JobLock";
import { Noop } from "./Noop";
import { QueueLock } from "./QueueLock";
import { Retry } from "./Retry";

export default {
DelayQueueLock,
JobLock,
Noop,
QueueLock,
Retry,
};

0 comments on commit b2882dc

Please sign in to comment.