Keep a background worker running
A worker is a program that keeps running and waits for work, such as a queue worker that sends emails as soon as your app adds them. Keep a worker running starts the program, watches it, and starts it again if it stops. This article explains when to use a worker and how to set one up.
Before you start
- Workers are on Business (1 worker) and Pro (3 workers). Essential doesn't include workers. See Schedule limits by plan.
- Your program must keep running on its own and not exit when it runs out of work.
- On Essential, run the work on a schedule instead. A queue command that stops when the queue is empty, run every minute, does the same job. See Use a schedule instead.
Set up a worker
- Open Account → Hosting → your site → Schedules.
- Click New schedule.
- Give it a Name, such as
Queue worker. - Under What to run, choose Keep a worker running.
- Under Runs with, choose Command, PHP, Node.js or Python. PHP, Node.js and Python use the version your site or app already uses.
- Enter what to run, such as
artisan queue:work --sleep=3 --max-time=3600with PHP. - Open Advanced options and set Working folder to your app's folder, such as
shop. - Under Restart, choose Always or Only if it fails.
- Click Save schedule.
The worker starts within about a second. Its row on the list shows Running and how long it has been up.
Restart choices
| Choice | What happens when the program ends |
|---|---|
| Always | It starts again, whether it ended with an error or not. Best for workers that exit on purpose now and then, such as --max-time=3600 |
| Only if it fails | It starts again only after an error. If it exits with code 0, its row shows Stopped, and Resume in its menu starts it again |
Each restart after a failure waits a little longer than the last, starting at 10 seconds and growing to at most 5 minutes, so a worker that crashes at once doesn't use up your account's resources. Every start and stop shows in history.
A worker isn't held to your plan's Longest run. It keeps running until it exits or you pause it. Set a Memory limit in Advanced options to keep it in check.
Example workers
| App | Runs with | What to run | Working folder |
|---|---|---|---|
| Laravel queue | PHP | artisan queue:work --sleep=3 --max-time=3600 |
shop |
| Symfony Messenger | PHP | bin/console messenger:consume async --time-limit=3600 |
shop |
| Node.js queue | Node.js | worker.js |
apps/api |
| Python queue | Python | worker.py |
apps/tasks |
Tip: Let your worker exit and restart about once an hour, with an option like --max-time=3600. That clears memory leaks before they build up, and Always starts it again straight away.
Stop, restart and change a worker
| To | Do this |
|---|---|
| Stop it for a while | Click Pause. The worker is asked to stop, and ended 10 seconds later if it hasn't. A paused worker doesn't count toward your plan's workers |
| Start it again | Click Resume |
| Restart it after a deploy | Click Pause, then Resume. Or use your app's own restart command, such as php artisan queue:restart |
| Read its output | Click History, then open the current run. Its output updates every second |
A worker's output is kept like any run's, within your plan's output limit. Old output is trimmed from the middle, so the start and the newest lines are always there.
Use a schedule instead
On every plan, a schedule that runs every minute and stops when its work is done is often all you need.
| App | Command | When |
|---|---|---|
| Laravel queue | /usr/local/bin/php artisan queue:work --stop-when-empty |
Repeat, every 1 minute, If still running set to Skip |
| Laravel scheduler | /usr/local/bin/php artisan schedule:run |
Repeat, every 1 minute |
Skip means a long batch is never started twice. See Retries, time limits and other job options.
If something goes wrong
| Problem | Fix |
|---|---|
| Keep a worker running isn't offered | Your plan doesn't include workers, or you already use all of them. Pause another worker, or use a schedule instead |
| The worker keeps restarting | Open History and read the output of the last run. The error is usually near the end |
| The worker uses more and more memory | Make it restart regularly, such as with --max-time=3600. Set a Memory limit in Advanced options |
| Changes to your code don't show | Workers keep old code loaded. Restart the worker after each deploy |
Still stuck? Ask us and we'll walk you through it.