Heartbeat Monitoring in AdonisJS Using Acumen Logs

Heartbeat monitoring is a simple yet powerful technique to ensure your processes and scheduled tasks are running as expected. By sending periodic “heartbeat” signals to a monitoring service like AcumenLogs, you can track the health of your application’s jobs. If the heartbeat signal fails to arrive within a predefined time frame, Acumen Logs triggers an alert to notify you of potential issues.

In this blog post, we’ll walk you through integrating heartbeat monitoring into an AdonisJS application, specifically within the scheduler, to help you monitor the reliability of your scheduled tasks.

What is Heartbeat Monitoring?

Heartbeat monitoring involves sending a recurring signal (or "pulse") from your application to a monitoring endpoint. This signal indicates that your process is running as expected. If no signal is received within a specified interval, the monitoring service, such as Acumen Logs, will notify you of the issue. This ensures proactive monitoring of background jobs and scheduled tasks.

How to Create a Heartbeat Check in Acumen Logs

  1. 1. Log in to Acumen Logs Dashboard: Start by logging in to your Acumen Logs account.

  1. 2. Navigate to Heartbeat: On the side navigation bar, select the Heartbeat section.

  2. 3. Create a New Heartbeat: Click the Create button, give your heartbeat check an identifiable name (e.g., "Daily Report Task"), and press Save.

  3. 4. Copy the Endpoint: After saving, you’ll be redirected to your Heartbeat dashboard, where you’ll find a unique endpoint for your heartbeat check. Copy this endpoint for use in your application.

Adding a Heartbeat to AdonisJS Scheduler

AdonisJS provides a powerful task scheduler through its @adonisjs/scheduler package. Let’s integrate heartbeat monitoring into a scheduled task.

Step 1: Install the Scheduler Package

If you haven’t already installed the scheduler package, add it to your project:

npm install @adonisjs/scheduler

Step 2: Configure the Scheduler

Ensure the scheduler is enabled in your start/kernel.ts file. It should look like this:

import Scheduler from '@adonisjs/scheduler/build/Scheduler';
import HttpContext from '@ioc:Adonis/Core/HttpContext';

export const kernel = {
  jobs: [Scheduler],
};

Step 3: Add the Heartbeat to Your Task

Inside your scheduler task file (e.g., app/Tasks/HeartbeatTask.ts), use the unique endpoint from AcumenLogs:

import { BaseTask } from '@adonisjs/scheduler/build';
import axios from 'axios';

export default class HeartbeatTask extends BaseTask {
  public static get schedule() {
    return '0 * * * *'; // Runs hourly
  }

  public static get useLock() {
    return false; // Disable locking for this example
  }

  public async handle() {
    try {
      // Send a heartbeat signal to Acumen Logs
      await axios.post('https://acumenlogs.com/heartbeat/xxxx-xxxx-xxxx-xxxx/pulse');
      console.log('Heartbeat sent successfully');
    } catch (error) {
      console.error('Failed to send heartbeat:', error.message);
    }
  }
}

This code sends a POST request to the unique Acumen Logs endpoint, confirming that your scheduler is running as expected.

Step 4: Run the Scheduler

Start the scheduler to ensure your tasks run as expected:

node ace scheduler:run

Once the scheduler is running, you’ll see your heartbeat pulses registered in the Acumen Logs dashboard.