| 123456789101112131415161718192021222324252627 |
- /*---
- title: Includes and background jobs
- group: Request modes
- summary: Share code with includes() and start a detached script with execd().
- setup: {"files":[{"path":"web:/Playground/lib/format.js","text":"// Shared helpers, loaded with includes(\"lib/format.js\").\nfunction formatBytes(n){\n var units = [\"B\", \"KB\", \"MB\", \"GB\"];\n var i = 0;\n while (n >= 1024 && i < units.length - 1){ n = n / 1024; i++; }\n return (Math.round(n * 10) / 10) + \" \" + units[i];\n}\n"},{"path":"web:/Playground/jobs/cleanup.agi","text":"requirelib(\"filelib\");\nvar log = \"Cleanup ran for \" + USERNAME + \" with payload \" + PARENT_PAYLOAD + \"\\n\";\nif (!filelib.fileExists(\"user:/Logs\")) { filelib.mkdir(\"user:/Logs\"); }\nfilelib.writeFile(\"user:/Logs/cleanup.log\", log);\nconsole.log(\"cleanup.agi finished\");\n"}]}
- ---*/
- /*
- includes() loads another script of the same WebApp into this one.
- execd() starts a script in the background: the response does not wait
- for it, and it gets PARENT_DETACHED and PARENT_PAYLOAD.
- */
- requirelib("filelib");
- includes("lib/format.js");
- var files = filelib.walk("user:/", "file");
- var total = 0;
- for (var i = 0; i < files.length; i++) {
- total += filelib.filesize(files[i]);
- }
- execd("jobs/cleanup.agi", JSON.stringify({ files: files.length }));
- sendJSONResp({
- files: files.length,
- used: formatBytes(total),
- background: "jobs/cleanup.agi started; see Changes for user:/Logs/cleanup.log"
- });
|