Jerry's WIKIJerry's WIKI
Overview
  • ๐Ÿž Web
  • ๐Ÿ“ Components
  • ๐Ÿ’ก Skills
  • ๐ŸŽฑ Specification
  • ๐Ÿ–ฅ Workflows
  • ๐Ÿ›  Tools
  • ๐ŸŒ๏ธ Linux
  • ๐Ÿ“ฆ Container
  • โ™จ๏ธ Language
Coffee
  • ็ฎ€ไฝ“ไธญๆ–‡
  • English
GitHub
Overview
  • ๐Ÿž Web
  • ๐Ÿ“ Components
  • ๐Ÿ’ก Skills
  • ๐ŸŽฑ Specification
  • ๐Ÿ–ฅ Workflows
  • ๐Ÿ›  Tools
  • ๐ŸŒ๏ธ Linux
  • ๐Ÿ“ฆ Container
  • โ™จ๏ธ Language
Coffee
  • ็ฎ€ไฝ“ไธญๆ–‡
  • English
GitHub
  • ๐Ÿ“ž Event Mechanism

    • Event Roles and Considerations
    • Code Example
  • โฐ Crontab
  • โ›“ Processes
  • ๐Ÿ“ File System
  • ๐Ÿ•“ Cache
  • ๐Ÿ“ฉ Queue

    • Queue Usage
    • Notes
  • ๐Ÿšฆ Signal
  • ๐Ÿ“ค GuzzleHttp
  • ๐Ÿ“‰ Rate Limiter
  • โŒ Exception
  • ๐Ÿ–จ Logs
  • ๐Ÿ“ก Command
  • ๐Ÿ” WebSocket

Processes

Index

  • Register Custom Processes
  • Override System Processes

ใ€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 the app 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';
}

Edit this page
Update At:
Contributor: ็”ฐๆœๅธ†
Prev
Crontab
Next
File System