Glanceway Glanceway
Todas as fontes

Service Health Check

Desenvolvimento v1.0.0

Monitor service endpoints and get notified when they go down

@codytseng #monitoring #health-check

Configuração

Nome Chave Tipo Obrigatório Padrão Descrição
Service URLs URLS list Sim URLs to monitor (e.g. https://example.com)

Código-fonte

version: 1.0.0
name: Service Health Check
description: Monitor service endpoints and get notified when they go down
author: codytseng
author_url: https://github.com/codytseng
category: Developer
tags:
  - monitoring
  - health-check

config:
  - key: URLS
    name: Service URLs
    type: list
    required: true
    description: URLs to monitor (e.g. https://example.com)
module.exports = async (api) => {
  const urls = api.config.get("URLS");

  async function fetchData() {
    if (!urls || urls.length === 0) return;

    const previousDownUrls = new Set(
      JSON.parse(api.storage.get("downUrls") ?? `[]`),
    );
    const currentDownUrls = new Set();

    await Promise.allSettled(
      urls.map(async (url) => {
        const res = await api.fetch(url);
        if (!res.ok) {
          currentDownUrls.add(url);
          if (!previousDownUrls.has(url)) {
            api.emit([
              {
                id: `${url}-${Date.now()}`,
                title: `Failed to fetch ${url}`,
                subtitle: `HTTP ${res.status}, error: ${res.error || "unknown"}`,
                url,
                timestamp: Date.now(),
              },
            ]);
          }
        }
      }),
    );

    // Store the current down URLs for the next refresh
    api.storage.set("downUrls", JSON.stringify(Array.from(currentDownUrls)));
  }

  await fetchData();

  return {
    refresh: fetchData,
  };
};