code wiki / _hdl_build / nx_dr_schedule.nx
nx_dr_schedule.nx source
↩ module page · 57 lines · 3686 B
1// nx_dr_schedule.nx -- CAP-DR-SCHEDULE: schedule-due policy for AUTOMATED periodic backup (the "don't rely on a
2// human to remember" S-class practice). sc_is_due(last_run, interval, now): due iff never-run (last_run==0) OR
3// now >= last_run+interval. check <schedule-conf> <now_epoch>: per job (name<TAB>interval_sec<TAB>last_run_epoch)
4// report DUE / waiting. Pure integer, deterministic (now passed in, so gateable + reproducible). Composes with the
5// supervisor / nx_cron for the actual trigger -- this is the DECISION, not the timer. license_tier: ORIGINAL
6import "nx_syscalls.nx"
7import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc)
8import "nx_site_lock_lib.nx"
9const K_MAGIC_262144: i64 = 262144
10
11func sc_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
12// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer
13// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the
14// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls).
15// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign.
16func sc_wn(v: i64) -> i64 { nxi_out(v); return 0 }
17func sc_read(path: *u8, out: *u8, cap: i64) -> i64 {
18 let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 - 1 }
19 var total: i64 = 0; var go: i64 = 1
20 while go == 1 { let nr: i64 = sys_read(fd, (out as i64 + total) as *u8, cap - total); if nr <= 0 { go = 0 } if nr > 0 { total = total + nr } if total >= cap { go = 0 } }
21 sys_close(fd); return total
22}
23func sc_atoi(s: *u8) -> i64 { var v: i64=0; var i: i64=0; while s[i]!=(0 as u8) { if s[i]>=(48 as u8) { if s[i]<=(57 as u8) { v=v*10+((s[i] as i64)-48) } } i=i+1 } return v }
24func sc_field(reg: *u8, ls: i64, le: i64, f: i64, out: *u8, cap: i64) -> i64 {
25 let fs: *i64=sys_mmap(8); let fe: *i64=sys_mmap(8)
26 if slk_field(reg, ls, le, f, fs, fe) == 1 { var o: i64=0; let l: i64=fe[0]-fs[0]; while o<l { if o<cap-1 { out[o]=reg[fs[0]+o] } o=o+1 } out[o]=0 as u8; return l }
27 out[0]=0 as u8; return 0
28}
29// due iff never run, or the interval has elapsed.
30func sc_is_due(last_run: i64, interval: i64, now: i64) -> i64 {
31 if last_run == 0 { return 1 }
32 if now >= last_run + interval { return 1 }
33 return 0
34}
35
36func main(argc: i64, argv: *i64) -> i64 {
37 if argc < 4 { sc_w("usage: nx_dr_schedule check <schedule-conf> <now_epoch>\n" as *u8); sys_exit(2); return 2 }
38 let conf: *u8 = argv[2] as *u8
39 let now: i64 = sc_atoi(argv[3] as *u8)
40 let reg: *u8 = sys_mmap(K_MAGIC_262144); let rn: i64 = sc_read(conf, reg, K_MAGIC_262144)
41 if rn <= 0 { sc_w(" FATAL: cannot read schedule conf\n" as *u8); sys_exit(3); return 3 }
42 sc_w("=== schedule check @ now=" as *u8); sc_wn(now); sc_w(" ===\n" as *u8)
43 let name: *u8=sys_mmap(256); let iv: *u8=sys_mmap(64); let lr: *u8=sys_mmap(64)
44 var ls: i64=0; var due: i64=0; var wait: i64=0
45 while ls < rn { let le: i64=slk_line_end(reg,rn,ls)
46 if le > ls { if reg[ls] != (35 as u8) {
47 if sc_field(reg, ls, le, 0, name, 256) > 0 {
48 sc_field(reg, ls, le, 1, iv, 64); sc_field(reg, ls, le, 2, lr, 64)
49 let d: i64 = sc_is_due(sc_atoi(lr), sc_atoi(iv), now)
50 sc_w(" "); if d==1 { sc_w("DUE " as *u8); due=due+1 } else { sc_w("waiting " as *u8); wait=wait+1 }
51 sc_w(name); sc_w(" interval=" as *u8); sc_w(iv); sc_w(" last_run=" as *u8); sc_w(lr); sc_w("\n" as *u8)
52 }
53 } }
54 ls = le + 1 }
55 sc_w(" ---- due=" as *u8); sc_wn(due); sc_w(" waiting=" as *u8); sc_wn(wait); sc_w("\n" as *u8)
56 sys_exit(0); return 0
57}