What is the best practice for a polling activity?

It depends on how frequently you want to poll.

  • For infrequent polls (every minute or slower) use the server side retry. Specify a RetryPolicy (or RetryOptions for Java) when invoking the activity. In the RetryPolicy specify an exponential coefficient of 1 and an initial interval of the poll frequency. Then fail the activity in case the polled resource is not ready and the server is going to retry it up to the specified retry policy expiration interval. So what you are doing is absolutely OK. I’ve updated the issue to suppress logging in this case.

  • For very frequent polls of every few seconds or faster the solution is to implement the polling inside an activity implementation as a loop that polls and then sleeps for the poll interval. To ensure that the polling activity is restarted in a timely manner in case of worker failure/restart the activity has to heartbeat on every iteration. Note that heartbeating only works if the HeartbeatTimeout that is shorter than the StartToClose timeout is specified. Use an appropriate RetryPolicy for the restarts of such failed activity.

  • In a rare case when the polling requires a periodic execution of a sequence of activities or activity arguments should change between retries a child workflow can be used. The trick is that a parent is not aware about a child calling continue as new. It only gets notified when a child completes or fails. So if a child executes the sequence of activities in a loop and calls continue as new periodically the parent is not affected until the child completes.

13 Likes
Design Patterns
When should one use activity heartbeating and when is it okay not to use it?
Activity retries without exception
Workflow scheduling strategies go-SDK
Deployment of Temporal
Long polling inside workflows or activities
Repeat an activity with time limit
What is the best way to implement long time retry for downstream service unavailble, increasing activity timeout or cronjob workflow?
Brainstorming the best way to create a heartbeat sensor
Numerous Infinite-Loop Tasks Per Worker
Best practive for notifying completion of async activities
Using A Recursive call for Worker Polling
Is it a good practice to call an activity in a loop?
Managing long running, polling, transactions
Implement task scheduler asynchronously from workflow
Scheduler and infinite loop
Chilling Temporal Anti-Patterns 👻
Getting error in workflow when waiting for 2 hours and querying at the same time
How to trigger activity retry from activity interceptor
Is there anything comparable to airflow deferrable operator and triggers in temporal
Best practice for handling lost external events and resuming paused workflows
Activity completes long after ScheduleToCloseTimeout
Possible to Implement Finite State Machine using Workflows?
Deployment of Temporal
Limit task execution concurrency based on task param
Can I attach a listener to workflow execution?
How can I cancel workflow execution within activityInboundInterceptors?
Waiting for external conditions before running workflow
Postgres Table as Event Source
Continue-as-new overhead, suitability of using Temporal
Is Temporal inherently not built for long polling?
Continue as new when reaching 5 000 events limit
Get result of another workflow from an activity in using typeScript
Wait for condition between retries
Go samples - recovery example exception
Need help with A non-deterministic error has caused the Workflow Task to hang