Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Not a Bug] await doesn't interrupts on the activity with error #399

Open
pfy-oleksii-storozhylov opened this issue Feb 15, 2024 · 3 comments
Labels
Tests Update tests or testing tools

Comments

@pfy-oleksii-storozhylov
Copy link
Contributor

pfy-oleksii-storozhylov commented Feb 15, 2024

What are you really trying to do?

I want to use await function to ensure that activities are completed with or without an errors

Describe the bug

When any activity is completed with error, await function doesn't continue workflow immediately, it waits for the full interval

Minimal Reproduction

            foreach ($anticipationPromises as $promise) {
                $promise
                    ->then(function() use (&$index) {
                        $index++;
                        file_put_contents('/srv/app/ddd.log', time()." success $index\n", FILE_APPEND);
                    })
                    ->otherwise(function() use (&$index) {
                        $index++;
                        file_put_contents('/srv/app/ddd.log', time()." error $index\n", FILE_APPEND);
                    });
            }

            file_put_contents('/srv/app/ddd.log', time(). " start awaiting $index\n", FILE_APPEND);

            yield \Temporal\Workflow::awaitWithTimeout(
                 CarbonInterval::seconds(60),
                \React\Promise\all($anticipationPromises)
             );

            file_put_contents('/srv/app/ddd.log', time(). " complete $index\n", FILE_APPEND);

Result when 2 activities have no errors looks good

1708030014 start awaiting 0
1708030016 success 1
1708030018 success 2
1708030018 complete 2

Result when one of the activities has an error looks good (completion takes 60 seconds)

1708030094 start awaiting 0
1708030096 error 1
1708030098 success 2
1708030154 complete 2

Environment/Versions

  • temporal/sdk 2.7+

Additional context

@pfy-oleksii-storozhylov pfy-oleksii-storozhylov added the Bug Something isn't working label Feb 15, 2024
@pfy-oleksii-storozhylov
Copy link
Contributor Author

workarround with a counter and deferred promise could be used

$index = 0;

            $deferred = new Deferred();
            $deferred->promise()
                ->then(function () use (&$index) {
                    file_put_contents('/srv/app/ddd.log', time() . " deferred resolved $index\n", FILE_APPEND);
                });

            foreach ($anticipationPromises as $promise) {
                $promise
                    ->then(function () use (&$index, $deferred) {
                        $index++;
                        file_put_contents('/srv/app/ddd.log', time() . " success $index\n", FILE_APPEND);

                        if ($index >= 2) {
                            $deferred->resolve(true);
                        }
                    })
                    ->otherwise(function () use (&$index, $deferred) {
                        $index++;
                        file_put_contents('/srv/app/ddd.log', time() . " error $index\n", FILE_APPEND);

                        if ($index >= 2) {
                            $deferred->resolve(true);
                        }
                    });
            }

            file_put_contents('/srv/app/ddd.log', time() . " start awaiting $index\n", FILE_APPEND);

            yield \Temporal\Workflow::awaitWithTimeout(
                CarbonInterval::seconds(60),
                $deferred->promise()
            );

And the result looks good

1708030906 start awaiting 0
1708030908 error 1
1708030910 success 2
1708030910 deferred resolved 2
1708030910 complete 2

@pfy-oleksii-storozhylov
Copy link
Contributor Author

pfy-oleksii-storozhylov commented Feb 16, 2024

Seems not an issue, all promise is rejected if any of promises is rejected, so the solution here is

            yield \Temporal\Workflow::awaitWithTimeout(
                CarbonInterval::seconds(60),
                \Temporal\Promise::all(array_map(
                    fn (ExtendedPromiseInterface $promise) => $promise->otherwise(fn () => false),
                    $promises
                ))
            );

@wolfy-j
Copy link
Collaborator

wolfy-j commented Feb 16, 2024

I would like to keep this issue open for our records, so we can check the await behavior in combination with promise chaining.

@wolfy-j wolfy-j added Tests Update tests or testing tools and removed Bug Something isn't working labels Feb 16, 2024
@wolfy-j wolfy-j changed the title [Bug] await doesn't interrupts on the activity with error [Not a Bug] await doesn't interrupts on the activity with error Aug 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Tests Update tests or testing tools
Projects
None yet
Development

No branches or pull requests

2 participants