medusa icon indicating copy to clipboard operation
medusa copied to clipboard

fix: assign scheduled job worker to JobSchedulerService class

Open u11d-bartlomiej-galezowski opened this issue 6 months ago • 2 comments

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 the JobSchedulerService, 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 handling SIGTERM and SIGINT signals, then starting and stopping the server process to confirm proper worker closure.