/* List cluster files - the launcher Submits list_files.job.agi as one cluster job, waits for it and returns its output: every file under cluster:/ with its size and extension, and a size total per extension. Optional parameters, in the URL query or a form body: root folder to list, default cluster:/ limit maximum files in the list, default 5000 Keep list_files.job.agi in the same folder as this file. */ requirelib("cluster"); //Read a request parameter. A script run as a Serverless endpoint gets the //request through getPara() (URL query) and postPara() (form body); a WebApp //backend script gets its POST fields as global variables instead. function requestParam(name) { var v = null; if (typeof getPara === "function") { v = getPara(name); } if ((v === null || v === "") && typeof postPara === "function") { v = postPara(name); } if (v === null || v === "") { var globalScope = (function () { return this; })(); v = (typeof globalScope[name] === "string") ? globalScope[name] : ""; } return v === null ? "" : String(v); } function main() { if (!cluster.inCluster()) { return { error: "This node is not part of a cluster. Create or join one in System Settings > Cluster Settings." }; } var args = {}; if (requestParam("root") !== "") { args.root = requestParam("root"); } if (parseInt(requestParam("limit")) > 0) { args.limit = parseInt(requestParam("limit")); } var id = cluster.jobs.submit({ name: "List cluster files", script: "list_files.job.agi", args: args, timeout: 600 }); var rec; try { rec = cluster.jobs.wait(id, 120); } catch (e) { rec = cluster.jobs.status(id); return { job: id, status: rec.state.status, note: "Still running. Follow it on System Settings > Cluster Jobs." }; } if (rec.state.status !== "succeeded") { return { job: id, status: rec.state.status, error: rec.state.error || rec.state.reason || "" }; } return rec.state.output; } sendJSONResp(JSON.stringify(main()));