Sari la conținutul principal
FreeThree checks for your projectUptime, accessibility, SEO and cookie checks for your project
Menu

Cron and processes

Run scheduled jobs and long-running programs beside your website.

On this page

Besides answering web requests, a website usually has work to do in the background. Tilizy offers two tools for it, both declared in .tilizy/app.yaml: scheduled jobs under crons, which run a command every few minutes, and long-running programs under processes, which Tilizy starts and keeps running.

Which one to use

UseWhenExamples
cronsThe work can wait for the next minute and ends by itself.The cron run of your framework, a cleanup, a feed import
processesThe work must react within seconds, or the program has to keep running.A queue worker, an indexer, a small server of your own

Scheduled jobs

crons:
  - schedule: '* * * * *'
    cmd: drush cron
  - schedule: '*/15 * * * *'
    cmd: drush queue:run my_queue

Each entry needs both keys.

  • schedule has the five fields of a crontab line. Only intervals in minutes are supported: * * * * * for every minute, or */N * * * * for every N minutes. The other four fields must be *.
  • cmd is a shell command. It runs from the root of your application, with the same environment variables as the website. The tools your project installs with Composer, such as drush, are on the path.

To run something once a day, schedule a command every few minutes and let your application decide whether the work is due. The scheduler of your framework does exactly that. On Laravel, for example, run php artisan schedule:run every minute.

Each environment runs its own scheduled jobs. Commit the change and deploy to apply it.

Long-running processes

processes:
  worker:
    cmd: "php artisan queue:work"
    resources:
      mem: 512m
      cpus: 0.5

The name, here worker, is yours to choose. Tilizy runs the command beside your website, with the code of the current release, the same environment variables and access to the same services. The application code is read-only for the process.

KeyWhat it does
cmdThe command to run. It must stay in the foreground. When it ends, Tilizy starts it again.
buildA list of commands that prepare the process during a deploy, for example compiling it. They run in the build container after hooks.build, and are tried up to three times.
readyA check that tells Tilizy the process is usable: cmd plus the optional interval, timeout, retries and start_period. The check runs next to the process. A deploy waits for it.
portThe port the process listens on, if it listens at all. Your website reaches it on localhost at this port. Two processes cannot claim the same port: that is an error when the file is read.
envExtra environment variables for this process only. They cannot replace a variable of the website or one that starts with TILIZY_.
resourcesLimits for the process: mem, cpus, pids.
restartalways, on-failure or no. Leave it out for the default.
own_networktrue gives the process its own host name, equal to the name of the process, so that other parts of the environment can reach it by name instead of on localhost.
replicasHow many copies to run. More than one is allowed only together with own_network: true.

A process with a check looks like this:

processes:
  indexer:
    cmd: "bin/indexer"
    port: 9464
    ready:
      cmd: "bin/indexer --healthcheck"
      interval: 10s

Processes and deploys

  • A process holds the code of a release in memory. After every deploy, and after a rollback, Tilizy restarts each process on the code that is now live.
  • The deploy waits until every process with a ready check reports ready.
  • If a process never becomes ready, the deploy is reported as failed. The new release stays live: fix the process and deploy again.

Check what is running

Read the recent log lines of the live environment, and keep following them:

tilizy app:logs --prod -f

The copy on your own computer is built from the same file. See Run your website locally.

What can go wrong

  • The schedule is refused. A schedule with a fixed time, such as 0 3 * * *, is not accepted. Use an interval in minutes.
  • A job runs during a deploy. Scheduled jobs are not paused while a deploy runs. Until the switch, they run the code of the previous release against a database that may already be updated. Keep database updates compatible with the previous release.
  • The process is restarted again and again. A command that sends itself to the background looks finished, so Tilizy starts it again. Run the program in the foreground.
  • A job is faked with a loop. Do not keep a cron command alive with a loop to react faster than once a minute. Declare a process.
  • The memory runs out. Jobs and processes share the memory of your server with the website. Set resources for a process that could grow.

Next: Deploys and releases and Storage and limits.