How to deploy atomically on Nginx+Php-fpm without losing requests?

During deployment I need to change php-fpm source code (more files), but I don’t want to lose ANY http requests. Old pending requests could be handled by new source code (let’s not worry about compatibility).

I don’t think a reload (USR2 signal) of the php-fpm service is sufficient (it’s not possible to atomically swap the source code because opcache.revalidate_freq), it probably has to do:

  1. php-fpm stop
  2. source code update (mv command)
  3. php-fpm start,

but in that case nginx doesn’t wait for php-fpm to start and starts returning 503 to visitors.

Does anyone know a simple way how to deploy atomically and process 100% of requests?

I have a solution that might work (another server in nginx upstream waiting for php-fpm to start…, configure graceful php-fpm stop…) but it seems complicated.