shirakaba-add-job
Add a background job or a scheduled task to a Shirakaba application - the job DTO, an idempotent handler, pushing it in the writing transaction, workers, the schedule and tests. Use when asked to move
Files1
View on GitHubSKILL.mdpackages/docs/skills/shirakaba-add-job/SKILL.md
GitHubAdd a background job to a Shirakaba application
Read vendor/shirakaba/docs/guide/14-background-jobs.md and vendor/shirakaba/docs/design/10-background-work.md first.
- Job DTO. A
#[JsonDto]final readonlyclass holding IDs and small values, not entities or services: it is stored as JSON. - Handler. Register it with a stable name, so renaming the class does not orphan
waiting jobs:
$jobs->handle(SendWelcome::class, $handler, name: 'mail.welcome'). The handler receives the job and aRequestScope; lease database access from that scope. - Make it idempotent. Delivery is at least once: record a unique key of the work done
(an insert with
onConflict) and return early when it exists. - Push it in the transaction that writes the data it depends on:
$db->transaction(fn (Writer $db) => ... $jobs->push(new SendWelcome($user->id), $db)). UsePushOptionsfor the queue, a delay or a unique key. - Recurring work. Add a task to the
Schedulewith a cron expression and a stable name; make sureCreateScheduleTableis migrated and cron runsapp schedule:runevery minute. - Test. With
InMemoryQueue, assertpendingTypes()after the request; run jobs withJobRunner::runPending()and assert their effect, including running one twice. For a schedule, call$schedule->run($writer, $frozenClock)and advance the clock.
Verify
vendor/bin/phpstan analyse- the test suite, including the twice-run (idempotency) test
app queue:work --stop-when-emptyprocesses a pushed job locally;app schedule:listshows the next run of a scheduled task