PHP code example of fof / horizon

1. Go to this page and download the library: Download fof/horizon 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/ */

    

fof / horizon example snippets




use FoF\Redis\Extend\Redis;

return [
    // Basic Redis configuration
    new Redis([
        'host'     => '127.0.0.1',
        'password' => null,
        'port'     => 6379,
        'database' => 0,
    ]),

    // ... other extenders
];

return [
    (new Redis([
        'host'     => '127.0.0.1',
        'password' => null,
        'port'     => 6379,
    ]))
    ->useDatabaseWith('cache', 1)
    ->useDatabaseWith('queue', 2)
    ->useDatabaseWith('session', 3),
];

return [
    (new Redis([
        'host' => '127.0.0.1',
        'port' => 6379,
    ]))
    ->disable(['cache']), // Only use Redis for queue and sessions
];

return [
    new Redis([
        'host' => '127.0.0.1',
        'port' => 26379,
        'options' => [
            'replication' => 'sentinel',
            'service'     => 'mymaster',
        ],
    ]),
];

// config.php
'horizon' => [
    'email_concurrency' => 4,
],

// extend.php
(new FoF\Horizon\Extend\Horizon)->emailConcurrency(4),

'horizon' => [
    'supervisors' => [
        // Give the realtime tier more workers than the default.
        'realtime' => [
            'processes' => 3,
        ],
        // A heavy tier for bulk work.
        'long' => [
            'queues' => ['exports', 'gdpr'],
        ],
    ],
    'memory_limit' => 256,
],



use FoF\Horizon\Extend\Horizon;

return [
    (new Horizon)
        // Route a job onto a queue. This sets the job's queue AND registers it
        // so the dashboard and per-queue pause know about it. If the job class
        // isn't installed it's skipped, so optional dependencies are safe.
        //
        // Pass a third argument to also attach that queue to a supervisor in one
        // go — e.g. put a translation job's queue on the always-on `standard`
        // pool. Without it, the job is routed but no worker consumes the queue
        // until a supervisor serves it.
        ->routeJob(\Your\Extension\Jobs\TranslateJob::class, 'translate', 'standard')
        ->routeJob(\Your\Extension\Jobs\RealtimeJob::class, 'realtime')

        // Routing an abstract base (or interface) covers every job that extends
        // (or implements) it — route the base once and all its concrete
        // subclasses inherit the queue. A more specific route on a subclass
        // still wins over the base.
        ->routeJob(\Your\Extension\Jobs\AbstractExportJob::class, 'long')

        // Add extra queues under an existing profile without touching its other
        // settings — handy for base images layering their own queues on.
        ->queueOn('long', 'exports', 'gdpr')

        // Override a profile's knobs. Recognised keys map onto the profile;
        // anything else (nice, balanceMaxShift, retry_after, …) passes straight
        // through to Horizon.
        ->supervisor('realtime', ['processes' => 12])

        // Define a brand-new profile.
        ->supervisor('media', ['queues' => ['thumbnails'], 'timeout' => 300, 'processes' => 2]),
];

return [
    // Put three extra queues on the `long` profile alongside whatever it
    // already serves. Repeated/duplicate names are de-duplicated.
    (new FoF\Horizon\Extend\Horizon)
        ->queueOn('long', 'exports', 'gdpr', 'migration'),
];

(new FoF\Horizon\Extend\Horizon)->useRawConfig([
    'supervisor-1' => [
        'connection'   => 'redis',
        'queue'        => ['default'],
        'balance'      => 'auto',
        'maxProcesses' => 10,
        // ...any Laravel Horizon supervisor options
    ],
]);

'horizon' => [
    'memory_limit' => 256, // MB — master supervisor
    'supervisors' => [
        'standard' => [
            'memory' => 256, // MB — per worker in this profile
        ],
    ],
],

'horizon' => [
    'supervisors' => [
        'standard' => [
            'maxJobs' => 1000, // restart the worker after X jobs
            'maxTime' => 3600, // restart the worker after X seconds
        ],
    ],
],
bash
composer ache:clear
bash
php flarum horizon
ini
[Unit]
Description=Flarum Horizon
After=network.target

[Service]
Type=simple
User=www-data
WorkingDirectory=/var/www/flarum
ExecStart=/usr/bin/php /var/www/flarum/flarum horizon
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
bash
php flarum horizon
bash
php flarum horizon:list
bash
php flarum horizon:pause
bash
php flarum horizon:continue
bash
php flarum horizon:pause-supervisor supervisor-1
bash
php flarum horizon:continue-supervisor supervisor-1
bash
php flarum horizon:status
bash
php flarum horizon:supervisor-status supervisor-1
bash
php flarum horizon:terminate
bash
php flarum horizon:clear redis --queue=default
bash
php flarum horizon:purge
bash
php flarum horizon:forget {job-id}
bash
php flarum horizon:clear-metrics
bash
php flarum queue:restart
bash
php flarum horizon:terminate
# Your process monitor (Supervisor/systemd/container runtime) restarts it
bash
composer update fof/horizon
php flarum migrate
php flarum cache:clear
php flarum horizon:terminate