Shutdown-only hook for service providers (e.g. a tick that runs only on shutdown)
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 48/100
Research direction
Start by tracing Ticks::every and run_on_shutdown through the shutdown sequence, then inspect ServiceProvider and the testing helpers mentioned in the issue. The issue allows either a shutdown-only tick callback or a provider shutdown hook, and also suggests adding a way for tests to trigger shutdown. Done means shutdown work runs within the existing timeout after in-flight work finishes, without running periodically, with coverage for the shutdown behavior.
Written by the indexing model from the issue text.
Description
Summary
Please add a way to register a callback that runs only when the process shuts down, with the same timing and deadline as today's run_on_shutdown ticks: after in-flight requests, jobs, and background work finish, within server.shutdown_timeout.
Use case
We're building the PostHog integration for Laravel Rust (PostHog/posthog-rs#426). The PostHog client already batches and sends events from its own background worker every few seconds. It also holds failed batches until their exponential backoff or the server's Retry-After allows a retry.
The provider only needs to flush once, when a server or queue worker stops, so events buffered by the last requests or jobs aren't lost.
A periodic forced flush is harmful here. Flushing retries held batches immediately, ignoring their Retry-After. During an ingestion outage that adds traffic and uses up retry attempts early.
Current behavior (0.1.4 and 0.1.5)
Ticks::every(interval, callback) is the only way to get a callback into the shutdown sequence, via .run_on_shutdown(). A tick must have a non-zero interval and always runs periodically too. There's no shutdown-only option, and ServiceProvider has no shutdown method.
Our workaround
We register a tick whose interval is long enough that it never fires periodically, and rely on run_on_shutdown for the one run we want:
// This tick only exists to flush when the server or worker stops.
// Laravel Rust 0.1 has no shutdown-only hook: a tick must have an
// interval, and `run_on_shutdown` runs it once more on exit. The
// 365-day interval keeps it from ever firing periodically.
app.ticks()
.every(365.days(), flush)
.name("posthog:flush")
.run_on_shutdown();
This works, but:
- The intent isn't visible from the API.
- The tick appears as a yearly tick wherever ticks are listed.
- A process that runs longer than the interval would get a forced flush.
Possible APIs
Any of these would work for us:
ticks.on_shutdown(callback)(orTick::only_on_shutdown()): a callback that runs only in the shutdown sequence, with the same dependency injection andconfig()context as ticks.- A
ServiceProvider::shutdown(&self, app: &Container)hook, mirroringboot.
Testing
It would also help if the testing helpers could trigger the shutdown sequence, for example app.shutdown().await or a queue:work run in tests that starts and finishes ticks. Today app.tick(name) always runs a tick, and the testing console doesn't start tick loops. So we can't write a test showing that a tick does not run periodically, or that shutdown callbacks run.
- Dominant language
- Rust
- Stars
- 2.2k
- Forks
- 105
- Avg merge
- 7h 19m
- Merged PRs (30d)
- 41
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from laravel/rust
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
laravel/rust#98 · 8 comments · 10 reactions ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
neuralmagic/crucible#579 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
laravel/rust-framework#864 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
eclipse-score/kyron#249 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
seanmonstar/reqwest#3157 ·
Maintainers usually reply within 2 days