Download the PHP package aihimel/laravel-waiting-request without Composer
On this page you can find all versions of the php package aihimel/laravel-waiting-request. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-waiting-request
Laravel Waiting Request
A simple implementation for holding requests until a job or background process is finished. This package allows you to conditionally block requests and wait for them to be unblocked within a specified timeout.
Installation
You can install the package via composer:
Configuration
You can publish the configuration file using:
The published configuration file config/waiting-request.php contains the following defaults:
| Key | Env | Purpose |
|---|---|---|
cache_prefix |
LW_REQUEST_CACHE_PREFIX |
Prepended to every cache key the package writes. |
timeout |
LW_REQUEST_MAX_WAITING_TIME |
How long whenResolved() waits before giving up. |
check_interval |
LW_REQUEST_CHECK_INTERVAL |
How often whenResolved() polls between checks. |
max_blocking_time |
LW_REQUEST_MAX_BLOCKING_TIME |
Default lifetime of a blocker. After this many seconds, the next isBlocked() / whenResolved() call evicts the blocker and emits a warning log. |
Usage
Basic Example
Suppose you have a resource that is being processed in the background (e.g., a large PDF generation or a data sync). You can block requests for this resource until the process is complete.
1. Add a Blocker
In your controller or job where the background process starts:
2. Wait for Resolution
In the request that needs to wait for the resource:
3. Resolve the Blocker
When the background process is finished:
Calling resolveBlocker() explicitly is still the recommended path — it releases the lock immediately so waiting requests can proceed and avoids the warning log emitted by the auto-expiry backstop (see below).
Checking if Blocked
You can also check if a resource is currently blocked without waiting:
Note:
isBlocked()is not a pure read. If the blocker has passed its expiry, this call also forgets the cache entry and emits aLog::warning. Avoid placing it on a hot path that you expect to be side-effect-free.
Blocker Lifetime
Every blocker now has a finite lifetime.
- The lifetime defaults to
max_blocking_timefrom the config (10 seconds out of the box). - You can override it per call via the third argument to
addBlocker(). - When the lifetime is reached, the next
isBlocked()(and thereforewhenResolved()) call:- Deletes the cache entry, and
- Emits
Log::warning('Waiting-request blocker expired without being resolved', [...])withclass_path,resource_id, andexpired_atin the context.
The log line goes through Laravel's Log facade, so it honors whatever channel and formatter your app has configured in config/logging.php.
If you have a background job that may take longer than 10 seconds, raise LW_REQUEST_MAX_BLOCKING_TIME (or pass an explicit TTL to addBlocker()) — otherwise the blocker will be auto-released while your job is still running, defeating the purpose of the lock.
Upgrading
From 1.x to 2.x
Version 2.x changes how blockers expire. The public method signatures stay backwards-compatible, but the runtime behavior changes:
- Blockers now expire automatically. In 1.x, a blocker lived in cache until
resolveBlocker()was called. In 2.x, every blocker has a finite lifetime (default 10s, configurable viaLW_REQUEST_MAX_BLOCKING_TIME). If your job can exceed that, set the env or pass a per-call TTL. isBlocked()has side effects. It deletes expired entries and emits aLog::warning.- The cache value shape changed from
trueto a Unix expiry timestamp. Any pre-upgrade blocker keys left in cache will be read as integer1, treated as already-expired, deleted, and logged on first access. Flush the cache (or at least the keys undercache_prefix) when deploying the upgrade to avoid a one-time burst of warning logs and stale-blocker side effects.
See CHANGELOG.md for the full change list.
Features & Bug Fixes
If you find any bugs or have a feature request, please create an issue on GitHub.
Contributing
We welcome contributions! To become a contributor:
- Fork the repository.
- Clone your fork to your local machine.
- Create a branch for your feature or bug fix.
- Commit your changes with descriptive messages.
- Push your branch to your fork.
- Submit a Pull Request to the main repository.
Local Development
This package comes with a Docker-based development environment.
Build container
Run automated tests
Run PHPCS code inspection
Run PHPCBF auto-fixer
License
The GPL-3.0-or-later License. Please see License File for more information.