Trigger An API Call When The Server Restarts

I needed to extend express to send an API request to an internal endpoint whenever the application starts to reattach workers to bull queues. It doesn’t depend on nodemon events etc.

You can use it by creating a file myfile.js in /extensions/server_connect/routes

Use dotenv if you need API tokens or other credentials.

const request = require("request");
let requestSent = false;

exports.handler = function (app) {
  if (requestSent) return;
  requestSent = true;

  const options = {
    method: "POST",
    url: "myapp.url",
    headers: {
      Authorization: "Bearer YOUR_TOKEN",
    },
  };
  request(options, (error, response, body) => {
    if (error) throw new Error(error);
    console.log(body);
  });
};

4 Likes

Worth noting that you also have the following hooks available: exports.before and exports.after

These will run before and after user generated routes. In case you need it to be like that.

3 Likes

Thanks @JonL

Here are my first example (setting a variable), and the other two use the exports.before and exports.after hooks in case anybody needs them. I added dotenv as well.

Variable

require("dotenv").config();
const axios = require("axios");
let requestSent = false;

exports.handler = (app) => {
  if (requestSent) return;
  requestSent = true;

  const options = {
    method: "POST",
    url: "myapp.url",
    headers: {
      Authorization: `Bearer ${process.env.TOKEN}`,
    },
  };

  axios(options)
    .then((response) => {
      console.log(response.data);
    })
    .catch((error) => {
      console.error(error);
    });
};

Run before user-generated routes:

require("dotenv").config();
const axios = require("axios");

exports.before = (app) => {
    const options = {
        method: "POST",
        url: "myapp.url",
        headers: {
            Authorization: `Bearer ${process.env.TOKEN}`,
        },
    };

    axios(options)
        .then((response) => {
            console.log(response.data);
        })
        .catch((error) => {
            console.error(error);
        });
};

And after user-generated routes:

require("dotenv").config();
const axios = require("axios");

exports.after = (app) => {
    const options = {
        method: "POST",
        url: "myapp.url",
        headers: {
            Authorization: `Bearer ${process.env.TOKEN}`,
        },
    };

    axios(options)
        .then((response) => {
            console.log(response.data);
        })
        .catch((error) => {
            console.error(error);
        });
};

1 Like

Thanks for these Tobias,

I took your earlier example and modified to use axios since request is now deprecated.

In case anybody wants…

const axios = require('axios');
let requestSent = false;

let url = 'your.url';
exports.handler = function (app) {
    if (requestSent) return;
    requestSent = true;
    axios.get(url)
        .then(() => {
            done();
        }).catch((err) => {
            done(err)
        });


};
1 Like

Good point, thanks Ken,

I’ve updated my post above.

I’m using these to call an internal endpoint that runs a custom module that gets all Bull Queues from my Redis cluster that still have pending or waiting jobs. I coded that last night, and it works well. It might be something you can make use of as well.

1 Like

I’ve already put it into use, as I just happened to need it this morning. I have it deleting any previously stored websocket id’s that do not get removed on a server restart. It was the perfect fix!

1 Like