Getting Started with Heartbeat

Heartbeat is an automated way of checking whether a jobs, such as a backup jobs or an application scheduled work, is working properly.

Getting Started with Heartbeat

Introduction

We have a background job that makes a daily database backup of our primary PostgreSQL database. We want to get alerted if the background job doesn't run successfully.

Creating your first Heartbeat

Creating a heartbeat monitor:

  • Go to HeartbeatsCreate heartbeat.
  • Name your heartbeat — "Daily database backup job".
  • Change the Expect a heartbeat every selection to "1 day".
  • Optionally configure the On-call escalation settings.
  • Click Save heartbeat.
  • Copy the secret URL on the Heartbeat detail page; you will need it later.

image 4

This heartbeat will expect us to make a GET or POST request to the URL provided to us every day after the first request.

Set up a CRON and a background job

  1. Add a CRON task that will execute backup\db_full_backup.sh background job every day at midnight:

    Copied!

    0 0 * * * ruby /home/deploy/db_full_backup.sh >/dev/null 2>&1
    
  2. Include the curl call to the heartbeat URL at the end of your backup script:

    Copied!

    #!/usr/bin/env bash
    
    set -o errexit
    set -o xtrace
    
    date=`date "+%Y-%m-%d_%H:%M:%S"`
    file="/dumps/mydbname.$date.dump"
    
    time dokku postgres:export mydbname > "$file"
    
    /usr/local/bin/aws s3 cp "$file" s3://mydbname-dbbackups/
    
    rm "$file"
    
    # you get this URL in the Palzin Monitor dashboard
    curl "https://XXXXXXXX.palzin.app/api/heartbeat/XXXXXXXXXXXXXXXXXXXXXX"
    

What happens here is that the Heartbeat URL we've created above expects a GET or POST request every day since having made the first request.

If the code above fails, our background job won't make the request to the Heartbeat URL. In that case, the Heartbeat will alert the current on-call person and create an Incident.

Image

Last updated: 1 year ago

Want to get started with Palzin Monitor? We offer a no-strings-attached
15 days trial. No credit card required.

It takes less than a minutes to setup your first monitoring.