PHP code example of hyperf / xxl-job-incubator
1. Go to this page and download the library: Download hyperf/xxl-job-incubator library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hyperf / xxl-job-incubator example snippets
return [
// 是否启用
'enable' => env('XXL_JOB_ENABLE', true),
// XXL-JOB 服务端地址
'admin_address' => env('XXL_JOB_ADMIN_ADDRESS', 'http://127.0.0.1:8080/xxl-job-admin'),
// 对应的 AppName
'app_name' => env('XXL_JOB_APP_NAME', 'xxl-job-demo'),
// 访问凭证
'access_token' => env('XXL_JOB_ACCESS_TOKEN', ''),
// 执行器心跳间隔(秒)
'heartbeat' => env('XXL_JOB_HEARTBEAT', 30),
// 日志文件保存天数 [选填]: 过期日志自动清理, 限制值大于等于3时生效; 否则, 如-1, 关闭自动清理功能;
'log_retention_days' => -1,
// 执行器 HTTP Server 相关配置
'executor_server' => [
// HTTP Server 路由前缀
'prefix_url' => env('XXL_JOB_PREFIX_URL', 'php-xxl-job'),
],
];
namespace App\Job;
use Hyperf\Di\Annotation\Inject;
use Hyperf\XxlJob\Annotation\XxlJob;
use Hyperf\XxlJob\Handler\AbstractJobHandler;
use Hyperf\XxlJob\Logger\JobExecutorLoggerInterface;
use Hyperf\XxlJob\Requests\RunRequest;
#[XxlJob("demoJobClassHandler")]
class DemoJobClass extends AbstractJobHandler
{
#[Inject]
protected JobExecutorLoggerInterface $jobExecutorLogger;
/**
* 执行任务
*/
public function execute(RunRequest $request): void
{
// 获取参数
$params = $request->getExecutorParams();
// 获取 LogId
$logId = $request->getLogId();
$this->jobExecutorLogger->info('demoJobClassHandler...');
$this->jobExecutorLogger->info('params:' . $params);
for ($i = 1; $i < 5; ++$i) {
sleep(2);
$this->jobExecutorLogger->info($i);
$this->jobExecutorLogger->info('logId:' . $logId);
$this->jobExecutorLogger->info('params:' . $params);
}
}
}
use Hyperf\XxlJob\Annotation\XxlJob;
class Foo {
#[XxlJob('demoJobHandler')]
public function demoJobHandler(){}
}
namespace App\Job;
use Hyperf\Di\Annotation\Inject;
use Hyperf\XxlJob\Annotation\XxlJob;
use Hyperf\XxlJob\Logger\JobExecutorLoggerInterface;
use Hyperf\XxlJob\Requests\RunRequest;
class DemoJob
{
#[Inject]
protected JobExecutorLoggerInterface $jobExecutorLogger;
/**
* 1.任务示例.
*/
#[XxlJob('demoJobHandler')]
public function demoJobHandler(RunRequest $request)
{
//获取参数
$params = $request->getExecutorParams();
//获取logId
$logId = $request->getLogId();
$this->jobExecutorLogger->info('params:' . $params);
for ($i = 1; $i < 5; ++$i) {
sleep(2);
$this->jobExecutorLogger->info($i);
$this->jobExecutorLogger->info('logId:' . $logId);
$this->jobExecutorLogger->info('params:' . $params);
}
}
/**
* 2、分片广播任务
*/
#[XxlJob('shardingJobHandler')]
public function shardingJobHandler(RunRequest $request)
{
// 分片参数
$shardIndex = $request->getBroadcastIndex();
$shardTotal = $request->getBroadcastTotal();
$this->jobExecutorLogger->info(sprintf('分片参数:当前分片序号 = %d, 总分片数 = %d', $shardIndex, $shardTotal));
// 业务逻辑
for ($i = 0; $i < $shardTotal; ++$i) {
if ($i == $shardIndex) {
$this->jobExecutorLogger->info('第 %d 片, 命中分片开始处理', $i);
} else {
$this->jobExecutorLogger->info('第 %d 片, 忽略', $i);
}
}
}
/**
* 3、执行命令.
*/
#[XxlJob('commandJobHandler')]
public function commandJobHandler(RunRequest $request)
{
//获取参数
//例子:php -v
$command = $request->getExecutorParams();
var_dump($command);
$result = System::exec($command);
$message = str_replace("\n", '<br>', $result['output']);
$this->jobExecutorLogger->info($message);
}
/**
* 4、param任务
* 参数示例:
* "url: http://www.baidu.com\n" +
* "method: get".
*/
#[XxlJob('paramJobHandler')]
public function paramJobHandler(RunRequest $request)
{
$param = $request->getExecutorParams();
$array = explode(PHP_EOL, $param);
/** array(2) {
[0]=>
string(25) "url: http://www.baidu.com"
[1]=>
string(11) "method: get"
}
*/
var_dump($param, $array);
}
/**
* 5、任务示例:任务初始化与销毁时,支持自定义相关逻辑.
*/
#[XxlJob(value: 'demoJob', init: 'initMethod', destroy: 'destroyMethod')]
public function demoJob()
{
$this->jobExecutorLogger->info('demoJob run...');
}
public function initMethod()
{
$this->jobExecutorLogger->info('initMethod');
}
public function destroyMethod()
{
$this->jobExecutorLogger->info('destroyMethod');
}
}
bash
php bin/hyperf.php vendor:publish hyperf/xxl-job-incubator