fix: assign scheduled job worker to JobSchedulerService class #8891
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.
What - Enhance the
JobSchedulerService
by making the scheduler jobs worker available for proper shutdown procedures.Why - Currently, when the Medusa server is shut down, workers are not properly closed, increasing the risk of jobs being marked as stalled. If a worker is terminated without completing its jobs, these jobs are automatically marked as stalled and will be reprocessed when new workers come online, typically after a delay of about 30 seconds. To minimize the occurrence of stalled jobs, it is crucial to gracefully close workers during server shutdown, as recommended by the BullMQ documentation.
How - The worker is currently created but not assigned to the
JobSchedulerService
class. This update ensures that the worker is properly attached to theJobSchedulerService
, allowing for a graceful shutdown process.Test - The changes were tested by linking the package to an existing Medusa project. The worker shutdown process was verified by adding
jobSchedulerService.worker_.close();
to the function handlingSIGTERM
andSIGINT
signals, then starting and stopping the server process to confirm proper worker closure.