# Background jobs

URL: https://rustpress.witsberry.com/background-jobs/

> Scheduled posts, email, WP-Cron events and plugin jobs run inside RustPress with retries, backoff and a breaker. Action Scheduler functions work too.

This page explains how RustPress runs work in the background: scheduled posts, email, WordPress-style cron events and plugins’ background jobs. On a VPS, in Docker, or on Render, Railway or Fly.io, all of it runs inside `rp serve` and you don’t need a cron job.

## What runs by itself

*   **Scheduled events** (`wp_schedule_event` and friends) are checked every 30 seconds.
*   **Background jobs** run as soon as they are due, on `queue_workers` workers (default 2).
*   **Scheduled posts** are published on time, and **email** is sent from a queue with retries.
*   **Upkeep**: daily cleanup, weekly database compaction and the twice-daily update check.

## Queueing a job from a plugin

Use a job for work that shouldn’t slow a page or may fail: syncing an order, sending to an API, rebuilding an index. A worker calls the plugin’s action with the arguments.

```
// JavaScript
rp.addAction('myshop_sync_order', async (orderId) => {
  const r = await rp.http('POST', 'https://api.example.com/orders', { body: { id: orderId } });
  if (r.status >= 500) throw new Error('service down');   // retried later
});
await rp.queue.push('myshop_sync_order', [orderId],
  { attempts: 5, backoff: [10, 60, 300], unique: 'order-' + orderId });
```

```
// PHP
add_action( 'myshop_sync_order', 'myshop_sync_order' );
wp_rs_queue_push( 'myshop_sync_order', array( $order_id ), array( 'attempts' => 5 ) );

// Action Scheduler's functions work too
as_enqueue_async_action( 'myshop_sync_order', array( $order_id ) );
as_schedule_single_action( time() + 3600, 'myshop_sync_order', array( $order_id ) );
```

Python and Rust plugins use `queue_push()` and `queue_cancel()`. Cancel a waiting job by its id or its `unique` key.

## Options

| Option     | What it does                                                                                        |
| ---------- | --------------------------------------------------------------------------------------------------- |
| `delay`    | Seconds to wait before the first run                                                                |
| `attempts` | How many times to try (default 3)                                                                   |
| `backoff`  | Pauses between tries in seconds, e.g. `[30, 120, 600]`; the last one repeats, plus up to 20% jitter |
| `timeout`  | Seconds one run may take                                                                            |
| `unique`   | Keeps one waiting job per key: pushing again returns the same id                                    |
| `queue`    | A named queue, to group jobs                                                                        |

## Failures and retries

*   A job fails when its action throws, or replies with an error. It runs again after the next backoff step until its attempts are used up.
*   Then it is kept as failed under **Tools › Scheduled tasks**, where you can retry or delete it, and the `rp_job_failed` action fires with the id, plugin, hook, arguments, attempts and error.
*   A hook that fails 5 times in a row rests, from a minute up to an hour, instead of hammering a service that is down.
*   A one-off cron event whose plugin callback fails is retried the same way.
*   Limits: arguments up to 64 KB (pass ids, not data), at most 10,000 waiting jobs per plugin, at most 50 jobs per pass per worker.

## Several servers on one database

Run them all. A database lease lets one server run the scheduled events at a time, and each job is claimed by exactly one worker. To turn the built-in runner off on a server, set `background_cron = false` in `rp-config.toml`; then a cron job must run `rp cron run`.

## Shared hosting and serverless

Without `rp serve` (CGI or FastCGI on shared hosting, or serverless), add a system cron job that runs every minute, either the command or the address:

```
* * * * * cd /path/to/site && /path/to/rp cron run >/dev/null 2>&1
* * * * * curl -fsS https://example.com/rp-cron >/dev/null
```

Without either, RustPress does the same work after visits, at most once a minute, so a quiet site may run late. `/rp-cron` answers at most once every 10 seconds. See [Hosting](/hosting/) for the shared hosting steps.

## Seeing what’s queued

```
rp cron list   # scheduled events and job counts
rp cron run    # run what is due once
```

**Tools › Scheduled tasks** shows events and jobs, with failures. Every operation and limit is in the [Plugin API reference](/plugin-api/).
