Overview

Beach supports user-defined daemon scripts for selected project plans. Daemon scripts can be used for long-running PHP processes, for example for watching an event loop or running a job queue. Beach makes sure that your script keeps running and, in case in crashes or terminates, will try to start it again right away. After it failed a couple of times in a row, Beach will not try to start it again until the next deployment.

The Details

Simply create a file called beach-service-something.sh, where you replace "something" with something meaningful. Commit the file to your project's root directory and deploy it. You can create multiple scripts, just make sure that their filename starts with "beach-service-".

The following script watches a Flowpack job queue. It has a few interesting details:

  • depending on if the MY_PROJECT_WORKER_SCRIPTS_ENABLE variable is set, the script is active or not. That way you can enable or disable daemon scripts per Beach instance
  • the job:work command exits after 10 minutes or when 10 jobs were processed. This way you can avoid memory leaks and other side effects of long-running processes in PHP
#!/bin/bash

if [ "${MY_PROJECT_WORKER_SCRIPTS_ENABLE}" != "true" ] ; then
    echo "$(basename "$0"): sleeping because worker scripts are disabled ..."
    sleep 86400
    exit 0
fi

exec /application/flow job:work --limit 10 --exit-after 600 MyJobQueue

Important: You should make sure that your script does not exit right away, because the system will try to restart your script. Even though your script will not be restarted endlessly, this might generate some CPU load, so try to avoid it.