tina4components / tina4jobsmodule
This is a package that will allows you to add a redis jobs queue to any tin4 project
Installs: 33
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Type:composer-plugin
Requires
- composer-plugin-api: ^2.0
- ext-redis: *
- tina4stack/tina4php: ^2.0
- tina4stack/tina4php-sqlite3: ^2.0
This package is auto-updated.
Last update: 2024-11-06 14:41:43 UTC
README
This package uses a redis server to manage the jobs and to run them on a schedule. It is designed to be a simple way to manage jobs and to be able to run them on a schedule.
Installation
To install the package you can use the following command
composer require tina4components/tina4jobsmodule
Usage
There are two variables that you need to set in your .env file
REDIS_HOST: <host address to your redis server> REDIS_PORT: <ports to your redis server: Default usually on redis is :6379>
When creating a job you need to create a class that implements the Tina4Job
interface and implement the handle()
method.
The handle()
method is the method that will be called when the job is run.
The following is an example of a job that writes a user to a file, All job classes need to implement the Tina4Job interface.
<?php namespace Tina4Jobs; use Tina4Jobs\Tina4Job; class TestJob implements Tina4Job { use Tina4Queueable; // This is a trait that is used to make the job queueable private $user; public function __construct($payload = []) { $this->user = ["name" => "John", "surname" => "Doe", ...$payload]; } public function handle(): void { file_put_contents("test.txt", json_encode($this->user) . "\n\n", FILE_APPEND); } }
Creating a job is as simple as creating a new instance of Tina4Jobs and adding it to the jobs list.
$jobs = Tina4Jobs\Tina4JobFactory::createQueueDriver(); $jobs->addJob(new \Tina4Jobs\TestJob( [ "surname" => "Smith", "age" => 30, "country" => "South Africa" ] ), "queue_name");
To run the jobs there is a jobs service in the Tina4Jobs module that you can use to run the jobs.
composer start-jobs