Search by

mishabachkur / laravel-once-run

mishabachkur

Artisan commands that run exactly once during deployment, like migrations for data

Package info

github.com/mishabachkur/laravel-once-run

pkg:composer/mishabachkur/laravel-once-run

Statistics

Installs: 19

Dependents: 0

Suggesters: 0

Stars: 2

Open Issues: 0

v1.1.0 2026-12-03 17:09 UTC

This package is auto-updated.

Last update: 2026-12-03 17:10:22 UTC


README

Run Artisan commands exactly once during deployment, safe by default for blue-green and multi-server setups. Migrations stay for schema changes; once-run commands handle backfills and other one-time data tasks.

Requires PHP 8.2+ and Laravel 12 or 13.

Why this package

  • The lock is always on. There is no flag to forget in a deploy script, and it lasts 24 hours, so a long backfill is not started twice. It needs a shared cache store (see below).
  • Plain Artisan commands. Output, options and dependency injection work as usual.
  • Synchronous. A command counts as done only when handle() succeeds.
  • Interrupted runs are visible. The runner warns when it retries a command that did not finish.

If you need checkpoints to resume a long operation partway, look at directorytree/operations. If you want queued operations, look at timokoerber/laravel-one-time-operations.

Installation

composer require mishabachkur/laravel-once-run
php artisan vendor:publish --tag=once-run-migrations
php artisan migrate

Publish the config (--tag=once-run-config) or the stub (--tag=once-run-stubs) to change defaults.

The runner lock lives in a cache store that every deploying server or container must share. Set it with ONCE_RUN_CACHE_STORE=redis (or database, memcached, dynamodb). A file, array or null store does not prevent parallel runs, and once:run warns about it.

Usage

php artisan make:once_run BackfillUserUuids

This creates a timestamped class in app/Console/Commands/OnceRun. Implement handle(). Run pending commands after migrations in your deploy script:

php artisan migrate --force
php artisan once:run --force --no-interaction
Option Effect
--pretend List pending commands without running them
--force Run, or release the lock, without confirmation
--wait[=seconds] Wait for a held lock instead of failing at once
--release-lock Release a lock left by a killed process

Waiting for the lock

Without --wait, once:run fails immediately if the lock is held. With it, the command waits, then determines what is still pending after it gets the lock, so commands finished by the other process are not run again. If the time runs out, it exits with a failure. --wait without a value waits up to lock.seconds (24 hours by default), so set a number if your deploy job has a shorter timeout.

Releasing the lock

--release-lock asks for confirmation, because another process may still be working, and releasing its lock lets the same commands run twice in parallel.

Scripts: non-interactive scripts must now pass --force, otherwise the lock is not released and the command fails:

php artisan once:run --release-lock --force

Checking the state

php artisan once:status

Lists every registered once-run command with started_at, finished_at and its state:

State Meaning
pending No record yet
finished Completed, or marked as finished
unfinished Started but not finished: it failed, was interrupted, or is running right now

A running command with runsInTransaction() shows as pending, because its record is not committed yet.

Marking without running

In a new environment, where there is no old data to process, mark commands as finished without running them:

php artisan once:mark --all
php artisan once:mark 'App\Console\Commands\OnceRun\Run_2026_11_20_170000_BackfillUsers'

Both ask for confirmation; pass --force to skip it (required in non-interactive scripts). --all includes unfinished commands. once:mark takes the deployment lock, so it fails while a run is active.

How it works

  • Every registered command extending MishaBachkur\OnceRun\OnceRunCommand is picked up and run in class name order.
  • Executions are recorded by class name in once_run_commands. Never rename or move a deployed command.
  • A failure stops the runner with a non-zero exit code. The failed command runs again next time.
  • Return true from runsInTransaction() for DB-only work, so the work and its record are committed together.
  • The lock lasts 24 hours by default. SIGINT and SIGTERM release it. SIGKILL, including an out-of-memory kill, does not: the lock stays until lock.seconds pass or you release it with --release-lock.
  • In MySQL and MariaDB, DDL statements (CREATE, ALTER, DROP, TRUNCATE and others) cause an implicit commit. runsInTransaction() cannot roll back the DDL or the work done before it. Keep schema changes in migrations.

Writing safe commands

  • Make them idempotent: a retry must not duplicate changes.
  • Keep them synchronous and non-interactive, with no required arguments.
  • Dispatching a queued job does not mean the work is done.
  • Calling a command directly by its signature bypasses tracking and the lock.