File size: 2,197 Bytes
ddccbe9
 
 
 
 
 
0fab20e
 
 
 
 
 
 
 
 
ddccbe9
 
0fab20e
ddccbe9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0fab20e
 
 
 
 
 
 
 
ddccbe9
 
 
0fab20e
ddccbe9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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" }
      });
    }
  }
};