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

feat(job): add exclusive execution option #2465

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions docs/gitbook/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* [Stalled Jobs](guide/workers/stalled-jobs.md)
* [Sandboxed processors](guide/workers/sandboxed-processors.md)
* [Pausing queues](guide/workers/pausing-queues.md)
* [Preserve Order](guide/workers/preserve-order.md)
* [Jobs](guide/jobs/README.md)
* [FIFO](guide/jobs/fifo.md)
* [LIFO](guide/jobs/lifo.md)
Expand Down
16 changes: 16 additions & 0 deletions docs/gitbook/guide/workers/preserve-order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Preserve Order

BullMQ supports preserving execution order in jobs. When _preserveOrder_ option is provided as *true*, jobs will be processed in the same order, indendently of retry strategies. If the current job fails and has a retry strategy, queue will be in rate limit state until the delay is accomplish.

```typescript
const worker = new Worker('queueName', async (job: Job) => {
// do some work
}, {
concurrency: 1
Copy link
Contributor

Choose a reason for hiding this comment

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

As concurrency is by default 1, I assume it is not required to specify any when using "preserveOrder".

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes, no necessary, I added it just to be clear, I can add a comment about the default value

preserveOrder: true
});
```

{% hint style="warning" %}
This feature is only allowed when using concurrency 1, any greater value will throw an error:
Copy link
Contributor

Choose a reason for hiding this comment

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

We should add another hint, when using retries and backoffs, for instance, a failed job will keep the queue idle during the time the job is being backed off until it is picked again.

