ResetTimer

ResetTimer is a microservice for a resetting countdown timer. The timers are polled with at-most-once signalling when the timer expires. The source code is available on github.

The service is built on AWS Lambda, API Gateway, and DynamoDB using update expressions. It’s intended as a demonstration of DynamoDB atomic update triggers, but is available for use. It’s based on polling, so it’s not always the best solution, but it can be useful in the right circumstance.

Why?

Two reasons:

  1. To experiment with Lambda update expressions
  2. To solve a specific problem – coordinate a set of N app servers that are polling for a signal to execute a job by any one of the app servers. Jobs run every N seconds.

Create a timer

curl -XGET curl -XGET 'https://api.prohakr.com/resettimer/YOURTIMERNAME/10'
{
  "secondsRemaining": 10,
  "timerName": "YOURTIMERNAME"
}

That’s it. You don’t need an API token, to create an account, or log in. The first HTTP GET you issue will create the timer. If you don’t want somebody else to use your sequence name, pick something unguessable like a GUID

Get status of the timer

curl -XGET 'https://api.prohakr.com/resettimer/YOURTIMERNAME'
{
  "secondsRemaining": 5,
  "timerName": "YOURTIMERNAME"
}

sleep 3

curl -XGET 'https://api.prohakr.com/resettimer/YOURTIMERNAME'
{
  "secondsRemaining": 2,
  "timerName": "YOURTIMERNAME"
}

sleep 2

curl -XGET 'https://api.prohakr.com/resettimer/YOURTIMERNAME'
{
  "secondsRemaining": 0,
  "timerName": "YOURTIMERNAME"
}

curl -XGET 'https://api.prohakr.com/resettimer/YOURTIMERNAME'
{
  "secondsRemaining": 10,
  "timerName": "YOURTIMERNAME"
}

The timer silently ticks down in the background. Each HTTP GET will return the current status of the timer.

When the timer hits zero, it pauses and does not reset. The next HTTP GET on the timer will receive JSON like this:

{
  "secondsRemaining": 0,
  "timerName": "YOURTIMERNAME"
}

At that point the timer resets in the background and starts counting down again. Only one HTTP request will recieve the zero value. If a timer expires and multiple request are sent for status, one request will see the zero time, and the remaining requests will see the timer counting down again.

Concurrency

You can issue as many concurrent requests to the timer as you like. Each request will receive a status of the timer at the time of the request.

However, as mentioned above, when the timer hits zero a special condition is triggered. The first HTTP request received will be returned the zero value for the timer and will trigger the timer to be reset. This will happen for exactly one http request. All other requests will receive non-zero values for the status of the timer.

Set the sequence to a specific number

A timer resets to the same period under which it was created. If you created a timer with a 30 second interval, then the timer will count down from 30 seconds each time it resets.

However, should you need to change the period of the timer, it’s easy to do so. Simply re-issue the create command and the timer will reset to the new frequency.

curl -XGET curl -XGET 'https://api.prohakr.com/resettimer/YOURTIMERNAME/45'
{
  "secondsRemaining": 45,
  "timerName": "YOURTIMERNAME"
}

Guarantees

Absolutely none. I’m running this for free, as a service. But anything could change: I reserve the right to fully manage the system, restrict it’s use, or change it’s terms of use. There could be flaws, the system can go down, or might go away.

On the other hand, perhaps ResetTimer will take over the world – in which case I will change the terms and introduce a pricing model.

If you’d like more than absolutely no guarantee, contact me. Perhaps we could work something out.