Last active
June 24, 2022 17:15
-
-
Save seregazhuk/2bf1ca03bcc8567c31dccb71c94acb5b to your computer and use it in GitHub Desktop.
Workflow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
use Carbon\CarbonInterval; | |
use Temporal\Activity\ActivityOptions; | |
use Temporal\Common\RetryOptions; | |
use Temporal\Internal\Workflow\ActivityProxy; | |
use Temporal\Workflow; | |
use Temporal\Workflow\WorkflowMethod; | |
#[Workflow\WorkflowInterface] | |
class SignalWorkflow | |
{ | |
private ActivityProxy $pollStatus; | |
private bool $received = false; | |
private bool $statusPolled = false; | |
public function __construct() | |
{ | |
$this->pollStatus = Workflow::newActivityStub( | |
SimpleActivity::class, | |
ActivityOptions::new() | |
->withStartToCloseTimeout(CarbonInterval::seconds(50)) | |
->withRetryOptions(RetryOptions::new()->withMaximumAttempts(0)) | |
); | |
} | |
#[WorkflowMethod] | |
public function start() | |
{ | |
$pollingStatusScope = Workflow::async( | |
function () { | |
while (!$result = yield $this->pollStatus->pollStatus()) { | |
yield Workflow::timer(CarbonInterval::seconds(5)); | |
} | |
$this->statusPolled = $result; | |
} | |
); | |
Workflow::async( | |
function () use ($pollingStatusScope) { | |
yield Workflow::await(fn() => $this->received || $this->statusPolled); | |
$pollingStatusScope->cancel(); | |
} | |
); | |
try { | |
yield $pollingStatusScope; | |
} catch (CanceledFailure $exception) { | |
} | |
return $this->statusPolled; | |
} | |
#[Workflow\SignalMethod] | |
public function setReceived(bool $received): void | |
{ | |
$this->received = $received; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment