Add scheduled GitHub Action task
This commit is contained in:
97
scripts/daily-task.js
Normal file
97
scripts/daily-task.js
Normal file
@@ -0,0 +1,97 @@
|
||||
const DEFAULT_METHOD = 'GET';
|
||||
const DEFAULT_EXPECTED_STATUS = 200;
|
||||
const DEFAULT_RETRY_COUNT = 3;
|
||||
const DEFAULT_RETRY_DELAY_MS = 5000;
|
||||
|
||||
function readNumber(value, fallback) {
|
||||
if (!value) {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
const parsed = Number.parseInt(value, 10);
|
||||
return Number.isNaN(parsed) ? fallback : parsed;
|
||||
}
|
||||
|
||||
function sleep(ms) {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
function buildHeaders() {
|
||||
const headers = {
|
||||
'content-type': 'application/json',
|
||||
};
|
||||
|
||||
if (process.env.HEADERS_JSON) {
|
||||
const customHeaders = JSON.parse(process.env.HEADERS_JSON);
|
||||
Object.assign(headers, customHeaders);
|
||||
}
|
||||
|
||||
if (process.env.AUTH_TOKEN) {
|
||||
headers.authorization = `Bearer ${process.env.AUTH_TOKEN}`;
|
||||
}
|
||||
|
||||
if (!process.env.REQUEST_BODY) {
|
||||
delete headers['content-type'];
|
||||
}
|
||||
|
||||
return headers;
|
||||
}
|
||||
|
||||
async function runTask() {
|
||||
const targetUrl = process.env.TARGET_URL;
|
||||
if (!targetUrl) {
|
||||
throw new Error('Missing TARGET_URL secret.');
|
||||
}
|
||||
|
||||
const method = (process.env.HTTP_METHOD || DEFAULT_METHOD).toUpperCase();
|
||||
const expectedStatus = readNumber(process.env.EXPECTED_STATUS, DEFAULT_EXPECTED_STATUS);
|
||||
const retryCount = readNumber(process.env.RETRY_COUNT, DEFAULT_RETRY_COUNT);
|
||||
const retryDelayMs = readNumber(process.env.RETRY_DELAY_MS, DEFAULT_RETRY_DELAY_MS);
|
||||
const headers = buildHeaders();
|
||||
|
||||
const requestOptions = {
|
||||
method,
|
||||
headers,
|
||||
};
|
||||
|
||||
if (process.env.REQUEST_BODY) {
|
||||
requestOptions.body = process.env.REQUEST_BODY;
|
||||
}
|
||||
|
||||
for (let attempt = 1; attempt <= retryCount; attempt += 1) {
|
||||
console.log(`Attempt ${attempt}/${retryCount}: ${method} ${targetUrl}`);
|
||||
|
||||
try {
|
||||
const response = await fetch(targetUrl, requestOptions);
|
||||
const responseText = await response.text();
|
||||
|
||||
console.log(`Response status: ${response.status}`);
|
||||
if (responseText) {
|
||||
console.log(`Response body: ${responseText.slice(0, 1000)}`);
|
||||
}
|
||||
|
||||
if (response.status !== expectedStatus) {
|
||||
throw new Error(`Expected status ${expectedStatus}, received ${response.status}.`);
|
||||
}
|
||||
|
||||
console.log('Scheduled task completed successfully.');
|
||||
return;
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
console.error(`Attempt ${attempt} failed: ${message}`);
|
||||
|
||||
if (attempt === retryCount) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
console.log(`Waiting ${retryDelayMs}ms before retry.`);
|
||||
await sleep(retryDelayMs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
runTask().catch((error) => {
|
||||
console.error('Scheduled task failed.');
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user