No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

workQueue.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.WorkQueue = void 0;
  4. const utils = require("../utils");
  5. const error_1 = require("../error");
  6. const emulatorLogger_1 = require("./emulatorLogger");
  7. const types_1 = require("./types");
  8. class WorkQueue {
  9. constructor(mode = types_1.FunctionsExecutionMode.AUTO, maxParallelWork = WorkQueue.DEFAULT_MAX_PARALLEL) {
  10. this.mode = mode;
  11. this.maxParallelWork = maxParallelWork;
  12. this.logger = emulatorLogger_1.EmulatorLogger.forEmulator(types_1.Emulators.FUNCTIONS);
  13. this.queue = [];
  14. this.running = [];
  15. this.notifyQueue = () => {
  16. };
  17. this.notifyWorkFinish = () => {
  18. };
  19. this.stopped = true;
  20. if (maxParallelWork < 1) {
  21. throw new error_1.FirebaseError(`Cannot run Functions emulator with less than 1 parallel worker (${WorkQueue.MAX_PARALLEL_ENV}=${process.env[WorkQueue.MAX_PARALLEL_ENV]})`);
  22. }
  23. }
  24. submit(entry) {
  25. this.queue.push(entry);
  26. this.notifyQueue();
  27. this.logState();
  28. }
  29. async start() {
  30. if (!this.stopped) {
  31. return;
  32. }
  33. this.stopped = false;
  34. while (!this.stopped) {
  35. if (!this.queue.length) {
  36. await new Promise((res) => {
  37. this.notifyQueue = res;
  38. });
  39. }
  40. if (this.running.length >= this.maxParallelWork) {
  41. this.logger.logLabeled("DEBUG", "work-queue", `waiting for work to finish (running=${this.running})`);
  42. await new Promise((res) => {
  43. this.notifyWorkFinish = res;
  44. });
  45. }
  46. const workPromise = this.runNext();
  47. if (this.mode === types_1.FunctionsExecutionMode.SEQUENTIAL) {
  48. await workPromise;
  49. }
  50. }
  51. }
  52. stop() {
  53. this.stopped = true;
  54. }
  55. async flush(timeoutMs = 60000) {
  56. if (!this.isWorking()) {
  57. return;
  58. }
  59. this.logger.logLabeled("BULLET", "functions", "Waiting for all functions to finish...");
  60. return new Promise((res, rej) => {
  61. const delta = 100;
  62. let elapsed = 0;
  63. const interval = setInterval(() => {
  64. elapsed += delta;
  65. if (elapsed >= timeoutMs) {
  66. rej(new Error(`Functions work queue not empty after ${timeoutMs}ms`));
  67. }
  68. if (!this.isWorking()) {
  69. clearInterval(interval);
  70. res();
  71. }
  72. }, delta);
  73. });
  74. }
  75. getState() {
  76. return {
  77. queuedWork: this.queue.map((work) => work.type),
  78. queueLength: this.queue.length,
  79. runningWork: this.running,
  80. workRunningCount: this.running.length,
  81. };
  82. }
  83. isWorking() {
  84. const state = this.getState();
  85. return state.queueLength > 0 || state.workRunningCount > 0;
  86. }
  87. async runNext() {
  88. const next = this.queue.shift();
  89. if (next) {
  90. this.running.push(next.type || "anonymous");
  91. this.logState();
  92. try {
  93. await next();
  94. }
  95. catch (e) {
  96. this.logger.log("DEBUG", e);
  97. }
  98. finally {
  99. const index = this.running.indexOf(next.type || "anonymous");
  100. if (index !== -1) {
  101. this.running.splice(index, 1);
  102. }
  103. this.notifyWorkFinish();
  104. this.logState();
  105. }
  106. }
  107. }
  108. logState() {
  109. this.logger.logLabeled("DEBUG", "work-queue", JSON.stringify(this.getState()));
  110. }
  111. }
  112. exports.WorkQueue = WorkQueue;
  113. WorkQueue.MAX_PARALLEL_ENV = "FUNCTIONS_EMULATOR_PARALLEL";
  114. WorkQueue.DEFAULT_MAX_PARALLEL = Number.parseInt(utils.envOverride(WorkQueue.MAX_PARALLEL_ENV, "50"));