{% endhint %}
7 changes: 5 additions & 2 deletions python/bullmq/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ async def moveToFailed(self, err, token:str, fetchNext:bool = False):
)
if delay == -1:
move_to_failed = True
elif delay:
elif delay and not self.queue.opts.get("preserveOrder", False):
keys, args = self.scripts.moveToDelayedArgs(
self.id,
round(time.time() * 1000) + delay,
Expand All @@ -160,7 +160,10 @@ async def moveToFailed(self, err, token:str, fetchNext:bool = False):
await self.scripts.commands["moveToDelayed"](keys=keys, args=args, client=pipe)
command = 'delayed'
else:
keys, args = self.scripts.retryJobArgs(self.id, self.opts.get("lifo", False), token)
keys, args = self.scripts.retryJobArgs(self.id, self.opts.get("lifo", False), token, {
"preserveOrder": self.queue.opts.get("preserveOrder", False),
"pttl": delay
})

await self.scripts.commands["retryJob"](keys=keys, args=args, client=pipe)
command = 'retryJob'
Expand Down
7 changes: 5 additions & 2 deletions python/bullmq/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(self, prefix: str, queueName: str, redisConnection: RedisConnection
"promote": self.redisClient.register_script(self.getScript("promote-8.lua")),
"removeJob": self.redisClient.register_script(self.getScript("removeJob-1.lua")),
"reprocessJob": self.redisClient.register_script(self.getScript("reprocessJob-7.lua")),
"retryJob": self.redisClient.register_script(self.getScript("retryJob-10.lua")),
"retryJob": self.redisClient.register_script(self.getScript("retryJob-11.lua")),
"moveJobsToWait": self.redisClient.register_script(self.getScript("moveJobsToWait-7.lua")),
"saveStacktrace": self.redisClient.register_script(self.getScript("saveStacktrace-1.lua")),
"updateData": self.redisClient.register_script(self.getScript("updateData-1.lua")),
Expand Down Expand Up @@ -250,12 +250,15 @@ def retryJobArgs(self, job_id: str, lifo: bool, token: str, opts: dict = {}):
keys.append(self.keys['delayed'])
keys.append(self.keys['prioritized'])
keys.append(self.keys['pc'])
keys.append(self.keys['limiter'])
keys.append(self.keys['marker'])

push_cmd = "RPUSH" if lifo else "LPUSH"

pttl = opts.get("pttl", 0)
args = [self.keys[''], round(time.time() * 1000), push_cmd,
job_id, token, "1" if opts.get("skipAttempt") else "0"]
job_id, token, "1" if opts.get("preserveOrder") else "0",
pttl if pttl > 0 else 0]

return (keys, args)

Expand Down
7 changes: 5 additions & 2 deletions src/classes/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ export class Job<

if (delay === -1) {
moveToFailed = true;
} else if (delay) {
} else if (delay && !opts.preserveOrder) {
const args = this.scripts.moveToDelayedArgs(
this.id,
Date.now() + delay,
Expand All @@ -695,7 +695,10 @@ export class Job<
} else {
// Retry immediately
(<any>multi).retryJob(
this.scripts.retryJobArgs(this.id, this.opts.lifo, token),
this.scripts.retryJobArgs(this.id, this.opts.lifo, token, {
preserveOrder: opts.preserveOrder,
pttl: delay,
}),
);
command = 'retryJob';
}
Expand Down
5 changes: 5 additions & 0 deletions src/classes/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
WorkerOptions,
KeepJobs,
MoveToDelayedOpts,
RetryOpts,
} from '../interfaces';
import {
JobState,
Expand Down Expand Up @@ -907,6 +908,7 @@ export class Scripts {
jobId: string,
lifo: boolean,
token: string,
opts: RetryOpts,
): (string | number)[] {
const keys: (string | number)[] = [
this.queue.keys.active,
Expand All @@ -918,6 +920,7 @@ export class Scripts {
this.queue.keys.delayed,
this.queue.keys.prioritized,
this.queue.keys.pc,
this.queue.keys.limiter,
this.queue.keys.marker,
];

Expand All @@ -929,6 +932,8 @@ export class Scripts {
pushCmd,
jobId,
token,
opts.preserveOrder ? '1' : '0',
opts.pttl && opts.pttl > 0 ? opts.pttl : 0,
]);
}

Expand Down
10 changes: 10 additions & 0 deletions src/classes/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,18 @@ export class Worker<
stalledInterval: 30000,
autorun: true,
runRetryDelay: 15000,
preserveOrder: false,
...this.opts,
};

if (this.opts.stalledInterval <= 0) {
throw new Error('stalledInterval must be greater than 0');
}

if (this.opts.preserveOrder && this.opts.concurrency > 1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice to refactor this check as we are doing exactly the same on "set concurrency". In fact, couldn't we just do this.concurrency = ... here and let the checks from set concurrency take care of it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Another thing, shouldn't we have this check on the python version as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was think on adding it later

throw new Error('concurrency must be 1 when preserveOrder is enabled');
}

if (this.opts.drainDelay <= 0) {
throw new Error('drainDelay must be greater than 0');
}
Expand Down Expand Up @@ -385,6 +390,11 @@ export class Worker<
) {
throw new Error('concurrency must be a finite number greater than 0');
}

if (this.opts.preserveOrder && concurrency > 1) {
throw new Error('concurrency must be 1 when preserveOrder is enabled');
}

this.opts.concurrency = concurrency;
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/moveToDelayed-7.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ if rcall("EXISTS", jobKey) == 1 then
if ARGV[7] == "0" then
rcall("HINCRBY", jobKey, "atm", 1)
end

rcall("HSET", jobKey, "delay", ARGV[6])

local maxEvents = getOrSetMaxEvents(metaKey)
Expand Down
16 changes: 13 additions & 3 deletions src/commands/retryJob-10.lua → src/commands/retryJob-11.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
KEYS[7] delayed key
KEYS[8] prioritized key
KEYS[9] 'pc' priority counter
KEYS[10] 'marker'
KEYS[10] limiter key
KEYS[11] 'marker'

ARGV[1] key prefix
ARGV[2] timestamp
ARGV[3] pushCmd
ARGV[3] pushCmd - lifo -> RPUSH - fifo -> LPUSH
ARGV[4] jobId
ARGV[5] token
ARGV[6] preserve order
ARGV[7] rate limit pttl

Events:
'waiting'
Expand All @@ -36,7 +39,7 @@ local rcall = redis.call
--- @include "includes/promoteDelayedJobs"

