DevelopersWorkers and scheduled jobs
Workers and scheduled jobs
Most apps need more than a web server: a queue consumer, a bot, database migrations on every deploy, a cleanup at night. On Hostwolf Apps these run next to your web app, from the same code, with nothing extra to pay.
- A worker is a process with no web address, such as a queue consumer.
- The release command runs once on every deploy, before the new version starts.
- A scheduled job runs a command at set times (cron), inside the web app or a worker.
Examples use the hw helper and $API from the quick start, and $APP is the web app's uuid.
Workers#
A worker uses the same repository, code and variables as the web app, with its own start command. It has no domain and no port.
- Copies: 1 to 10 copies of a worker run on its server.
- Where it runs: on the web app's server, or on another of your app servers. A worker on another server reaches your databases over your private network while it is on; without it, only at their public addresses.
- Limits: at most 10 workers per app. A worker's name is lowercase letters, numbers and dashes, starts with a letter and is at most 30 characters (
webandreleaseare taken). - Price: nothing extra. Workers run on the app servers you already pay for, so they use those servers' CPU and memory.
Static sites and Docker Compose apps can't have workers.
Add a worker in the console#
- Open the app and go to the Workers & jobs tab.
- Click Add worker, give it a name and its command, and pick the server and the number of copies.
- Deploy it. After that, it is deployed with the web app every time.
You can also start from New resource → Worker in a project, or from Create → Worker on the project canvas, and then choose the app.
Deploys#
- The web app builds first. After a successful deploy of the web app, each worker is rebuilt and deployed at the same commit.
- A push to the app's branch deploys the web app and then its workers. A push never deploys a worker twice.
- If the web app's deploy fails, its workers keep running the version they have.
- Variables come from the web app. A changed variable reaches a worker on its next deploy.
- A changed command or number of copies applies on the worker's next deploy. You can deploy one worker on its own; the web app is not deployed then.
The start command#
- Nixpacks and Railpack builds: the worker's command is its start command, for example
php artisan queue:workornode worker.js. - Dockerfile and image apps: the command replaces the image's
CMD. The image'sENTRYPOINTstill runs. For shell features such as&&, pipes or$VARIABLES, wrap it:sh -c 'cd /app && node worker.js'.
The web app's own start command is on the same tab. Leave it empty to use the one your build detects; Dockerfile and image apps then use their CMD. For a Dockerfile or image app you can set it too (start_command on the app): it replaces the image's CMD for the web container, and the image's ENTRYPOINT still runs, as for a worker. This is how to run an image whose CMD is not a server, for example bundle exec rails s -p 3000 -b 0.0.0.0.
Release command#
The release command runs once on every deploy of the web app, in a one-off container with the new code and your variables, after the build and before the new version starts. Use it for database migrations, for example php artisan migrate --force or DATABASE_URL=$DATABASE_DIRECT_URL npx prisma migrate deploy.
- If it fails, the deploy stops and the current version keeps running. Its output is in the deployment's build log.
- It runs for the web app only; workers skip it.
- With a Dockerfile or image, the release command is handled like a worker's command. If your image has an
ENTRYPOINT, the command is passed to it as its arguments (split into words, no shell), so a wrapper entrypoint that ends inexec yourapp "$@"receives it. If your entrypoint is a program wrapper, give just its arguments, for examplemigrate. To bypass the entrypoint, writesh -c '...'only if your image's entrypoint passes it through. An image without anENTRYPOINTruns the command withsh -c, and Nixpacks and Railpack apps run it in their own shell.
Procfile#
If your repository on GitHub has a Procfile, click Read Procfile on the Workers & jobs tab. The console proposes its entries: release: as the release command, web: as the web app's start command, and every other entry as a worker. Nothing is created until you click to add each one.
release: php artisan migrate --force
web: php artisan serve --host 0.0.0.0 --port 8000
worker: php artisan queue:work
mailer: php artisan queue:work --queue=mail
Workers over the API#
hw $API/applications/$APP/workers | jq # {data: [worker], release_command, start_command}
WORKER=$(hw -X POST $API/applications/$APP/workers \
-d '{"name": "worker", "command": "php artisan queue:work", "replicas": 2}' | jq -r .uuid)
hw -X POST $API/applications/$APP/workers/$WORKER/deploy # {message, deployment_uuid}
hw -X PATCH $API/applications/$APP/workers/$WORKER -d '{"replicas": 3}' # applies on its next deploy
hw -X PUT $API/applications/$APP/release-command -d '{"command": "php artisan migrate --force"}'
hw -X PUT $API/applications/$APP/release-command -d '{"command": null}' # remove it
hw -X DELETE $API/applications/$APP/workers/$WORKER -d '{"confirm": true}'
POST …/workerstakesname,command,server_uuid(a ready app server of your team fromGET /hostwolf/servers; default: the web app's server) andreplicas(1-10, default 1). It answers201with the worker, which is not deployed yet.- A worker is
{uuid, name, process, command, replicas, server_uuid, server_name, city, status, last_deployment, created_at}.processis its short name;statusisrunning,exited,restarting,startingordegraded. - A worker's own uuid works for its deployments (
GET /deployments/applications/{uuid}) and its logs (GET /applications/{uuid}/logs). DELETEstops the worker and deletes it with its scheduled jobs. Without"confirm": trueit answers422withconfirm_required: trueand changes nothing.422explains what is wrong: an invalid or taken name, an empty command, more than 10 workers, or a server that is not one of your ready app servers.
Worker autoscaling#
Each autoscaled worker container is limited to 256 MB of memory and 0.25 vCPU, including containers on its original server. More copies do not increase the limit of a single container. Check that each job fits before enabling worker autoscaling.
See Scaling for worker triggers and billing.
Scheduled jobs#
A scheduled job runs a command at set times inside the running web app's container, or a worker's. It uses that container's code and variables, so php artisan reports:send or node scripts/cleanup.js works as it does in your app.
- Schedule: a cron expression with 5 fields (minute, hour, day of month, month, day of week), such as
*/15 * * * *or0 3 * * *, orhourly,daily,weekly,monthlyoryearly. - Timezone: the schedule is read in an IANA timezone, such as
Europe/BerlinorAmerica/New_York. The default is UTC. - Timeout: a run is stopped after this many seconds: 60 to 36000 in the console, 300 by default.
- Command: at most 2000 characters.
- When the app is not running, for example during a stop, the run is skipped.
- Price: nothing extra. Jobs run on your own servers.
Add a job in the console#
- Open the app and go to the Workers & jobs tab.
- Under Scheduled jobs, click Add job. Pick where it runs (the web app or one of its workers), then enter the name, command, schedule, timezone and timeout.
- Run now starts it right away. Each job lists its last runs with their output (only team owners and admins see the output).
Output and alerts#
- Each run's output is kept with the job. It also goes to the app's lasting logs, each line starting with
[job <name>]. - A failing job sends a Scheduled job failed alert when your team has that alert on (Team → Alerts, on by default). The same page also has the Server out of memory alert; see Memory on the app server.
Jobs over the API#
JOB=$(hw -X POST $API/applications/$APP/scheduled-tasks -d '{
"name": "cleanup", "command": "php artisan cleanup", "frequency": "0 3 * * *",
"timezone": "Europe/Berlin", "timeout": 600}' | jq -r .uuid)
hw $API/applications/$APP/scheduled-tasks | jq # the web app's jobs
hw -X POST $API/applications/$APP/scheduled-tasks/$JOB/execute # run now: {message, execution_uuid}
hw $API/applications/$APP/scheduled-tasks/$JOB/executions | jq '.[0] | {status, message}'
hw -X PATCH $API/applications/$APP/scheduled-tasks/$JOB -d '{"enabled": false}' # switch it off
hw -X DELETE $API/applications/$APP/scheduled-tasks/$JOB
- A job is
{uuid, name, command, frequency, timezone, timeout, enabled, …}. Create takesname,commandandfrequency;timezone(default UTC),timeout(seconds, default 300, at most 36000) andenabled(defaulttrue) are optional. - A worker's jobs use the worker's uuid in place of
$APP:/applications/$WORKER/scheduled-tasks. executionslists the runs, newest first. A run's output is inmessage, which needs a token with Read sensitive data; without it the command is hidden too.- An invalid schedule or timezone answers
422with what to send instead.
SDK#
import { HostwolfApps } from '@hostwolf/apps-sdk';
const hw = new HostwolfApps({ token: process.env.HOSTWOLF_APPS_TOKEN! });
const worker = await hw.workers.create(app.uuid, { name: 'worker', command: 'php artisan queue:work', replicas: 2 });
const { deployment_uuid } = await hw.workers.deploy(app.uuid, worker.uuid);
await hw.deployments.wait(deployment_uuid);
await hw.workers.setReleaseCommand(app.uuid, 'php artisan migrate --force');
const job = await hw.jobs.create(app.uuid, { name: 'cleanup', command: 'php artisan cleanup', frequency: '0 3 * * *', timezone: 'Europe/Berlin' });
const run = await hw.jobs.runAndWait(app.uuid, job.uuid); // { status: 'success' | 'failed', message, duration }
workers.list, get, update and delete (with { confirm: true }), and jobs.list, update, run, executions and delete complete the set; see the SDK reference. The MCP server has the same as tools, such as create_worker and run_job.
CLI#
hostwolf apps workers my-app # workers, release command and start command
hostwolf apps workers create my-app --name worker --command "php artisan queue:work" --deploy
hostwolf apps workers update my-app worker --copies 3 --deploy
hostwolf apps workers deploy my-app worker --follow
hostwolf apps workers release my-app "php artisan migrate --force"
hostwolf apps workers delete my-app worker --confirm
hostwolf apps jobs my-app # every job of the app and its workers, with the last run
hostwolf apps jobs create my-app --name cleanup --schedule "0 3 * * *" --command "php artisan cleanup" --timezone Europe/Berlin
hostwolf apps jobs create my-app --name report --schedule hourly --command "node report.js" --worker worker
hostwolf apps jobs run my-app cleanup --wait # prints the output; exit 1 if it fails
hostwolf apps jobs logs my-app cleanup
hostwolf apps jobs disable my-app cleanup
See Workers and scheduled jobs in the CLI reference for every flag.
Run a command once#
Open Workers & jobs → Run a command, or use:
hostwolf apps run "$APP" -- 'php artisan migrate --force'
hostwolf apps run "$APP" --follow --timeout 60 -- 'node scripts/report.js'
# Images without a shell (including distroless) need an argv array:
hostwolf apps run "$APP" --exec -- node scripts/report.js
Each run starts a fresh container of the currently deployed image, with the app's variables, network and volumes. It works while the web app is stopped or crash-looping, after at least one deployment created its image and container. It receives no web traffic. Shell strings use bash -l for automatically built apps and sh for Dockerfile/image apps; --exec bypasses the shell and image entrypoint.
The run's environment is the environment of the last deployment: variables you save after that deployment reach runs only after the next deploy, so redeploy first if you just changed variables. Its filesystem is ephemeral — files a run writes are gone when it ends and are not shared with the running app container — so keep data in a database or a bucket.
A command gets 1 GB of memory and one CPU unless you ask for more, and never more than your server has: at most all its CPUs and its memory less 256 MB (on a small server the default is also at most 40% of the memory that is free). Raise or lower it per run with --memory and --cpus (memory in MB and cpus in the API):
hostwolf apps run "$APP" --memory 2g --cpus 2 -- 'bundle exec rails runner "puts User.count"'
Booting a Rails or Laravel app takes 300 to 600 MB, and headless Chrome about 1 GB. The command shares the server with your running app and its databases, whose memory reservations stay unchanged. There is no extra charge. One command may run per app, and three per team. Docker Compose and Swarm apps are not supported.
The CLI waits and returns the command's exit code. The timeout starts when the command's container starts; it defaults to 900 seconds (15 minutes), maximum 3600 seconds (60 minutes). 124 means timeout, and 137 means the command was killed, usually because it used more memory than allowed — start it again with --memory. --follow polls a live output buffer. Output is limited to 5 MB, with app environment values redacted before it reaches the console's output cache; avoid printing secrets or putting them directly in command arguments.
A token needs the deploy ability and your account needs permission to deploy the app. Run records include who started the run, command, status, exit code, output and start/finish times. Starting a run creates an audit event.
hw -X POST "$API/applications/$APP/runs" \
-d '{"command":"php artisan migrate --force","timeout":900}'
# Poll the returned uuid for live output and the final exit code:
hw "$API/applications/$APP/runs/$RUN"
hw "$API/applications/$APP/runs" # latest 50 runs
The API accepts either a command string or an argv array, such as {"command":["node","script.js"]}. A successful create returns HTTP 202. Poll while status is queued or running; terminal states are success, failed and timed_out. A concurrency conflict returns HTTP 409.
const run = await hw.runs.runAndWait(appUuid, ['node', 'script.js'], {
timeout: 60,
onOutput: (buffer) => console.log(buffer), // complete current buffer, not a delta
});
console.log(run.exit_code);
MCP tools: run_command, get_command_run, list_command_runs. run_command queues the run; poll get_command_run for completion.