summaryrefslogtreecommitdiff
path: root/tests/temporal/Workflow/CancelledNestedWorkflow.php
blob: 0c82f761c515c673195be04ca920226255c05ee1 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php

namespace Temporal\Tests\Workflow;

use Temporal\Exception\Failure\CanceledFailure;
use Temporal\Workflow;
use Temporal\Workflow\WorkflowMethod;

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

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

    #[WorkflowMethod(name: 'CancelledNestedWorkflow')]
    public function handler()
    {
        $this->status[] = 'begin';
        try {
            yield Workflow::newCancellationScope(
                function () {
                    $this->status[] = 'first scope';

                    $scope = Workflow::newCancellationScope(
                        function () {
                            $this->status[] = 'second scope';

                            try {
                                yield Workflow::timer(2);
                            } catch (CanceledFailure $e) {
                                $this->status[] = 'second scope cancelled';
                                throw $e;
                            }

                            $this->status[] = 'second scope done';
                        }
                    )->onCancel(
                        function () {
                            $this->status[] = 'close second scope';
                        }
                    );

                    try {
                        yield Workflow::timer(1);
                    } catch (CanceledFailure $e) {
                        $this->status[] = 'first scope cancelled';
                        throw $e;
                    }

                    $this->status[] = 'first scope done';

                    yield $scope;
                }
            )->onCancel(
                function () {
                    $this->status[] = 'close first scope';
                }
            );
        } catch (CanceledFailure $e) {
            $this->status[] = 'close process';

            return 'CANCELLED';
        }

        return 'OK';
    }
}