local target, paused = getTargetQueueList(KEYS[5], KEYS[2], KEYS[3])
local markerKey = KEYS[10]
local markerKey = KEYS[11]

-- Check if there are delayed jobs that we can move to wait.
-- test example: when there are delayed jobs between retries
Expand Down Expand Up @@ -67,6 +70,13 @@ if rcall("EXISTS", KEYS[4]) == 1 then

rcall("HINCRBY", KEYS[4], "atm", 1)

if ARGV[6] == "1" then
local pttl = tonumber(ARGV[7])
if pttl > 0 then
rcall("SET", KEYS[10], 999999, "PX", pttl)
end
end

local maxEvents = getOrSetMaxEvents(KEYS[5])

-- Emit waiting event
Expand Down
5 changes: 5 additions & 0 deletions src/interfaces/minimal-job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export interface MoveToDelayedOpts {
skipAttempt?: boolean;
}

export interface RetryOpts {
preserveOrder?: boolean;
pttl?: number;
}

export interface MoveToWaitingChildrenOpts {
child?: {
id: string;
Expand Down
6 changes: 6 additions & 0 deletions src/interfaces/worker-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ export interface WorkerOptions extends QueueBaseOptions {
*/
maxStalledCount?: number;

/**
* If true, it will rate limit the queue when moving this job into delayed.
* Will stop rate limiting the queue until this job is moved to completed or failed.
*/
preserveOrder?: boolean;

/**
* Number of milliseconds between stallness checks.
*
Expand Down
6 changes: 6 additions & 0 deletions src/types/job-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ export type JobsOptions = BaseJobOptions & {
* These fields are the ones stored in Redis with smaller keys for compactness.
*/
export type RedisJobOptions = BaseJobOptions & {
/**
* If true, it will rate limit the queue when moving this job into delayed.
* Will stop rate limiting the queue until this job is moved to completed or failed.
*/
ee?: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

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

wouldn't this be better as "po" from Preserver Order?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

that option comes from the old version of this pr, I'm going to delete it


/**
* If true, moves parent to failed.
*/
Expand Down
53 changes: 53 additions & 0 deletions tests/test_delay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,59 @@ describe('Delayed jobs', function () {
await worker.close();
});

describe('when preserveOrder is provided', function () {
it('should process delayed jobs waiting to be finished in correct order ', async function () {
this.timeout(4000);
const numJobs = 12;

const worker = new Worker(queueName, async (job: Job) => {}, {
autorun: false,
preserveOrder: true,
connection,
prefix,
});

worker.on('failed', function (job, err) {});

const orderList: number[] = [];
let count = 0;
const completed = new Promise<void>((resolve, reject) => {
worker.on('completed', async function (job) {
try {
count++;
orderList.push(job.data.order as number);
if (count == numJobs) {
resolve();
}
} catch (err) {
reject(err);
}
});
});

const jobs = Array.from(Array(numJobs).keys()).map(index => ({
name: 'test',
data: { order: numJobs - index },
opts: {
delay: (numJobs - index) * 150,
attempts: 1,
backoff: { type: 'fixed', delay: 200 },
},
}));
const expectedOrder = Array.from(Array(numJobs).keys()).map(
index => index + 1,
);

await queue.addBulk(jobs);
worker.run();
await completed;

expect(orderList).to.eql(expectedOrder);

await worker.close();
});
});

it('should process delayed jobs with several workers respecting delay', async function () {
this.timeout(30000);
let count = 0;
Expand Down
15 changes: 15 additions & 0 deletions tests/test_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1927,6 +1927,21 @@ describe('workers', function () {
).to.throw('drainDelay must be greater than 0');
});

describe('when preserveOrder is enabled', () => {
it('concurrency cannot be greater than 1', function () {
this.timeout(4000);
expect(
() =>
new Worker(queueName, async () => {}, {
connection,
concurrency: 2,
prefix,
preserveOrder: true,
}),
).to.throw('concurrency must be 1 when preserveOrder is enabled');
});
});

it('lock extender continues to run until all active jobs are completed when closing a worker', async function () {
this.timeout(4000);
let worker;
Expand Down
Loading