Skip to content

Instantly share code, notes, and snippets.

@seregazhuk
Last active June 24, 2022 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seregazhuk/2bf1ca03bcc8567c31dccb71c94acb5b to your computer and use it in GitHub Desktop.
Save seregazhuk/2bf1ca03bcc8567c31dccb71c94acb5b to your computer and use it in GitHub Desktop.
Workflow
<?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