summaryrefslogtreecommitdiff
path: root/tests/temporal/Workflow/CancelledSingleScopeWorkflow.php
blob: 5fe8d3d86555b3992b6e8cc8319793e2134cf93b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php

namespace Temporal\Tests\Workflow;

use Temporal\Activity\ActivityOptions;
use Temporal\Exception\Failure\CanceledFailure;
use Temporal\Workflow;
use Temporal\Workflow\WorkflowMethod;
use Temporal\Tests\Activity\SimpleActivity;

#[Workflow\WorkflowInterface]
class CancelledSingleScopeWorkflow
{
    private array $status = [];

    #[Workflow\QueryMethod(name: 'getStatus')]
    public function getStatus(): array
    {
        return $this->status;
    }

    #[WorkflowMethod(name: 'CancelledSingleScopeWorkflow')]
    public function handler()
    {
        $simple = Workflow::newActivityStub(
            SimpleActivity::class,
            ActivityOptions::new()
                ->withStartToCloseTimeout(5)
        );

        $this->status[] = 'start';
        try {
            yield Workflow::newCancellationScope(
                function () use ($simple) {
                    try {
                        $this->status[] = 'in scope';
                        yield $simple->slow('1');
                    } catch (CanceledFailure $e) {
                        // after process is complete, do not use for business logic
                        $this->status[] = 'captured in scope';
                        throw $e;
                    }
                }
            )->onCancel(
                function () {
                    $this->status[] = 'on cancel';
                }
            );
        } catch (CanceledFailure $e) {
            $this->status[] = 'captured in process';
        }

        return 'OK';
    }
}