-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Improvements for Job#retry #318
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -443,6 +443,64 @@ var scripts = { | |
limit | ||
]; | ||
|
||
return execScript.apply(scripts, args); | ||
}, | ||
|
||
/** | ||
* Attempts to retry a job | ||
* | ||
* @param {Job} job | ||
* | ||
* @return {Promise<Number>} Returns a promise that evaluates to a return code: | ||
* 1 means the operation was a success | ||
* 0 means the job does not exist | ||
* -1 means the job is currently locked and can't be retried. | ||
* -2 means the job was not found in the `failed` set | ||
*/ | ||
retryJob: function(job) { | ||
var push = (job.opts.lifo ? 'R' : 'L') + 'PUSH'; | ||
|
||
var script = [ | ||
'if (redis.call("EXISTS", KEYS[1]) == 1) then', | ||
' if (redis.call("EXISTS", KEYS[2]) == 0) then', | ||
' if (redis.call("SREM", KEYS[3], ARGV[1]) == 1) then', | ||
' redis.call("' + push + '", KEYS[4], ARGV[1])', | ||
' redis.call("PUBLISH", KEYS[5], ARGV[1])', | ||
' return 1', | ||
' else', | ||
' return -2', | ||
' end', | ||
' else', | ||
' return -1', | ||
' end', | ||
'else', | ||
' return 0', | ||
'end' | ||
].join('\n'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of this nested code I would propose something like this: if ((redis.call("EXISTS", KEYS[1]) ~= 1) then
return 0
end
if ((redis.call("EXISTS", KEYS[2]) ~= 0) then
return -1
end
.
.
.
return 1; |
||
|
||
var queue = job.queue; | ||
|
||
var keys = [ | ||
queue.toKey(job.jobId), | ||
queue.toKey(job.jobId) + ':lock', | ||
queue.toKey('failed'), | ||
queue.toKey('wait'), | ||
queue.toKey('jobs') | ||
]; | ||
|
||
var args = [ | ||
queue.client, | ||
'retryJob', | ||
script, | ||
5, | ||
keys[0], | ||
keys[1], | ||
keys[2], | ||
keys[3], | ||
keys[4], | ||
job.jobId | ||
]; | ||
|
||
return execScript.apply(scripts, args); | ||
} | ||
}; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not allowing retries of completed jobs as well, since this could be reused to implement recurrent jobs in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One consideration on that though. The current implementation works with failed jobs only, so we'll be breaking some expectations on
Job#retry
by letting it to retry completed job as well.One common usage scenario I think is having parallel workers that have some kind of retry logic for failed jobs, something like this:
The above may lead to problems because if another worker retried a job successfully, it might be retried yet again by another worker since that job is also retriable if it's completed.
Perhaps we can allow parameters to
retry
to drive the behavior? or exposerepeat
as a separate Job function instead? Considering that if we "Retry" a complated job, we are not really "Retrying" it, we are "Repeating" it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes a retry with parameters, or the script part with parameters and then reuse that code in the repeat function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
speaking of which. Wouldn't be nice that retry includes a delay parameter as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, I made the script accept an options parameter to determine on which state the job should be when attempting to reprocess it. This should support retrying a job (from a failed state) or repeating a job (from the completed state).
As for the delay, I took a dive into it, but it was taking me too much time, maybe that can be added on a separate PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xdc0 @manast
why "retry for completed" still not implement?