Code Monkey home page Code Monkey logo

scheduled-healthcheck's Introduction

โœจ Trigger.dev Scheduled Healthcheck

This repo contains a Scheduled Trigger that will run every 5 minutes and send a Slack message if a website url returns a non-200 response:

new Trigger({
  // Give your Trigger a stable ID
  id: "scheduled-healthcheck",
  name: "Scheduled Healthcheck",
  // Trigger every 5 minutes, see https://docs.trigger.dev/triggers/scheduled
  on: scheduleEvent({
    rateOf: { minutes: 5 },
  }),
  // The run functions gets called every 5 minutes
  async run(event, ctx) {
    // Fetch the website using generic fetch, see https://docs.trigger.dev/functions/fetch
    const response = await ctx.fetch("๐Ÿง‘โ€โš•๏ธ", WEBSITE_URL, {
      method: "GET",
      retry: {
        enabled: false, // Disable retrying
      },
    });

    // If the website is down (or we are in a test run), send a message to Slack
    if (!response.ok || ctx.isTest) {
      // Post a message to Slack, see https://docs.trigger.dev/integrations/apis/slack/actions/post-message
      await slack.postMessage("๐Ÿค’", {
        channelName: "health-checks",
        text: `๐Ÿ˜ญ ${WEBSITE_URL} is down!`,
      });
    }
  },
}).listen();

๐Ÿ”ง Install

You can easily create a new project interactively based on this template by running:

npx create-trigger@latest scheduled-healthcheck
# or
yarn create trigger scheduled-healthcheck
# or
pnpm create trigger@latest scheduled-healthcheck

Follow the instructions in the CLI to get up and running locally in <30s.

โœ๏ธ Customize

  • You can set the website url by defining the WEBSITE_URL environment variable.
  • Customize the Slack message and channel name.
  • Update the frequency (you can go as frequent as once per minute)

Be sure to check out more over on our docs

๐Ÿงช Test it

After successfully running this template locally, head over to your Trigger.dev Dashboard and you should see your newly created workflow:

workflow list

Click on the workflow in the list and then click on the "Test your workflow" button, where you will be able to simulate a scheduled event:

workflow test

Since we setup our workflow to always post to Slack in a test run, after clicking "Run Test" you'll see a message requiring Slack authentication to continue:

connect to slack

Once you authenticate your Slack workspace, the run will pickup where it left off and post the message:

workflow run complete

Head over to slack to see your newly created message:

slack message

scheduled-healthcheck's People

Contributors

ericallam avatar

Stargazers

Taylor D. Edmiston avatar

Watchers

 avatar James Ritchie avatar Matt Aitken avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.