Skip to content

Commit

Permalink
Support custom scripts to determine whether it is up/down based on th…
Browse files Browse the repository at this point in the history
…e response JSON
  • Loading branch information
YinDongFang committed Jan 26, 2025
1 parent 3024c20 commit f5634ab
Show file tree
Hide file tree
Showing 7 changed files with 265 additions and 5 deletions.
15 changes: 15 additions & 0 deletions Client/src/Pages/Uptime/CreateUptime/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const CreateMonitor = () => {
url: "",
name: "",
type: "http",
testScripts: "",
notifications: [],
interval: 1,
});
Expand All @@ -90,6 +91,7 @@ const CreateMonitor = () => {
port: monitor.type === "port" ? monitor.port : undefined,
name: monitor.name === "" ? monitor.url : monitor.name,
type: monitor.type,
testScripts: monitor.testScripts,
interval: monitor.interval * MS_PER_MINUTE,
};

Expand All @@ -107,6 +109,8 @@ const CreateMonitor = () => {
return;
}

if (!form.testScripts) delete form.testScripts;

if (monitor.type === "http") {
// const checkEndpointAction = await dispatch(
// checkEndpointResolution({ authToken, monitorURL: form.url })
Expand Down Expand Up @@ -280,6 +284,17 @@ const CreateMonitor = () => {
error={errors["name"] ? true : false}
helperText={errors["name"]}
/>
<TextInput
type="text"
id="testScripts"
label="testScripts"
isOptional={true}
placeholder={monitorTypeMaps[monitor.type].namePlaceholder || ""}
value={monitor.testScripts}
onChange={(event) => handleChange(event, "testScripts")}
error={errors["name"] ? true : false}
helperText={errors["name"]}
/>
</Stack>
</ConfigBox>
<ConfigBox>
Expand Down
3 changes: 3 additions & 0 deletions Server/db/models/Monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const MonitorSchema = mongoose.Schema(
"distributed_http",
],
},
testScripts: {
type: String,
},
url: {
type: String,
required: true,
Expand Down
158 changes: 156 additions & 2 deletions Server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"handlebars": "^4.7.8",
"helmet": "^8.0.0",
"ioredis": "^5.4.2",
"isolated-vm": "^5.0.3",
"joi": "^17.13.1",
"jsonwebtoken": "9.0.2",
"mailersend": "^2.2.0",
Expand Down
36 changes: 33 additions & 3 deletions Server/service/networkService.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { errorMessages, successMessages } from "../utils/messages.js";
import runInSandbox, { TimeoutError, UserScriptError } from "../utils/sandbox.js";
const SERVICE_NAME = "NetworkService";

/**
Expand Down Expand Up @@ -135,6 +136,7 @@ class NetworkService {
type: job.data.type,
responseTime,
payload: response?.data,
code: response.status,
};

if (error) {
Expand All @@ -144,9 +146,37 @@ class NetworkService {
httpResponse.message = this.http.STATUS_CODES[code] || "Network Error";
return httpResponse;
}
httpResponse.status = true;
httpResponse.code = response.status;
httpResponse.message = this.http.STATUS_CODES[response.status];

const testScripts = job.data.testScripts;

if (testScripts) {
try {
const status = await runInSandbox(testScripts, response?.data);
httpResponse.status = !!status;
httpResponse.message = this.http.STATUS_CODES[response.status];
} catch (error) {
httpResponse.status = false;

if (error instanceof TimeoutError) {
httpResponse.message = "User Scripts Timeout";
} else if (error instanceof UserScriptError) {
httpResponse.message = "User Scripts Runtime Error";
} else {
httpResponse.message = "System Error";
}

this.logger.error({
message: error.message,
service: SERVICE_NAME,
method: "requestHttp",
stack: error.stack,
});
}
} else {
httpResponse.status = true;
httpResponse.message = this.http.STATUS_CODES[response.status];
}

return httpResponse;
} catch (error) {
error.service = this.SERVICE_NAME;
Expand Down
Loading

0 comments on commit f5634ab

Please sign in to comment.