iPhoney
iPhoney

Reputation: 781

Delay message processing and delete before processing

I need this ability to send push notifications for an action in a mobile app but wait for the user to undo the action until say 10 seconds.

Is it possible to delay the processing of a message published in a topic by 10 seconds ? And then (sometimes, if user does undo) delete the message before 10 seconds, if it doesn't need to be processed ?

Upvotes: 20

Views: 19791

Answers (3)

Gabriel Kohen
Gabriel Kohen

Reputation: 4286

Depends on if you write the subscribers as well or not:

You have control over your subscriber's code:

  1. In your PubSub messages add a timestamp for when you want that message to be processed.
  2. In your clients (subscribers), have logic to acknowledge the message only if the timestamp to process the message is reached.
  3. PubSub will retry delivering the message until it's acknowledged (or 10 days)

If you don't have control over your subscriber you can have a my-topic and my-delayed-topic. Folks can publish to the former topic and that topic will have only one subscriber which you will implement:

  1. Publish message as before to my-topic.
  2. You will have a subscriber for that topic that can do the same throttling as shown above.
  3. If the time for that message has reached your handler will publish/relay that message to my-delayed-topic.

You can also implement the logic above with task-queue+pubsub-topic instead of pubsub-topic+pubsub-topic.

Upvotes: 27

Ehsan
Ehsan

Reputation: 92

If architecturally possible at all, you could use Cloud Tasks. This API has the following features that might suit your usecase:

  • You can schedule the delivery of the message (task)
  • You can delete the tasks from the queue (before they are executed)

Assuming that your client has a storage for some task Ids:

  1. Create a task with schedule_time set to 10s in the future.
  2. Store the task name in memory (you can either assign a name to the task at creation time, or use the automatically generated ID returned from the create response).
  3. If user undid the job, then call DeleteTask.

Upvotes: 3

David Xia
David Xia

Reputation: 5453

Just wanted to share that I noticed Pub/Sub supports retry policies 1 that are GA as of 2020-06-16 2.

If the acknowledgement deadline expires or a subscriber responds with a negative acknowledgement, Pub/Sub can send the message again using exponential backoff.

If the retry policy isn't set, Pub/Sub resends the message as soon as the acknowledgement deadline expires or a subscriber responds with a negative acknowledgement.

If the maximum backoff duration is set, the default minimum backoff duration is 10 seconds. If the minimum backoff duration is set, the default maximum backoff duration is 600 seconds.

The longest backoff duration that you can specify is 600 seconds.

Upvotes: 2

Related Questions