Processes
Index
ใGuidelinesใ
For the processes provided by the system (framework), if no inheritance or modification is required, please declare them in the configuration files. We should follow these rules:
- 1ใIf a component provides the corresponding process and no customization is needed, declare it in
config/autoload/process.php
. - 2ใFor custom components (business needs), create a
Process
directory under theapp
directory, and place all custom process files within it. Custom processes should be declared using annotations.
In summary: system (framework) processes should be declared in configuration files; custom processes should be declared using annotations.
ใTipใ
- Throwing exceptions will cause the process to exit, and it will be restarted by the Server. It is recommended to use
try-catch-finally
in custom processes and throw custom exceptions. - Cron jobs are essentially custom processes, but they handle exceptions differently (you need to listen for exception events).
Register Custom Processes
<?php
declare(strict_types=1);
namespace App\Process;
use App\Lib\Log\Log;
use Exception;
use Hyperf\Coroutine\Coroutine;
use Hyperf\Process\AbstractProcess;
use Hyperf\Process\Annotation\Process;
use Hyperf\Process\ProcessManager;
use Throwable;
#[Process(
nums: 1, // ่ฟ็จๆฐ็ฎ
name: 'ConsumerProcess',
redirectStdinStdout: false,
pipeType: 2,
enableCoroutine: true // ่ฟ็จๅ
ๆฏๅฆๅฏ็จๅ็จ
)]
class ConsumerProcess extends AbstractProcess
{
public function handle(): void
{
$index = 0;
try {
while (ProcessManager::isRunning()) {
Coroutine::sleep(1);
++$index;
// ๆจกๆๅผๅธธ
if ($index === 10) {
throw new Exception('ไธปๅจๆๅบๅผๅธธ');
}
}
} catch (Throwable $e) {
Log::stdout()->error("ConsumerProcess ๅผๅธธ่ขซๆ่ท: {$e->getMessage()}");
} finally {
Log::stdout()->warning('ConsumerProcess ่ฟ็จๅฐ่ขซๆ่ตท !!!');
}
}
// ๆฏๅฆ้็ๆๅกไธ่ตทๅฏๅจ
public function isEnable($server): bool
{
return \Hyperf\Support\env('APP_ENV', 'dev') === 'dev';
}
}
Override System Processes
ใNoteใ
Some system processes may require us to manage or modify the logic ourselves, in which case we need to inherit the corresponding consumer process and then register it ourselves. If no changes are needed, it can simply be configured in config/autoload/process.php
. I personally prefer to manage it myself. ๐
AsyncQueue Consumer Process
<?php
declare(strict_types=1);
namespace App\Process\OverloadProcess;
use App\Constants\ConstCode;
use Hyperf\AsyncQueue\Process\ConsumerProcess;
use Hyperf\Process\Annotation\Process;
#[Process(
nums: 4, // ๆถ่ดน่
่ฟ็จๆฐ
name: 'AsyncQueueProcess', // ้ๅๅ็งฐ
redirectStdinStdout: false, // ้ๅฎๅ่ชๅฎไน่ฟ็จ็ๆ ๅ่พๅ
ฅๅ่พๅบ
enableCoroutine: true, // ๆฏๅฆๅฏ็จๅ็จ
)]
class AsyncQueueProcess extends ConsumerProcess
{
// ่ฟ้็้ๅๅ็งฐ่ฏทๅ้
็ฝฎๆไปถๅฏนๅบ็้ๅๅ็งฐไฟๆไธ่ด
protected string $queue = ConstCode::NORMAL_QUEUE_NAME;
}
Parallel Concurrency of 1
<?php
declare(strict_types=1);
namespace App\Process\OverloadProcess;
use App\Constants\ConstCode;
use Hyperf\AsyncQueue\Process\ConsumerProcess;
use Hyperf\Process\Annotation\Process;
#[Process(
nums: 1, // ๆถ่ดน่
่ฟ็จๆฐ
name: 'LockQueueProcess', // ้ๅๅ็งฐ
redirectStdinStdout: false, // ้ๅฎๅ่ชๅฎไน่ฟ็จ็ๆ ๅ่พๅ
ฅๅ่พๅบ
enableCoroutine: true, // ๆฏๅฆๅฏ็จๅ็จ
)]
class LockQueueProcess extends ConsumerProcess
{
// ่ฟ้็้ๅๅ็งฐ่ฏทๅ้
็ฝฎๆไปถๅฏนๅบ็้ๅๅ็งฐไฟๆไธ่ด
protected string $queue = ConstCode::LOCK_QUEUE_NAME;
}
Scheduled Task Consumer Process
<?php
declare(strict_types=1);
namespace App\Process\OverloadProcess;
use Hyperf\Crontab\Process\CrontabDispatcherProcess;
use Hyperf\Process\Annotation\Process;
#[Process(
nums: 1, // ่ฟ็จๆฐ็ฎ
name: 'SchedulerProcess', // ่ฟ็จๅ็งฐ
redirectStdinStdout: false, // ้ๅฎๅ่ชๅฎไน่ฟ็จ็ๆ ๅ่พๅ
ฅๅ่พๅบ
pipeType: 2, // ็ฎก้็ฑปๅ
enableCoroutine: true // ่ฟ็จๅ
ๆฏๅฆๅฏ็จๅ็จ
)]
class SchedulerProcess extends CrontabDispatcherProcess
{
public string $name = 'scheduler-process';
}