NL
apps/cryo-studio / assets / demo / freeze_worker.php · commit 76aa68d
<?php// A queue worker frozen in the middle of its loop, for the Stack screen of Cryo Studio. Run with ext-cryo://   php -d extension=cryo -d cryo.key=demo-key freeze_worker.php// cryo_freeze() writes worker.cryf (signed, zlib) and the script goes on (mode continue).class Job{    public function __construct(public int $id, public string $kind, public array $payload = []) {}}class JobQueue{    /** @var list<Job> */    private array $jobs = [];    public int $done = 0;    public function push(Job $job): void    {        $this->jobs[] = $job;    }    public function process(string $snapshot, int $stopAt): int    {        $total = 0;        foreach ($this->jobs as $i => $job) {            $total += count($job->payload);            $this->done++;            if ($i === $stopAt) {                cryo_freeze($snapshot, ['mode' => 'continue', 'compress' => 'zlib']);            }        }        return $total;    }}function runWorker(string $snapshot): int{    $queue = new JobQueue();    foreach (['mail', 'resize', 'invoice', 'mail', 'export'] as $n => $kind) {        $queue->push(new Job($n + 1, $kind, array_fill(0, $n + 2, "$kind data")));    }    $started = 'batch-42';    return $queue->process($snapshot, 2);}$config = ['retries' => 3, 'queue' => 'orders'];echo 'processed ', runWorker(__DIR__ . '/worker.cryf'), " items\n";