list_files.agi 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. List cluster files - the launcher
  3. Submits list_files.job.agi as one cluster job, waits for it and returns
  4. its output: every file under cluster:/ with its size and extension, and
  5. a size total per extension.
  6. Optional parameters, in the URL query or a form body:
  7. root folder to list, default cluster:/
  8. limit maximum files in the list, default 5000
  9. Keep list_files.job.agi in the same folder as this file.
  10. */
  11. requirelib("cluster");
  12. //Read a request parameter. A script run as a Serverless endpoint gets the
  13. //request through getPara() (URL query) and postPara() (form body); a WebApp
  14. //backend script gets its POST fields as global variables instead.
  15. function requestParam(name) {
  16. var v = null;
  17. if (typeof getPara === "function") {
  18. v = getPara(name);
  19. }
  20. if ((v === null || v === "") && typeof postPara === "function") {
  21. v = postPara(name);
  22. }
  23. if (v === null || v === "") {
  24. var globalScope = (function () { return this; })();
  25. v = (typeof globalScope[name] === "string") ? globalScope[name] : "";
  26. }
  27. return v === null ? "" : String(v);
  28. }
  29. function main() {
  30. if (!cluster.inCluster()) {
  31. return { error: "This node is not part of a cluster. Create or join one in System Settings > Cluster Settings." };
  32. }
  33. var args = {};
  34. if (requestParam("root") !== "") {
  35. args.root = requestParam("root");
  36. }
  37. if (parseInt(requestParam("limit")) > 0) {
  38. args.limit = parseInt(requestParam("limit"));
  39. }
  40. var id = cluster.jobs.submit({
  41. name: "List cluster files",
  42. script: "list_files.job.agi",
  43. args: args,
  44. timeout: 600
  45. });
  46. var rec;
  47. try {
  48. rec = cluster.jobs.wait(id, 120);
  49. } catch (e) {
  50. rec = cluster.jobs.status(id);
  51. return { job: id, status: rec.state.status, note: "Still running. Follow it on System Settings > Cluster Jobs." };
  52. }
  53. if (rec.state.status !== "succeeded") {
  54. return { job: id, status: rec.state.status, error: rec.state.error || rec.state.reason || "" };
  55. }
  56. return rec.state.output;
  57. }
  58. sendJSONResp(JSON.stringify(main()));