-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Unsubscribe #7358
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
Open
shortlight5980
wants to merge
4
commits into
labring:main
Choose a base branch
from
shortlight5980:unsubscribe
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Unsubscribe #7358
Changes from all commits
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 hidden or 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 hidden or 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 hidden or 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,78 @@ | ||
| import { LeaseCache } from '../caches'; | ||
| import { bullMQ } from './binding'; | ||
| import type { Queue } from './types'; | ||
|
|
||
| const FAILED_JOB_RECOVERY_LEASE_TTL_MS = 30 * 1000; | ||
|
|
||
| /** | ||
| * 添加稳定 ID 任务;若同 ID 历史任务已失败,则在分布式租约内刷新数据并手动重试。 | ||
| * 其它状态继续复用现有任务,避免并发生产者制造重复工作。 | ||
| */ | ||
| export async function addOrRequeueFailedJob<DataType, ReturnType = void>({ | ||
| queue, | ||
| name, | ||
| data, | ||
| opts | ||
| }: { | ||
| queue: Queue<DataType, ReturnType>; | ||
| name: Parameters<Queue<DataType, ReturnType>['add']>[0]; | ||
| data: Parameters<Queue<DataType, ReturnType>['add']>[1]; | ||
| opts: NonNullable<Parameters<Queue<DataType, ReturnType>['add']>[2]> & { jobId: string }; | ||
| }) { | ||
| /** unknown 可能来自 retention cleanup;二次读取确认不存在后才允许重建。 */ | ||
| const getJobWithConfirmedState = async () => { | ||
| const job = await queue.getJob(opts.jobId); | ||
| if (!job) return; | ||
|
|
||
| const state = await job.getState(); | ||
| if (state !== 'unknown') return { job, state }; | ||
|
|
||
| const latestJob = await queue.getJob(opts.jobId); | ||
| if (!latestJob) return; | ||
|
|
||
| const latestState = await latestJob.getState(); | ||
| if (latestState === 'unknown') { | ||
| throw new Error(`BullMQ job is in an unknown state: ${queue.name}/${opts.jobId}`); | ||
| } | ||
| return { job: latestJob, state: latestState }; | ||
| }; | ||
|
|
||
| const existing = await getJobWithConfirmedState(); | ||
| if (existing) { | ||
| if (existing.state !== 'failed') return existing.job; | ||
|
|
||
| const leaseCache = new LeaseCache({ logger: bullMQ.getLogger() }); | ||
| return leaseCache.withLease({ | ||
| key: `bullmq:failed-job-recovery:${queue.name}:${opts.jobId}`, | ||
| label: 'bullmq-failed-job-recovery', | ||
| ttlMs: FAILED_JOB_RECOVERY_LEASE_TTL_MS, | ||
| fn: async () => { | ||
| const current = await getJobWithConfirmedState(); | ||
| if (!current) return queue.add(name, data, opts); | ||
| if (current.state !== 'failed') return current.job; | ||
| const currentJob = current.job; | ||
|
|
||
| try { | ||
| await currentJob.updateData(data as DataType); | ||
| } catch (error) { | ||
| const latest = await getJobWithConfirmedState(); | ||
| if (!latest) return queue.add(name, data, opts); | ||
| if (latest.state !== 'failed') return latest.job; | ||
| throw error; | ||
| } | ||
|
|
||
| try { | ||
| await currentJob.retry('failed'); | ||
| return currentJob; | ||
| } catch (error) { | ||
| const latest = await getJobWithConfirmedState(); | ||
| if (!latest) return queue.add(name, data, opts); | ||
| if (latest.state !== 'failed') return latest.job; | ||
| throw error; | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| return queue.add(name, data, opts); | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.