When dealing with Http Handler or closing a database connection, etc., suppose there are multiple Jobs in a service with a live worker, how do I wait until all the Jobs are finished before shutting down and deleting the service (using Docker)? Here is the whole operation process:
Problems encountered
When the service is shut down or forced to use ctrl + c to stop, the service should wait until all workers have completed all jobs. First, let’s see what’s wrong with the first version. When the developer presses ctrl + c, the cancel() signal will be sent, then let’s see how the workers were originally written. For the complete code, please refer to here.
|
|
Suppose there are 10 jobs coming in at the same time and there are four workers processing at the same time. Done() channel will be triggered after pressing ctrl + c. Since Select accepts two channels, the developer cannot predict which one will be triggered first, but if jobsChan has other jobs to process, it will be terminated by the program. How to solve this problem? Continue to the next page
Rewrite worker
In fact, it’s as simple as rewriting the worker part and not using the select method (see the code here).
When the for method is used to read jobsChan, the for loop will not end until the channle is completely empty, so there are multiple workers reading jobsChan at the same time. wg.Done() will be triggered after the for is finished to tell the main program that the worker has finished and the main program can proceed to close. Note that the Consumer will receive a cancel() trigger first and then close the jobsChan channel, but it can still read the remaining jobs out of the channel by for. You can see how the consumer is written (see here for the full code):
|
|
Summary
It depends on the needs of the project to decide whether to stop the worker immediately or to wait until all the jobs are finished. The difference between the two approaches is that the former requires two more channels to be processed in the worker, while the latter only requires a for loop to read out all the job channels before finishing.