quantforge-miner / cloudflare_worker.js
VaNam56's picture
Update cloudflare_worker.js
d2a19a3 verified
Raw
History Blame Contribute Delete
2.2 kB
export default {
// Cron trigger entry point
async scheduled(event, env, ctx) {
const spaceUrl = env.SPACE_HEALTH_URL || "https://vanam56-quantforge-miner.hf.space/health";
console.log(`[Scheduled] Pinging Hugging Face Space at: ${spaceUrl}`);
// Prepare headers (including authentication if space is private)
const headers = {
"User-Agent": "QuantForge-Miner-Pinger/1.0",
"Cache-Control": "no-cache"
};
if (env.HF_TOKEN) {
headers["Authorization"] = `Bearer ${env.HF_TOKEN}`;
}
try {
const response = await fetch(spaceUrl, {
headers: headers
});
if (response.ok) {
console.log(`[Scheduled] Ping successful! HTTP Status: ${response.status}`);
} else {
console.error(`[Scheduled] Ping returned non-OK status: ${response.status} ${response.statusText}`);
}
} catch (error) {
console.error(`[Scheduled] Network error during ping: ${error.message}`);
}
},
// HTTP request entry point (for manual testing/triggers)
async fetch(request, env, ctx) {
const spaceUrl = env.SPACE_HEALTH_URL || "https://vanam56-quantforge-miner.hf.space/health";
console.log(`[HTTP] Manual trigger received. Pinging: ${spaceUrl}`);
const headers = {
"User-Agent": "QuantForge-Miner-Pinger/1.0",
"Cache-Control": "no-cache"
};
if (env.HF_TOKEN) {
headers["Authorization"] = `Bearer ${env.HF_TOKEN}`;
}
try {
const startTime = Date.now();
const response = await fetch(spaceUrl, {
headers: headers
});
const duration = Date.now() - startTime;
const responseText = await response.text();
const statusText = `Ping target: ${spaceUrl}\nHTTP Status: ${response.status}\nDuration: ${duration}ms\nResponse Body: ${responseText}`;
return new Response(statusText, {
headers: {
"content-type": "text/plain",
"Access-Control-Allow-Origin": "*"
}
});
} catch (error) {
return new Response(`Error pinging Space: ${error.message}`, {
status: 500,
headers: { "content-type": "text/plain" }
});
}
}
};