summaryrefslogtreecommitdiff
path: root/tests/src/Workflow/SagaWorkflow.php
blob: e47c02032a30825bce024fe6c1510436de112048 (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
<?php

/**
 * This file is part of Temporal package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Temporal\Tests\Workflow;

use Temporal\Activity\ActivityOptions;
use Temporal\Common\RetryOptions;
use Temporal\Tests\Activity\SimpleActivity;
use Temporal\Workflow;

#[Workflow\WorkflowInterface]
class SagaWorkflow
{
    #[Workflow\WorkflowMethod(name: 'SagaWorkflow')]
    public function run()
    {
        $simple = Workflow::newActivityStub(
            SimpleActivity::class,
            ActivityOptions::new()
                ->withStartToCloseTimeout(60)
                ->withRetryOptions(RetryOptions::new()->withMaximumAttempts(1))
        );

        $saga = new Workflow\Saga();
        $saga->setParallelCompensation(true);

        try {
            yield $simple->echo('test');
            $saga->addCompensation(
                function () use ($simple) {
                    yield $simple->echo('compensate echo');
                }
            );

            yield $simple->lower('TEST');
            $saga->addCompensation(
                function () use ($simple) {
                    yield $simple->lower('COMPENSATE LOWER');
                }
            );

            yield $simple->fail();
        } catch (\Throwable $e) {
            yield $saga->compensate();
            throw $e;
        }
    }
}