KScript docs
EN

Queues and jobs: KScript.Queue

Connections and drivers

A connection is where jobs wait, and its driver decides how. You name connections in kscript.json under queue:

JSON
{
  "queue": {
    "default": "file",                 // the connection jobs go to
    "connections": {
      "uploads": { "driver": "file", "path": "storage/queue" },
      "reports": { "driver": "database", "connection": "reports" }
    },
    "queues": ["high", "default"],     // only these, first to last
    "failed": { "keep": "7d" },        // how long failed jobs are kept
    "worker": { "concurrency": 2, "sleep": "1s" },
    "env": {
      "test": { "default": "sync" }    // per environment, as in $config
    }
  }
}

Four connections are always there, named after their drivers: file, database, memory and sync. Without queues, workers take the default queue first, then every other queue.

The file driver

The file driver is the default, and suits most apps. It keeps one JSON file per job in the app's data folder (see App folders), or in the folder path names. A worker claims a job by moving its file, so several workers, even in different processes, never run the same job twice.

The database driver

The database driver keeps jobs in the project's database (see Databases). It suits apps that already have a database, and workers on several machines that share it. Waiting and running jobs go in a kscript_jobs table, failed jobs in kscript_failed_jobs, and the keys of unique jobs in kscript_job_locks. It makes the tables the first time it needs them, so there is nothing to set up.

With no connection, it uses the default database: data/app.sqlite in the project folder, or DATABASE_URL. To keep jobs apart from the app's data, name another database connection from your config:

JSON
// kscript.json
{ "queue": { "default": "jobs", "connections": { "jobs": { "driver": "database", "connection": "queue" } } } }

// config.json
{ "database": { "connections": { "queue": { "url": "sqlite:data/queue.sqlite" } } } }

Naming the tables, and making them with migrations

tables names the tables a database connection uses. Leave out any you don't want to rename:

JSON
// kscript.json
{
  "queue": {
    "connections": {
      "database": { "driver": "database", "tables": { "jobs": "jobs", "failed": "failed_jobs", "locks": "job_locks", "batches": "job_batches" } }
    }
  }
}

To make the tables with the rest of your migrations, instead of the first time a job is dispatched, publish their migration:

Shell
kscript publish queue                      # the database connection's tables
kscript publish queue --connection jobs    # another database connection's
kscript db migrate

kscript publish queue writes database/migrations/<time>_create_queue_tables.kscript, using the names in tables, and sets the connection's auto_create to false. The driver then leaves the tables to the migration, and says to run kscript db migrate if they aren't there yet. Test databases run the migrations, so tests that dispatch to the database queue keep working. A connection that keeps its jobs in another database (connection) leaves its tables to the driver, because kscript db migrate runs on the default database.

A worker takes a job in one write transaction that reads the next due row and marks it as running, so two workers never get the same job. If the database is busy or can't be reached, the worker says so and tries again after a pause, rather than stopping.

The memory and sync drivers

The memory driver keeps jobs in memory, in one process only. kscript serve --with-worker uses it to run a worker inside the server.

The sync driver doesn't queue anything: ->dispatch() runs the job straight away, and its errors reach the caller. It suits tests, and scripts where waiting isn't worth it.