EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
agentHeartbeat.js
Go to the documentation of this file.
1/**
2 * @file agentHeartbeat.js
3 * @module routes/agentHeartbeat
4 * @description Agent heartbeat endpoint for health monitoring and command queue polling.
5 * Agents periodically call this endpoint to report status and retrieve pending commands.
6 * @requires express
7 * @author RMM-PSA Development Team
8 * @copyright 2026 RMM-PSA Platform
9 * @license Proprietary
10 */
11
12/**
13 * @apiDefine AgentHeartbeat Agent Heartbeat
14 * Agent health checks and command polling
15 */
16
17const express = require('express');
18const router = express.Router();
19
20// Dummy in-memory command queue for demo purposes
21const agentCommands = {};
22
23// Example: Add a command for an agent (in real use, this would be DB/Redis)
24/**
25 *
26 * @param agent_uuid
27 * @param command
28 */
29function addCommand(agent_uuid, command) {
30 if (!agentCommands[agent_uuid]) agentCommands[agent_uuid] = [];
31 agentCommands[agent_uuid].push(command);
32}
33
34// Example: Get and clear commands for agent
35/**
36 *
37 * @param agent_uuid
38 */
39function getPendingCommandsForAgent(agent_uuid) {
40 const cmds = agentCommands[agent_uuid] || [];
41 agentCommands[agent_uuid] = [];
42 return cmds;
43}
44
45/**
46 * @api {post} /api/agent/heartbeat/:agent_uuid Agent heartbeat
47 * @apiName AgentHeartbeat
48 * @apiGroup AgentHeartbeat
49 * @apiDescription Agent health check and command polling endpoint. Agents call periodically
50 * to report online status and retrieve pending commands from in-memory queue.
51 * Returns commands and clears queue for agent. TODO: Implement agent authentication.
52 * @apiParam {string} agent_uuid Agent UUID from URL path
53 * @apiSuccess {object[]} commands Array of pending commands for agent
54 * @apiSuccess {string} commands.command_id Command ID
55 * @apiSuccess {string} commands.type Command type (e.g., "run_script", "update")
56 * @apiSuccess {object} commands.payload Command parameters
57 * @apiExample {curl} Example:
58 * curl -X POST http://localhost:3000/api/agent/heartbeat/abc-123-uuid \\\
59 * -H "Content-Type: application/json" \\\
60 * -d '{}'
61 * @apiSuccessExample {json} Success-Response:
62 * HTTP/1.1 200 OK
63 * {
64 * "commands": [
65 * {
66 * "command_id": "cmd-001",
67 * "type": "run_script",
68 * "payload": {
69 * "script_id": 42,
70 * "script_content": "Get-Service"
71 * }
72 * }
73 * ]
74 * }
75 * @apiSuccessExample {json} No-Commands-Response:
76 * HTTP/1.1 200 OK
77 * {
78 * "commands": []
79 * }
80 */
81router.post('/heartbeat/:agent_uuid', async (req, res) => {
82 const agent_uuid = req.params.agent_uuid;
83 // TODO: Authenticate agent (JWT, etc.)
84 // Get and clear pending commands
85 const commands = getPendingCommandsForAgent(agent_uuid);
86 res.json({ commands });
87});
88
89module.exports = router;