Renamed the healthchecks task -> integrations

This commit is contained in:
Mathias Wagner
2023-05-22 22:08:11 +02:00
parent 09eedb850a
commit 149b3bb06d

View File

@@ -1,6 +1,5 @@
const axios = require("axios");
const config = require('../controller/config');
const schedule = require('node-schedule');
const {triggerEvent} = require("../controller/integrations");
let currentState = "ping";
let job;
@@ -10,27 +9,9 @@ module.exports.setState = (state = "ping") => {
currentState = state;
}
// Get the current health checks url from the config & format it
const getHealthChecksUrl = async () => {
let url = (await config.get("healthChecksUrl")).value.toString();
if (url.includes("<uuid>")) return null;
if (url.endsWith("/")) url = url.substring(0, url.length - 1);
return url;
}
// Send a ping to the provided healthcheck server
module.exports.sendPing = async (path, message) => {
let url = await getHealthChecksUrl();
if (url == null) return;
if (path) url += "/" + path;
try {
const response = await axios.post(url, message, {headers: {"user-agent": "MySpeed/HealthAgent"}});
if (response.data.includes("not found")) await config.resetDefault("healthChecksUrl");
} catch (e) {
console.error("Could not send ping: " + e.message);
}
module.exports.sendPing = async (type, message) => {
await triggerEvent("minutePassed", {type, message});
}
// Sends a ping only if the ping state is active
@@ -40,12 +21,16 @@ module.exports.sendCurrent = async () => {
// Sends an error to the healthcheck server
module.exports.sendError = async (error = "Unknown error") => {
await this.sendPing("fail", error);
await triggerEvent("testFailed", error);
}
// Sends a 'running' ping to the healthcheck server
module.exports.sendRunning = async () => {
await this.sendPing("start");
await triggerEvent("testStarted");
}
module.exports.sendFinished = async (data) => {
await triggerEvent("testFinished", data);
}
// Starts a timer which sends a ping every minute