Artisan commands that run exactly once during deployment, like migrations for data
Теги
README
Laravel Once Run
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 without confirmation |
--release-lock |
Release a lock left by a killed process |
How it works
- Every registered command extending
MishaBachkur\OnceRun\OnceRunCommandis 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
truefromrunsInTransaction()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 does not.
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.
Читати в документації
Схожі пакети
Laravel Octane
Прискорює застосунок, тримаючи його в пам'яті між запитами через FrankenPHP, Swoole або RoadRunner. Прибирає завантаження фреймворку з кожного запиту, даючи кратний приріст пропускної здатності.
Laravel Reverb
Власний WebSocket-сервер для Laravel: real-time мовлення без сторонніх сервісів на кшталт Pusher. Сумісний із протоколом Pusher, тож Echo на фронтенді працює без змін.
- Походження
- Українська розробка
- Категорія
- Інфраструктура та DevOps