Custom Background Job Runner for Laravel by Gcobani mkontwanaCustom Background Job Runner for Laravel by Gcobani mkontwana

Custom Background Job Runner for Laravel

Gcobani mkontwana

Gcobani mkontwana

Custom Background Job Runner for Laravel

Overview

This project implements a custom background job runner in Laravel without using Laravel’s built-in queue system. It is designed to showcase scalability, security, and robust error handling while executing jobs as background processes. Key features include:
CLI Job Execution: Execute PHP classes as background jobs using a CLI script (run-job.php). The CLI accepts the job's class name, method, and a comma-separated list of parameters.
Global Helper Function: The runBackgroundJob() function (defined in app/Helpers/helpers.php) lets you trigger background jobs from anywhere in your Laravel application on both Unix-based systems and Windows.
Logging & Retry Mechanism: Job executions are logged with timestamps, including details such as class, method, and status (success/failure). Failures trigger a configurable retry mechanism (with bonus exception type–based retry logic).
Security: Only pre-approved jobs and methods are executed. Allowed classes and methods are configured in config/background-jobs.php, and all inputs are sanitized.
Advanced Features:
Chained Jobs: Optionally, if a job class implements a nextJob() method, a subsequent job is triggered automatically.
Job Delays & Priorities: Configure per-job delays and assign priorities.
Web-Based Dashboard with Cancellation: A simple dashboard is available to view job logs, active job status, and error logs, with the ability to cancel running jobs.

Project Structure

The project is organized as follows:
Background-job-runner/ ├── app/ │ ├── Jobs/ │ │ ├── MyJob.php # Sample job that executes successfully (may chain to another job) │ │ ├── FailingJob.php # Sample job that always throws an exception for testing retry logic │ │ └── AnotherJob.php # Sample chained job (triggered via MyJob's nextJob() method) │ └── Helpers/ │ └── helpers.php # Contains the global helper function runBackgroundJob() ├── config/ │ └── background-jobs.php # Defines allowed job classes, allowed methods, retry attempts, and delays ├── public/ ├── resources/ │ └── views/ │ └── jobs_dashboard.blade.php # Web dashboard to display job logs and manage active jobs ├── routes/ │ └── web.php # Routes for the dashboard and job cancellation ├── storage/ │ └── logs/ │ ├── background_jobs.log # Stores log messages for job execution │ ├── background_jobs_errors.log # Stores log messages for job errors │ └── active_jobs.json # Tracks currently active jobs (for cancellation) ├── run-job.php # CLI script to execute jobs (with retry logic, logging, and chained jobs) ├── cancel-job.php # (Optional) CLI tool to cancel a job using its unique job ID ├── README.md # Project documentation (this file) └── composer.json # Composer configuration and autoload settings.

Installation and Setup

Clone the Repository:
Install Dependencies: Make sure you have Composer installed, then run:
composer install
#Configure Environment cp .env.example .env php artisan key:generate
#Verify Configuration Files Ensure the configuration file config/background-jobs.php exists and is properly set up.
#Register Global Helper Function In your composer.json ensure the autoload section includes your global helper file:
"autoload": { "psr-4": { "App\": "app/" }, "files": [ "app/Helpers/helpers.php" ]
} Then update autoloader like this below composer dump-autoload
#Running a Job via CLI To execute a background job using the CLI, use: php run-job.php "App\Jobs\MyJob" process "param1,param2"
#Web-based Dashboard Start the Laravel Server: by running this command; php artisan serve
#Access Dashbaord Try to navigate to this routing http://127.0.0.1:8000/dashboard
#Canceling a Running Job Via the Dashboard: In the active jobs table on the /dashboard page, click the Cancel button next to a running job to terminate it.
Via CLI option buy doing this as below php cancel-job.php
#Testing The project includes PHPUnit test cases to ensure that jobs execute properly, retry on failure, and log errors appropriately.
Run the Test Suite ./vendor/bin/phpunit --filter=JobRunnerTest
This README meets the challenge’s specifications by explaining how to use the global helper, configure allowed jobs and retry settings, run jobs via the CLI, view the web dashboard, and cancel jobs.
Like this project

Posted May 14, 2025

Developed a custom background job runner in Laravel with advanced features.

Likes

0

Views

1

Timeline

Apr 14, 2025 - May 1, 2025

Clients

PayEasy