code wiki / _hdl_build / nx_clock_sitecheck.nx
nx_clock_sitecheck.nx source
↩ module page · 30 lines · 2645 B
1// nx_clock_sitecheck.nx -- a NAS-APPROPRIATE clock job: probe the live site and record UP/DOWN. Unlike the local
2// jobs (publisher_drain etc. operate on local data), a health probe is data-independent and BELONGS on the
3// always-on server -- it is exactly the kind of periodic task the consolidated clock should run on the NAS. Uses
4// the sovereign fetch stack (our DNS + TLS-1.3). Writes the latest status (truncate, so the file is always the
5// current state) to knowledge/status/sitecheck.txt. license_tier: ORIGINAL expect_exit: 0
6import "nx_fetch_unit.nx"
7import "nx_syscalls.nx"
8
9func w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
10func cat(dst: *u8, o: i64, s: *u8) -> i64 { var x: i64=o; var i: i64=0; while s[i]!=(0 as u8){dst[x]=s[i];x=x+1;i=i+1} return x }
11func catnum(dst: *u8, o: i64, v: i64) -> i64 { var x: i64=o; var m: i64=v; if m<0{dst[x]=45 as u8;x=x+1;m=0-m} if m==0{dst[x]=48 as u8;return x+1} let t:*u8=sys_mmap(24); var k:i64=0; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var j:i64=0; while j<k{dst[x]=t[k-1-j];x=x+1;j=j+1} return x }
12
13func main(argc: i64, argv: *i64) -> i64 {
14 let body: *u8 = sys_mmap(FU_CAP)
15 let now: i64 = sys_now_realtime_sec() // epoch -> advances each run -> proves the clock dispatches us repeatedly
16 let path: *u8 = "sites/nishifamily/clock_health.txt" as *u8 // docroot (cwd=nishihost on the NAS) -> web-fetchable at /clock_health.txt
17 // HEARTBEAT FIRST -- robust: the file lands even if the network probe below crashes (NAS DNS/TLS differences).
18 // Its presence alone proves the clock DISPATCHED this job.
19 let hb: *u8 = sys_mmap(128); var ho: i64 = 0; ho = cat(hb, ho, "CLOCKJOB dispatched (probing) t=" as *u8); ho = catnum(hb, ho, now); hb[ho]=10 as u8; ho=ho+1; hb[ho]=0 as u8
20 let f1: i64 = sys_openat_wr(path, 0x1a4); if f1 >= 0 { sys_write(f1, hb, ho); sys_close(f1) }
21 // now the probe (may fail/crash on the NAS, but the heartbeat is already recorded)
22 let n: i64 = nx_fetch_staged("https://nishifamily.com/" as *u8, body, FU_CAP)
23 let st: *u8 = sys_mmap(256); var o: i64 = 0; o = cat(st, o, "CLOCKJOB SITECHECK nishifamily.com " as *u8)
24 if n > 0 { o = cat(st, o, "UP bytes=" as *u8); o = catnum(st, o, n) } else { o = cat(st, o, "DOWN rc=" as *u8); o = catnum(st, o, n) }
25 o = cat(st, o, " t=" as *u8); o = catnum(st, o, now); st[o] = 10 as u8; o = o + 1; st[o] = 0 as u8
26 let f2: i64 = sys_openat_wr(path, 0x1a4); if f2 >= 0 { sys_write(f2, st, o); sys_close(f2) } // overwrite with the probe result
27 w(st)
28 sys_exit(0)
29 return 0
30}