-1

Let's say I'm listening whether a random event has happened. (Specifically I'm doing this via a webhook. After I receive a webhook, I execute code)

I do not want my webhook event to execute code more than once in a given timeframe, e.g. 30 minutes. However, I don't mean it in this way:

"Received webhook?" 
"Wait 30 minutes until you can listen to webhooks again." 

Somehow I'd like to split the 24hrs of the day into 48 parts of 30 minutes, and then the webhook can activate it's event only once in a given part. [for clarification: if it was 20mins then there would be 72 parts of 20mins]

For example: The code activates at 14:21. The next possible time I want it to activate would be at 14:30. Until 14:30 I don't want the code to activate more than it already did (once). Then again from 14:30 to 15:00, I want it to activate maximum once, and so on.

I don't necessarily need to divide the day like so, I'm just trying to explain what I need.

I hope I explained myself clearly.

martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

1

Let's say you have two functions enable and disable that let you enable or disable the webhook. Start by adding a signal handler for SIGALRM that enables the hook:

signal.signal(signal.SIGALRM, lambda: enable())

When the hook is triggered, you can do the following:

disable()
signal.alarm(1800)

This will disable your hook, and request that your program receive a SIGALRM signal in 30 minutes. When the signal arrives, your handler will call enable to enable your web hook.

chepner
  • 497,756
  • 71
  • 530
  • 681