PHP code example of ojbaeza / station

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

    

ojbaeza / station example snippets


// All of these are tracked by Station
ProcessOrderJob::dispatch($order);
ProcessOrderJob::dispatch($order)->onQueue('high');
ProcessOrderJob::dispatch($order)->delay(now()->addMinutes(5));

use Station\Facades\Station;

Station::job(new ProcessOrderJob($order))
    ->onQueue('high')
    ->delay(now()->addMinutes(5))
    ->tags(['orders', 'payments'])
    ->dispatch();

use Illuminate\Bus\Batchable;

class ProcessOrderJob implements ShouldQueue
{
    use Batchable, Dispatchable, InteractsWithQueue, Queueable;

    public function handle(): void
    {
        if ($this->batch()?->cancelled()) {
            return;
        }

        // Do work...
    }
}

use Station\Facades\Batch;

$batch = Batch::create(
    jobs: [
        new ProcessOrderJob($order1),
        new ProcessOrderJob($order2),
        new ProcessOrderJob($order3),
    ],
    name: 'Daily Orders',
    allowedFailures: 2,  // Allow up to 2 jobs to fail (integer, not boolean)
);

// Check progress
$batch = Batch::find($batch->id);
$batch->progress();    // Percentage (0-100)

// Operations
Batch::cancel($batch->id);         // Cancel remaining jobs
Batch::retryFailed($batch->id);    // Retry failed jobs

use Station\Core\Workflow;

Workflow::create('order-pipeline')
    ->add('validate', new ValidateOrderJob($order))
    ->add('payment', new ProcessPaymentJob($order), ['validate'])
    ->add('inventory', new ReserveInventoryJob($order), ['validate'])
    ->add('ship', new ShipOrderJob($order), ['payment', 'inventory'])
    ->onQueue('high')
    ->dispatch();

use Station\Facades\Workflow;

// Define a reusable workflow
Workflow::define('payment-processing')
    ->addStep('validate', ValidatePaymentJob::class)
    ->addStep('charge', ChargeCardJob::class, ['validate'])
    ->addConditionalStep('notify',
        SendNotificationJob::class,
        fn($context) => $context['charge_successful'] ?? false,
        ['charge']
    )
    ->timeout(3600);

// Run synchronously
$instance = Workflow::run('payment-processing', [
    'amount' => 99.99,
    'user_id' => 42,
]);

// Or run asynchronously (recommended for production)
$instance = Workflow::runAsync('payment-processing', [
    'amount' => 99.99,
    'user_id' => 42,
]);

use Station\Contracts\Checkpointable;

class ImportUsersJob implements ShouldQueue, Checkpointable
{
    private int $lastId = 0;

    public function handle(): void
    {
        User::where('id', '>', $this->lastId)->orderBy('id')
            ->chunk(100, function ($users) {
                foreach ($users as $user) {
                    $this->processUser($user);
                }
                $this->lastId = $users->last()->id;
            });
    }

    public function checkpoint(): array
    {
        return ['last_id' => $this->lastId];
    }

    public function restore(array $data): void
    {
        $this->lastId = $data['last_id'] ?? 0;
    }

    public function hasMoreWork(): bool
    {
        return User::where('id', '>', $this->lastId)->exists();
    }
}

// config/station.php
'dashboard' => [
    'authorization' => 'viewStation', // Gate name to check
],

Gate::define('viewStation', function ($user) {
    return in_array($user->email, ['[email protected]']);
});

// config/station.php (key sections)
return [
    'default' => 'rabbitmq',

    'connections' => [
        'rabbitmq' => [
            'driver' => 'rabbitmq',
            'hosts' => [[ 'host' => env('RABBITMQ_HOST', 'localhost'), /* ... */ ]],
        ],
    ],

    'supervisors' => [
        'default' => [
            'queues' => ['default'],
            'processes' => 2,
            'timeout' => 60,
            'memory' => 128,
        ],
    ],

    'dashboard' => ['enabled' => true, 'path' => 'station', 'middleware' => ['web', 'auth']],
    'recovery' => ['enabled' => true, 'stuck_job_timeout' => 900, 'auto_resume' => true],
    'checkpoints' => ['enabled' => true, 'storage' => 'database'],
];
bash
composer vendor:publish --provider="Station\StationServiceProvider"
php artisan migrate
php artisan station:install
bash
php artisan station:work
bash
php artisan station:recover
php artisan station:recover --dry-run        # Preview without acting
php artisan station:recover --strategy=graceful --workflows