Skip to content

Patterns

Arie Roos edited this page Nov 13, 2020 · 4 revisions

Patterns

Scheduled Tasks

In many systems there are some things that must happen on a schedule. Let's say you want to poll an external API every 5 minutes for important data. If your poll failed for some reason, it would be better if you knew.

Here is an example of some JavaScript that does exactly that:

const axios = require('axios')
const heartbeatsh = require('heartbeat-sh')

const heartbeats = new heartbeatsh({subdomain: "example"})
const timeoutLength = 60 * 5 // seconds

setInterval(() => {
    axios({
        method: 'get',
        url: 'https://sampleapis.com/avatar/api/info',
    }).then(r => {
            console.log(r.data)
            heartbeats.SendHeartbeat("api-request", timeoutLength + 1, timeoutLength * 3)
        }
    )
}, timeoutLength * 1000)

This script hits the Avatar sample API every five minutes and prints out the result. In order to make sure it succeeded, we also send a heartbeat named api-request to example.heartbeat.sh. The heartbeat will go into warning state if we missed one poll to the external API, but if we missed the poll three times in a row, it will go into error state.

Daily Checks

Coming Soon!

Performance

Coming Soon!

Unclosed Resources

Coming Soon!

Clone this wiki locally