composer require neuron-php/jobsConfigure the application to use psr-4 autoloading in composer.json as the application relies on the autoloader to create the job objects.
Composer snippet:
"autoload": {
"psr-4": {
"App\\": "src/"
}├── composer.json
├── config
│ ├── config.yaml
│ └── schedule.yaml
├── src
│ └── Jobs
│ └── MyJob.php
└── vendor
The application configuration is loaded from config/config.yaml
Example config.yaml:
logging:
destination: \Neuron\Log\Destination\StdOut
format: \Neuron\Log\Format\PlainText
level: debug
system:
timezone: US/Eastern
base_path: /path/to/appThe job schedule is loaded from config/schedule.yaml
Example schedule.yaml:
schedule:
testJobWithArgs:
class: App\Jobs\ImportData
cron: "5 * * * *"
args:
doSomething: true
dontDoSomething: false
testJobWithOutArgs:
class: App\Jobs\SendReminderEmail
cron: "15 * * * *"- class: The class to instantiate and run.
- cron: The cron expression for the job schedule.
- args: An array of arguments to pass to the job.
Job classes must implement the Neuron\Jobs\IJob interface.
Example Job class:
namespace App\Jobs;
use Neuron\Jobs\IJob;
use Neuron\Log\Log;
class ExampleJob implements IJob
{
public function getName() : string
{
return 'TestJob';
}
public function run( array $Argv = [] ) : mixed
{
Log::debug( "TestJob::run( {$Argv['parameterName']} )" );
return true;
}
}./vendor/bin/neuron jobs:scheduleThis will run the scheduler in an infinite polling loop, polling every 60 seconds by default.
The polling interval seconds can be changed with the --interval option.
./vendor/bin/neuron jobs:schedule --interval 5The scheduler can also be run to perform a single poll of the schedule by using the --poll option.
For best results, the schedule should be run with the --poll option in a cron job and ran once per minute.
./vendor/bin/neuron jobs:schedule --pollYou can read more about the Neuron components at neuronphp.com