Reading a container’s real CPU quota before sizing a pool

nproc reports the host’s cores, so a worker pool sized from it starts thirty-two processes in a container entitled to two.

quota=$(cut -d' ' -f1 /sys/fs/cgroup/cpu.max 2>/dev/null)
period=$(cut -d' ' -f2 /sys/fs/cgroup/cpu.max 2>/dev/null)

if [ -n "$quota" ] && [ "$quota" != "max" ]; then
  cpus=$(( quota / period ))
else
  cpus=$(nproc)
fi

exec php artisan queue:work --workers="$(( cpus * 2 ))"

The arithmetic is integer division, so a quota of 1.5 cores reports one — which is the safe direction to round. Most language runtimes still do not do this in 2022: the JVM does, Node does not, and PHP has no notion of it at all, so anything sizing a pool has to compute it in an entrypoint script.