Guido Leibbrand
Guido Leibbrand

Reputation: 13

Soft restart daemon containers in docker swarm

we use multiple PHP workers. Every PHP worker is organized in one container. To scale the amount of parallel working processes we handle it in a docker swarm.

So the PHP is running in a loop and waiting for new jobs (Get jobs from Gearman). If a new job is receiving, it would be processed. After that, the script is waiting for the next job without quitting/leaving the PHP script.

Now we want to update our workers. In this case, the image is the same but the PHP script is changed. So we have to leave the PHP script, update the PHP script file, and restart the PHP script.

If I use this docker service update command. Docker will stop the container immediately. In the worst case, a running worker will be canceled during this work.

docker service update --force PHP-worker

Is there any possibility to restart the docker container soft? Soft means, give the container a sign: "I have to do a restart, please cancel all running processes." That the container has the chance to quit his work.

In my case, before I run the next process in the loop. I will check this cancel flag. If this cancel flag set I will end the loop and end running the PHP script.

Environment:

Upvotes: 0

Views: 163

Answers (1)

Guido Leibbrand
Guido Leibbrand

Reputation: 13

In the meantime, we have solved it with SIGNALS.

In PHP work with signals is very easy. In our case, this structure helped us.

//Terminate flag
$terminate = false;

//Register signals
pcntl_async_signals(true);
pcntl_signal(SIGTERM, function() use (&$terminate) {
  echo"Get SIGTERM. End worker LOOP\n";
  $terminate = true;
});
pcntl_signal(SIGHUP, function() use (&$terminate) {
  echo"Get SIGHUP. End worker LOOP\n";
  $terminate = true;
});

//loop
while($terminate === false){
  //do next job
}

Before the next job is started it is checked if the terminate flag is set.

Docker has great support for gracefully stopping containers. To define the time to wait we used the tag "stop_grace_period".

Upvotes: 0

Related Questions