For simple add/deduct inventory's quantity.
- A event listener is created called
AsyncUpdateInventoryQuantityand it listens to theUpdateInventoryQuantityevent. - To make it queueable, this listener implements
ShouldQueueindicating to Laravel that the job should be pushed onto the queue to run asynchronously. - This listener also implements
ShouldBeUniqueto ensure that only one instance of a specific job is on the queue at any point in time. - When the listener handle the job, it updates the quantity of the specific invetory with pessimistic locking
lockForUpdate.
- Git clone the project and change the working directory to the project root.
- Run the command
cp .env.example .envto define environment variables. - Install dependencies with
composer install. - Generate app key with
./vendor/bin/sail artisan key:generate. - Make sure Docker is running. Then run
./vendor/bin/sail up -dto start and run the container. - Run DB migration and seeding
./vendor/bin/sail artisan migrate:fresh --seed. - Send a couple of API requests with
curl --request POST --url http://localhost/api/inventories/random. - Run the worker manually
./vendor/bin/sail artisan queue:work.
- API authentications and authorization are not handled in this demo app.
- Rate limiting is following Laravel's default: max 60 attempts per minute by IP address.
- If APIs were authenticated,
uniqueIdshould be use and define the job unique key with sender's ID. - The API should return a unique request ID. With this ID the API sender can check the result of the sent API. (Out of scope for now)