firebase-tools-with-isolate 13.11.2 → 13.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/api.js +4 -2
- package/lib/apphosting/githubConnections.js +125 -37
- package/lib/apphosting/index.js +3 -8
- package/lib/auth.js +14 -15
- package/lib/bin/firebase.js +7 -5
- package/lib/commands/dataconnect-sdk-generate.js +5 -9
- package/lib/commands/dataconnect-services-list.js +1 -1
- package/lib/commands/deploy.js +10 -2
- package/lib/commands/ext-configure.js +1 -6
- package/lib/commands/ext-dev-init.js +27 -30
- package/lib/commands/ext-dev-upload.js +4 -6
- package/lib/commands/ext-info.js +3 -5
- package/lib/commands/ext-install.js +0 -5
- package/lib/commands/ext-uninstall.js +0 -5
- package/lib/commands/ext-update.js +0 -5
- package/lib/commands/firestore-backups-schedules-create.js +11 -14
- package/lib/commands/firestore-backups-schedules-list.js +1 -1
- package/lib/commands/firestore-backups-schedules-update.js +4 -3
- package/lib/commands/firestore-databases-create.js +24 -7
- package/lib/commands/firestore-databases-restore.js +4 -4
- package/lib/commands/firestore-databases-update.js +5 -6
- package/lib/commands/functions-secrets-set.js +9 -6
- package/lib/commands/help.js +2 -0
- package/lib/commands/hosting-channel-create.js +1 -1
- package/lib/commands/hosting-channel-delete.js +1 -1
- package/lib/commands/hosting-channel-deploy.js +1 -1
- package/lib/commands/hosting-clone.js +1 -2
- package/lib/commands/index.js +9 -11
- package/lib/commands/init.js +16 -12
- package/lib/commands/login.js +1 -2
- package/lib/commands/setup-emulators-dataconnect.js +22 -3
- package/lib/config.js +5 -0
- package/lib/crashlytics/buildToolsJarHelper.js +1 -1
- package/lib/dataconnect/client.js +57 -9
- package/lib/dataconnect/fileUtils.js +71 -7
- package/lib/dataconnect/filters.js +12 -1
- package/lib/dataconnect/load.js +9 -7
- package/lib/dataconnect/provisionCloudSql.js +5 -1
- package/lib/dataconnect/schemaMigration.js +21 -0
- package/lib/dataconnect/types.js +8 -1
- package/lib/deploy/dataconnect/prepare.js +16 -2
- package/lib/deploy/extensions/planner.js +52 -1
- package/lib/deploy/extensions/prepare.js +98 -23
- package/lib/deploy/extensions/release.js +6 -0
- package/lib/deploy/functions/build.js +15 -6
- package/lib/deploy/functions/deploy.js +12 -12
- package/lib/deploy/functions/params.js +5 -3
- package/lib/deploy/functions/prepare.js +8 -1
- package/lib/deploy/functions/release/index.js +4 -0
- package/lib/deploy/functions/runtimes/discovery/parsing.js +1 -1
- package/lib/deploy/functions/runtimes/discovery/v1alpha1.js +46 -0
- package/lib/emulator/adminSdkConfig.test.js +16 -0
- package/lib/emulator/constants.js +8 -0
- package/lib/emulator/controller.js +14 -10
- package/lib/emulator/dataconnectEmulator.js +122 -18
- package/lib/emulator/downloadableEmulators.js +24 -25
- package/lib/emulator/env.js +3 -0
- package/lib/emulator/eventarcEmulator.js +68 -18
- package/lib/emulator/functionsEmulator.js +119 -1
- package/lib/emulator/functionsEmulatorShared.js +20 -0
- package/lib/emulator/hub.js +1 -1
- package/lib/emulator/portUtils.js +10 -0
- package/lib/emulator/pubsubEmulator.js +1 -1
- package/lib/emulator/registry.js +2 -1
- package/lib/emulator/storage/apis/shared.js +2 -1
- package/lib/emulator/storage/persistence.js +5 -13
- package/lib/emulator/storage/rfc.js +9 -0
- package/lib/emulator/storage/rules/config.js +4 -2
- package/lib/emulator/taskQueue.js +348 -0
- package/lib/emulator/tasksEmulator.js +237 -0
- package/lib/emulator/types.js +3 -0
- package/lib/experiments.js +8 -3
- package/lib/extensions/askUserForEventsConfig.js +18 -8
- package/lib/extensions/askUserForParam.js +3 -2
- package/lib/extensions/change-log.js +2 -4
- package/lib/extensions/displayExtensionInfo.js +5 -10
- package/lib/extensions/extensionsApi.js +1 -1
- package/lib/extensions/extensionsHelper.js +30 -10
- package/lib/extensions/localHelper.js +1 -1
- package/lib/extensions/provisioningHelper.js +1 -1
- package/lib/extensions/refs.js +26 -11
- package/lib/extensions/runtimes/common.js +75 -0
- package/lib/extensions/types.js +56 -1
- package/lib/extensions/updateHelper.js +1 -2
- package/lib/extensions/warnings.js +2 -7
- package/lib/firestore/api-sort.test.js +74 -0
- package/lib/firestore/api.js +8 -7
- package/lib/firestore/backupUtils.test.js +18 -0
- package/lib/firestore/pretty-print.js +21 -1
- package/lib/firestore/pretty-print.test.js +61 -0
- package/lib/firestore/util.test.js +42 -0
- package/lib/frameworks/angular/utils.js +13 -5
- package/lib/frameworks/compose/discover/mockFileSystem.js +36 -0
- package/lib/frameworks/constants.js +1 -1
- package/lib/frameworks/index.js +2 -6
- package/lib/frameworks/next/constants.js +1 -1
- package/lib/frameworks/next/index.js +35 -15
- package/lib/frameworks/next/utils.js +53 -9
- package/lib/frameworks/nuxt/index.js +3 -3
- package/lib/functions/events/v2.js +2 -1
- package/lib/gcp/cloudsql/cloudsqladmin.js +6 -1
- package/lib/gcp/devConnect.js +36 -1
- package/lib/hosting/implicitInit.js +2 -2
- package/lib/init/features/dataconnect/index.js +104 -32
- package/lib/init/features/dataconnect/sdk.js +147 -0
- package/lib/init/features/firestore/indexes.js +2 -2
- package/lib/init/features/firestore/rules.js +2 -2
- package/lib/init/features/functions/javascript.js +6 -8
- package/lib/init/features/functions/python.js +4 -6
- package/lib/init/features/functions/typescript.js +8 -10
- package/lib/init/features/hosting/github.js +43 -21
- package/lib/init/features/hosting/index.js +3 -3
- package/lib/init/features/index.js +3 -1
- package/lib/init/features/storage.js +2 -2
- package/lib/init/index.js +1 -0
- package/lib/listFiles.js +1 -1
- package/lib/management/apps.js +2 -2
- package/lib/prompt.js +22 -1
- package/lib/rc.js +1 -0
- package/lib/requireAuth.js +8 -3
- package/lib/rulesDeploy.js +14 -12
- package/lib/templates.js +23 -0
- package/lib/utils.js +2 -2
- package/package.json +25 -24
- package/schema/connector-yaml.json +36 -4
- package/schema/dataconnect-yaml.json +4 -0
- package/schema/firebase-config.json +12 -8
- package/templates/_gitignore +3 -0
- package/templates/init/dataconnect/connector.yaml +15 -2
- package/templates/init/dataconnect/dataconnect.yaml +5 -4
- package/templates/init/dataconnect/mutations.gql +45 -25
- package/templates/init/dataconnect/queries.gql +69 -36
- package/templates/init/dataconnect/schema.gql +38 -21
- package/lib/extensions/billingMigrationHelper.js +0 -61
|
@@ -17,6 +17,7 @@ const events = require("../functions/events");
|
|
|
17
17
|
const utils_1 = require("../utils");
|
|
18
18
|
const V2_EVENTS = [
|
|
19
19
|
events.v2.PUBSUB_PUBLISH_EVENT,
|
|
20
|
+
events.v2.FIREALERTS_EVENT,
|
|
20
21
|
...events.v2.STORAGE_EVENTS,
|
|
21
22
|
...events.v2.DATABASE_EVENTS,
|
|
22
23
|
...events.v2.FIRESTORE_EVENTS,
|
|
@@ -130,6 +131,19 @@ function emulatedFunctionsFromEndpoints(endpoints) {
|
|
|
130
131
|
}
|
|
131
132
|
else if (backend.isTaskQueueTriggered(endpoint)) {
|
|
132
133
|
def.httpsTrigger = {};
|
|
134
|
+
def.taskQueueTrigger = {
|
|
135
|
+
retryConfig: {
|
|
136
|
+
maxAttempts: endpoint.taskQueueTrigger.retryConfig?.maxAttempts,
|
|
137
|
+
maxRetrySeconds: endpoint.taskQueueTrigger.retryConfig?.maxRetrySeconds,
|
|
138
|
+
maxBackoffSeconds: endpoint.taskQueueTrigger.retryConfig?.maxBackoffSeconds,
|
|
139
|
+
maxDoublings: endpoint.taskQueueTrigger.retryConfig?.maxDoublings,
|
|
140
|
+
minBackoffSeconds: endpoint.taskQueueTrigger.retryConfig?.minBackoffSeconds,
|
|
141
|
+
},
|
|
142
|
+
rateLimits: {
|
|
143
|
+
maxConcurrentDispatches: endpoint.taskQueueTrigger.rateLimits?.maxConcurrentDispatches,
|
|
144
|
+
maxDispatchesPerSecond: endpoint.taskQueueTrigger.rateLimits?.maxDispatchesPerSecond,
|
|
145
|
+
},
|
|
146
|
+
};
|
|
133
147
|
}
|
|
134
148
|
else {
|
|
135
149
|
}
|
|
@@ -187,6 +201,9 @@ function getFunctionService(def) {
|
|
|
187
201
|
if (def.httpsTrigger) {
|
|
188
202
|
return "https";
|
|
189
203
|
}
|
|
204
|
+
if (def.taskQueueTrigger) {
|
|
205
|
+
return constants_1.Constants.SERVICE_CLOUD_TASKS;
|
|
206
|
+
}
|
|
190
207
|
return "unknown";
|
|
191
208
|
}
|
|
192
209
|
exports.getFunctionService = getFunctionService;
|
|
@@ -203,6 +220,9 @@ function getServiceFromEventType(eventType) {
|
|
|
203
220
|
if (eventType.includes("storage")) {
|
|
204
221
|
return constants_1.Constants.SERVICE_STORAGE;
|
|
205
222
|
}
|
|
223
|
+
if (eventType.includes("firebasealerts")) {
|
|
224
|
+
return constants_1.Constants.SERVICE_FIREALERTS;
|
|
225
|
+
}
|
|
206
226
|
if (eventType.includes("analytics")) {
|
|
207
227
|
return constants_1.Constants.SERVICE_ANALYTICS;
|
|
208
228
|
}
|
package/lib/emulator/hub.js
CHANGED
|
@@ -156,7 +156,7 @@ class EmulatorHub extends ExpressBasedEmulator_1.ExpressBasedEmulator {
|
|
|
156
156
|
const locatorPath = EmulatorHub.getLocatorFilePath(this.args.projectId);
|
|
157
157
|
return new Promise((resolve, reject) => {
|
|
158
158
|
fs.unlink(locatorPath, (e) => {
|
|
159
|
-
if (e) {
|
|
159
|
+
if (e && e.code !== "ENOENT") {
|
|
160
160
|
reject(e);
|
|
161
161
|
}
|
|
162
162
|
else {
|
|
@@ -11,6 +11,7 @@ const types_1 = require("./types");
|
|
|
11
11
|
const constants_1 = require("./constants");
|
|
12
12
|
const emulatorLogger_1 = require("./emulatorLogger");
|
|
13
13
|
const node_child_process_1 = require("node:child_process");
|
|
14
|
+
const dataconnectEmulator_1 = require("./dataconnectEmulator");
|
|
14
15
|
const RESTRICTED_PORTS = new Set([
|
|
15
16
|
1,
|
|
16
17
|
7,
|
|
@@ -155,6 +156,7 @@ const EMULATOR_CAN_LISTEN_ON_PRIMARY_ONLY = {
|
|
|
155
156
|
functions: true,
|
|
156
157
|
logging: true,
|
|
157
158
|
storage: true,
|
|
159
|
+
tasks: true,
|
|
158
160
|
hosting: true,
|
|
159
161
|
};
|
|
160
162
|
const MAX_PORT = 65535;
|
|
@@ -219,6 +221,14 @@ async function resolveHostAndAssignPorts(listenConfig) {
|
|
|
219
221
|
available.push(listen);
|
|
220
222
|
}
|
|
221
223
|
else {
|
|
224
|
+
if (/^dataconnect/i.exec(name)) {
|
|
225
|
+
const alreadyRunning = await (0, dataconnectEmulator_1.checkIfDataConnectEmulatorRunningOnAddress)(listen);
|
|
226
|
+
if (alreadyRunning) {
|
|
227
|
+
emuLogger.logLabeled("DEBUG", "dataconnect", `Detected already running emulator on ${listen.address}:${listen.port}. Will attempt to reuse it.`);
|
|
228
|
+
}
|
|
229
|
+
available.push(listen);
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
222
232
|
if (!portFixed) {
|
|
223
233
|
if (i > 0) {
|
|
224
234
|
emuLogger.logLabeled("DEBUG", name, `Port ${p} taken on secondary address ${addr.address}, will keep searching to find a better port.`);
|
|
@@ -132,7 +132,7 @@ class PubsubEmulator {
|
|
|
132
132
|
};
|
|
133
133
|
}
|
|
134
134
|
createCloudEventRequestBody(topic, message) {
|
|
135
|
-
const truncatedPublishTime = new Date(message.publishTime.
|
|
135
|
+
const truncatedPublishTime = new Date(message.publishTime.getTime()).toISOString();
|
|
136
136
|
const data = {
|
|
137
137
|
message: {
|
|
138
138
|
messageId: message.id,
|
package/lib/emulator/registry.js
CHANGED
|
@@ -49,6 +49,7 @@ class EmulatorRegistry {
|
|
|
49
49
|
storage: 3.5,
|
|
50
50
|
eventarc: 3.6,
|
|
51
51
|
dataconnect: 3.7,
|
|
52
|
+
tasks: 3.8,
|
|
52
53
|
hub: 4,
|
|
53
54
|
logging: 5,
|
|
54
55
|
};
|
|
@@ -111,7 +112,7 @@ class EmulatorRegistry {
|
|
|
111
112
|
url.port = info.port.toString();
|
|
112
113
|
}
|
|
113
114
|
else {
|
|
114
|
-
|
|
115
|
+
throw new Error(`Cannot determine host and port of ${emulator}`);
|
|
115
116
|
}
|
|
116
117
|
return url;
|
|
117
118
|
}
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.sendFileBytes = void 0;
|
|
4
4
|
const zlib_1 = require("zlib");
|
|
5
5
|
const crc_1 = require("../crc");
|
|
6
|
+
const rfc_1 = require("../rfc");
|
|
6
7
|
function sendFileBytes(md, data, req, res) {
|
|
7
8
|
let didGunzip = false;
|
|
8
9
|
if (md.contentEncoding === "gzip") {
|
|
@@ -16,7 +17,7 @@ function sendFileBytes(md, data, req, res) {
|
|
|
16
17
|
res.setHeader("Accept-Ranges", "bytes");
|
|
17
18
|
res.setHeader("Content-Type", md.contentType || "application/octet-stream");
|
|
18
19
|
const fileName = md.name.split("/").pop();
|
|
19
|
-
res.setHeader("Content-Disposition", `${md.contentDisposition || "attachment"}; filename
|
|
20
|
+
res.setHeader("Content-Disposition", `${md.contentDisposition || "attachment"}; filename*=${(0, rfc_1.encodeRFC5987)(fileName)}`);
|
|
20
21
|
if (didGunzip) {
|
|
21
22
|
res.setHeader("Transfer-Encoding", "chunked");
|
|
22
23
|
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Persistence = void 0;
|
|
4
4
|
const fs_1 = require("fs");
|
|
5
|
-
const
|
|
5
|
+
const rimraf_1 = require("rimraf");
|
|
6
6
|
const fs = require("fs");
|
|
7
7
|
const fse = require("fs-extra");
|
|
8
8
|
const path = require("path");
|
|
@@ -56,18 +56,10 @@ class Persistence {
|
|
|
56
56
|
}
|
|
57
57
|
this._diskPathMap.delete(fileName);
|
|
58
58
|
}
|
|
59
|
-
deleteAll() {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
reject(err);
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
66
|
-
this._diskPathMap = new Map();
|
|
67
|
-
resolve();
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
});
|
|
59
|
+
async deleteAll() {
|
|
60
|
+
await (0, rimraf_1.rimraf)(this._dirPath);
|
|
61
|
+
this._diskPathMap = new Map();
|
|
62
|
+
return;
|
|
71
63
|
}
|
|
72
64
|
renameFile(oldName, newName) {
|
|
73
65
|
const oldNameId = this.getDiskFileName(oldName);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.encodeRFC5987 = void 0;
|
|
4
|
+
function encodeRFC5987(str) {
|
|
5
|
+
return encodeURIComponent(str)
|
|
6
|
+
.replace(/['()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`)
|
|
7
|
+
.replace(/%(7C|60|5E)/g, (str, hex) => String.fromCharCode(parseInt(hex, 16)));
|
|
8
|
+
}
|
|
9
|
+
exports.encodeRFC5987 = encodeRFC5987;
|
|
@@ -6,6 +6,7 @@ const fsutils_1 = require("../../../fsutils");
|
|
|
6
6
|
const constants_1 = require("../../constants");
|
|
7
7
|
const types_1 = require("../../types");
|
|
8
8
|
const emulatorLogger_1 = require("../../emulatorLogger");
|
|
9
|
+
const templates_1 = require("../../../templates");
|
|
9
10
|
function getSourceFile(rules, options) {
|
|
10
11
|
const path = options.config.path(rules);
|
|
11
12
|
return { name: path, content: (0, fsutils_1.readFile)(path) };
|
|
@@ -48,6 +49,7 @@ function getStorageRulesConfig(projectId, options) {
|
|
|
48
49
|
}
|
|
49
50
|
exports.getStorageRulesConfig = getStorageRulesConfig;
|
|
50
51
|
function defaultStorageRules() {
|
|
51
|
-
const
|
|
52
|
-
|
|
52
|
+
const defaultRulesPath = "emulators/default_storage.rules";
|
|
53
|
+
const name = (0, templates_1.absoluteTemplateFilePath)(defaultRulesPath);
|
|
54
|
+
return { name, content: (0, fsutils_1.readFile)(name) };
|
|
53
55
|
}
|
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TaskQueue = exports.TaskStatus = exports.Queue = void 0;
|
|
4
|
+
const abort_controller_1 = require("abort-controller");
|
|
5
|
+
const emulatorLogger_1 = require("./emulatorLogger");
|
|
6
|
+
const types_1 = require("./types");
|
|
7
|
+
const node_fetch_1 = require("node-fetch");
|
|
8
|
+
class Node {
|
|
9
|
+
constructor(data) {
|
|
10
|
+
this.data = data;
|
|
11
|
+
this.next = null;
|
|
12
|
+
this.prev = null;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
class Queue {
|
|
16
|
+
constructor(capacity = 10000) {
|
|
17
|
+
this.nodeMap = {};
|
|
18
|
+
this.count = 0;
|
|
19
|
+
this.first = null;
|
|
20
|
+
this.last = null;
|
|
21
|
+
this.capacity = capacity;
|
|
22
|
+
}
|
|
23
|
+
enqueue(id, item) {
|
|
24
|
+
if (this.count >= this.capacity) {
|
|
25
|
+
throw new Error("Queue has reached capacity");
|
|
26
|
+
}
|
|
27
|
+
const newNode = new Node(item);
|
|
28
|
+
if (this.nodeMap[id] !== undefined) {
|
|
29
|
+
throw new Error("Queue IDs must be unique");
|
|
30
|
+
}
|
|
31
|
+
this.nodeMap[id] = newNode;
|
|
32
|
+
if (!this.first) {
|
|
33
|
+
this.first = newNode;
|
|
34
|
+
}
|
|
35
|
+
if (this.last) {
|
|
36
|
+
this.last.next = newNode;
|
|
37
|
+
}
|
|
38
|
+
newNode.prev = this.last;
|
|
39
|
+
this.last = newNode;
|
|
40
|
+
this.count++;
|
|
41
|
+
}
|
|
42
|
+
peek() {
|
|
43
|
+
if (this.first) {
|
|
44
|
+
return this.first.data;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
throw new Error("Trying to peek into an empty queue");
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
dequeue() {
|
|
51
|
+
if (this.first) {
|
|
52
|
+
const currentFirst = this.first;
|
|
53
|
+
this.first = this.first.next;
|
|
54
|
+
if (this.last === currentFirst) {
|
|
55
|
+
this.last = null;
|
|
56
|
+
}
|
|
57
|
+
this.count--;
|
|
58
|
+
return currentFirst.data;
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
throw new Error("Trying to dequeue from an empty queue");
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
remove(id) {
|
|
65
|
+
if (this.nodeMap[id] === undefined) {
|
|
66
|
+
throw new Error("Trying to remove a task that doesn't exist");
|
|
67
|
+
}
|
|
68
|
+
const toRemove = this.nodeMap[id];
|
|
69
|
+
if (toRemove.next === null && toRemove.prev === null) {
|
|
70
|
+
this.first = null;
|
|
71
|
+
this.last = null;
|
|
72
|
+
}
|
|
73
|
+
else if (toRemove.next === null) {
|
|
74
|
+
this.last = toRemove.prev;
|
|
75
|
+
toRemove.prev.next = null;
|
|
76
|
+
}
|
|
77
|
+
else if (toRemove.prev === null) {
|
|
78
|
+
this.first = toRemove.next;
|
|
79
|
+
toRemove.next.prev = null;
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
const prev = toRemove.prev;
|
|
83
|
+
const next = toRemove.next;
|
|
84
|
+
prev.next = next;
|
|
85
|
+
next.prev = prev;
|
|
86
|
+
}
|
|
87
|
+
delete this.nodeMap[id];
|
|
88
|
+
this.count--;
|
|
89
|
+
}
|
|
90
|
+
getAll() {
|
|
91
|
+
const all = [];
|
|
92
|
+
let curr = this.first;
|
|
93
|
+
while (curr) {
|
|
94
|
+
all.push(curr.data);
|
|
95
|
+
curr = curr.next;
|
|
96
|
+
}
|
|
97
|
+
return all;
|
|
98
|
+
}
|
|
99
|
+
isEmpty() {
|
|
100
|
+
return this.first === null;
|
|
101
|
+
}
|
|
102
|
+
size() {
|
|
103
|
+
return this.count;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
exports.Queue = Queue;
|
|
107
|
+
var TaskStatus;
|
|
108
|
+
(function (TaskStatus) {
|
|
109
|
+
TaskStatus[TaskStatus["NOT_STARTED"] = 0] = "NOT_STARTED";
|
|
110
|
+
TaskStatus[TaskStatus["RUNNING"] = 1] = "RUNNING";
|
|
111
|
+
TaskStatus[TaskStatus["RETRY"] = 2] = "RETRY";
|
|
112
|
+
TaskStatus[TaskStatus["FAILED"] = 3] = "FAILED";
|
|
113
|
+
TaskStatus[TaskStatus["FINISHED"] = 4] = "FINISHED";
|
|
114
|
+
})(TaskStatus = exports.TaskStatus || (exports.TaskStatus = {}));
|
|
115
|
+
class TaskQueue {
|
|
116
|
+
constructor(key, config) {
|
|
117
|
+
this.key = key;
|
|
118
|
+
this.config = config;
|
|
119
|
+
this.queue = new Queue();
|
|
120
|
+
this.logger = emulatorLogger_1.EmulatorLogger.forEmulator(types_1.Emulators.TASKS);
|
|
121
|
+
this.tokens = 0;
|
|
122
|
+
this.addedTimes = [];
|
|
123
|
+
this.completedTimes = [];
|
|
124
|
+
this.failedTimes = [];
|
|
125
|
+
this.maxTokens = Math.max(this.config.rateLimits.maxDispatchesPerSecond, 1.1);
|
|
126
|
+
this.lastTokenUpdate = Date.now();
|
|
127
|
+
this.queuedIds = new Set();
|
|
128
|
+
this.dispatches = new Array(this.config.rateLimits.maxConcurrentDispatches).fill(null);
|
|
129
|
+
this.openDispatches = Array.from(this.dispatches.keys());
|
|
130
|
+
}
|
|
131
|
+
dispatchTasks() {
|
|
132
|
+
while (!this.queue.isEmpty() && this.openDispatches.length > 0 && this.tokens >= 1) {
|
|
133
|
+
const dispatchLocation = this.openDispatches.pop();
|
|
134
|
+
if (dispatchLocation !== undefined) {
|
|
135
|
+
const dispatch = this.queue.dequeue();
|
|
136
|
+
dispatch.metadata.lastRunTime = null;
|
|
137
|
+
dispatch.metadata.currentAttempt = 1;
|
|
138
|
+
dispatch.metadata.status = TaskStatus.NOT_STARTED;
|
|
139
|
+
dispatch.metadata.startTime = Date.now();
|
|
140
|
+
this.dispatches[dispatchLocation] = dispatch;
|
|
141
|
+
this.tokens--;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
setDispatch(dispatches) {
|
|
146
|
+
this.dispatches = dispatches;
|
|
147
|
+
const open = [];
|
|
148
|
+
for (let i = 0; i < this.dispatches.length; i++) {
|
|
149
|
+
if (dispatches[i] === null) {
|
|
150
|
+
open.push(i);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
this.openDispatches = open;
|
|
154
|
+
}
|
|
155
|
+
getDispatch() {
|
|
156
|
+
return this.dispatches;
|
|
157
|
+
}
|
|
158
|
+
processDispatch() {
|
|
159
|
+
for (let i = 0; i < this.dispatches.length; i++) {
|
|
160
|
+
if (this.dispatches[i] !== null) {
|
|
161
|
+
switch (this.dispatches[i]?.metadata.status) {
|
|
162
|
+
case TaskStatus.FAILED:
|
|
163
|
+
this.dispatches[i] = null;
|
|
164
|
+
this.openDispatches.push(i);
|
|
165
|
+
this.completedTimes.push(Date.now());
|
|
166
|
+
this.failedTimes.push(Date.now());
|
|
167
|
+
break;
|
|
168
|
+
case TaskStatus.NOT_STARTED:
|
|
169
|
+
void this.runTask(i);
|
|
170
|
+
break;
|
|
171
|
+
case TaskStatus.RETRY:
|
|
172
|
+
this.handleRetry(i);
|
|
173
|
+
break;
|
|
174
|
+
case TaskStatus.FINISHED:
|
|
175
|
+
this.dispatches[i] = null;
|
|
176
|
+
this.openDispatches.push(i);
|
|
177
|
+
this.completedTimes.push(Date.now());
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
async runTask(dispatchIndex) {
|
|
184
|
+
if (this.dispatches[dispatchIndex] === null) {
|
|
185
|
+
throw new Error("Trying to dispatch a nonexistent task");
|
|
186
|
+
}
|
|
187
|
+
const emulatedTask = this.dispatches[dispatchIndex];
|
|
188
|
+
if (emulatedTask.metadata.lastRunTime !== null &&
|
|
189
|
+
Date.now() - emulatedTask.metadata.lastRunTime < emulatedTask.metadata.currentBackoff * 1000) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
emulatedTask.metadata.status = TaskStatus.RUNNING;
|
|
193
|
+
try {
|
|
194
|
+
const headers = {
|
|
195
|
+
"Content-Type": "application/json",
|
|
196
|
+
"X-CloudTasks-QueueName": this.key,
|
|
197
|
+
"X-CloudTasks-TaskName": emulatedTask.task.name.split("/").pop(),
|
|
198
|
+
"X-CloudTasks-TaskRetryCount": `${emulatedTask.metadata.currentAttempt - 1}`,
|
|
199
|
+
"X-CloudTasks-TaskExecutionCount": `${emulatedTask.metadata.executionCount}`,
|
|
200
|
+
"X-CloudTasks-TaskETA": `${emulatedTask.task.scheduleTime || Date.now()}`,
|
|
201
|
+
...emulatedTask.task.httpRequest.headers,
|
|
202
|
+
};
|
|
203
|
+
if (emulatedTask.metadata.previousResponse) {
|
|
204
|
+
headers["X-CloudTasks-TaskPreviousResponse"] = `${emulatedTask.metadata.previousResponse}`;
|
|
205
|
+
}
|
|
206
|
+
const controller = new abort_controller_1.default();
|
|
207
|
+
const signal = controller.signal;
|
|
208
|
+
const request = (0, node_fetch_1.default)(emulatedTask.task.httpRequest.url, {
|
|
209
|
+
method: "POST",
|
|
210
|
+
headers: headers,
|
|
211
|
+
body: JSON.stringify(emulatedTask.task.httpRequest.body),
|
|
212
|
+
signal: signal,
|
|
213
|
+
});
|
|
214
|
+
const dispatchDeadline = emulatedTask.task.dispatchDeadline;
|
|
215
|
+
const dispatchDeadlineSeconds = dispatchDeadline
|
|
216
|
+
? parseInt(dispatchDeadline.substring(0, dispatchDeadline.length - 1))
|
|
217
|
+
: 60;
|
|
218
|
+
const abortId = setTimeout(() => {
|
|
219
|
+
controller.abort();
|
|
220
|
+
}, dispatchDeadlineSeconds * 1000);
|
|
221
|
+
const response = await request;
|
|
222
|
+
clearTimeout(abortId);
|
|
223
|
+
if (response.ok) {
|
|
224
|
+
emulatedTask.metadata.status = TaskStatus.FINISHED;
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
if (!(response.status >= 500 && response.status <= 599)) {
|
|
229
|
+
emulatedTask.metadata.executionCount++;
|
|
230
|
+
}
|
|
231
|
+
emulatedTask.metadata.previousResponse = response.status;
|
|
232
|
+
emulatedTask.metadata.status = TaskStatus.RETRY;
|
|
233
|
+
emulatedTask.metadata.lastRunTime = Date.now();
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
catch (e) {
|
|
237
|
+
this.logger.logLabeled("WARN", `${e}`);
|
|
238
|
+
emulatedTask.metadata.status = TaskStatus.RETRY;
|
|
239
|
+
emulatedTask.metadata.lastRunTime = Date.now();
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
handleRetry(dispatchIndex) {
|
|
243
|
+
if (this.dispatches[dispatchIndex] === null) {
|
|
244
|
+
throw new Error("Trying to retry a nonexistent task");
|
|
245
|
+
}
|
|
246
|
+
const { metadata } = this.dispatches[dispatchIndex];
|
|
247
|
+
const { retryConfig } = this.config;
|
|
248
|
+
if (this.shouldStopRetrying(metadata, retryConfig)) {
|
|
249
|
+
metadata.status = TaskStatus.FAILED;
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
this.updateMetadata(metadata, retryConfig);
|
|
253
|
+
metadata.status = TaskStatus.NOT_STARTED;
|
|
254
|
+
}
|
|
255
|
+
shouldStopRetrying(metadata, retryOptions) {
|
|
256
|
+
if (metadata.currentAttempt > retryOptions.maxAttempts) {
|
|
257
|
+
if (retryOptions.maxRetrySeconds === null || retryOptions.maxRetrySeconds === 0) {
|
|
258
|
+
return true;
|
|
259
|
+
}
|
|
260
|
+
if (Date.now() - metadata.startTime > retryOptions.maxRetrySeconds * 1000) {
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return false;
|
|
265
|
+
}
|
|
266
|
+
updateMetadata(metadata, retryOptions) {
|
|
267
|
+
const timeMultplier = Math.pow(2, Math.min(metadata.currentAttempt - 1, retryOptions.maxDoublings)) +
|
|
268
|
+
Math.max(0, metadata.currentAttempt - retryOptions.maxDoublings - 1) *
|
|
269
|
+
Math.pow(2, retryOptions.maxDoublings);
|
|
270
|
+
metadata.currentBackoff = Math.min(retryOptions.maxBackoffSeconds, timeMultplier * retryOptions.minBackoffSeconds);
|
|
271
|
+
metadata.currentAttempt++;
|
|
272
|
+
}
|
|
273
|
+
isActive() {
|
|
274
|
+
return !this.queue.isEmpty() || this.dispatches.some((e) => e !== null);
|
|
275
|
+
}
|
|
276
|
+
refillTokens() {
|
|
277
|
+
const tokensToAdd = ((Date.now() - this.lastTokenUpdate) / 1000) * this.config.rateLimits.maxDispatchesPerSecond;
|
|
278
|
+
this.addTokens(tokensToAdd);
|
|
279
|
+
this.lastTokenUpdate = Date.now();
|
|
280
|
+
}
|
|
281
|
+
addTokens(t) {
|
|
282
|
+
this.tokens += t;
|
|
283
|
+
this.tokens = Math.min(this.tokens, this.maxTokens);
|
|
284
|
+
}
|
|
285
|
+
setTokens(t) {
|
|
286
|
+
this.tokens = t;
|
|
287
|
+
}
|
|
288
|
+
getTokens() {
|
|
289
|
+
return this.tokens;
|
|
290
|
+
}
|
|
291
|
+
enqueue(task) {
|
|
292
|
+
if (this.queuedIds.has(task.name)) {
|
|
293
|
+
throw new Error(`A task has already been queued with id ${task.name}`);
|
|
294
|
+
}
|
|
295
|
+
const emulatedTask = {
|
|
296
|
+
task: task,
|
|
297
|
+
metadata: {
|
|
298
|
+
currentAttempt: 0,
|
|
299
|
+
currentBackoff: 0,
|
|
300
|
+
startTime: 0,
|
|
301
|
+
status: TaskStatus.NOT_STARTED,
|
|
302
|
+
lastRunTime: null,
|
|
303
|
+
previousResponse: null,
|
|
304
|
+
executionCount: 0,
|
|
305
|
+
},
|
|
306
|
+
};
|
|
307
|
+
emulatedTask.task.httpRequest.url =
|
|
308
|
+
emulatedTask.task.httpRequest.url === ""
|
|
309
|
+
? this.config.defaultUri
|
|
310
|
+
: emulatedTask.task.httpRequest.url;
|
|
311
|
+
this.queue.enqueue(emulatedTask.task.name, emulatedTask);
|
|
312
|
+
this.queuedIds.add(task.name);
|
|
313
|
+
this.addedTimes.push(Date.now());
|
|
314
|
+
}
|
|
315
|
+
delete(taskId) {
|
|
316
|
+
this.queue.remove(taskId);
|
|
317
|
+
}
|
|
318
|
+
getDebugInfo() {
|
|
319
|
+
return `
|
|
320
|
+
Task Queue (${this.key}):
|
|
321
|
+
- Active: ${this.isActive().toString()}
|
|
322
|
+
- Tokens: ${this.tokens}
|
|
323
|
+
- In Queue: ${this.queue.size()}
|
|
324
|
+
- Dispatch: [
|
|
325
|
+
${this.dispatches.map((t) => (t === null ? "empty" : t.task.name)).join(",\n")}
|
|
326
|
+
]
|
|
327
|
+
- Open Locations: [${this.openDispatches.join(", ")}]
|
|
328
|
+
`;
|
|
329
|
+
}
|
|
330
|
+
getStatistics() {
|
|
331
|
+
const fiveMinutesAgo = Date.now() - 5 * 60 * 1000;
|
|
332
|
+
const oneMinuteAgo = Date.now() - 60 * 1000;
|
|
333
|
+
this.addedTimes = this.addedTimes.filter((t) => t > fiveMinutesAgo);
|
|
334
|
+
this.failedTimes = this.failedTimes.filter((t) => t > fiveMinutesAgo);
|
|
335
|
+
this.completedTimes = this.completedTimes.filter((t) => t > oneMinuteAgo);
|
|
336
|
+
return {
|
|
337
|
+
numberOfTasks: this.queue.size(),
|
|
338
|
+
tasksAdded: this.addedTimes.length / 5,
|
|
339
|
+
completedLastMin: this.completedTimes.length,
|
|
340
|
+
failedTasks: this.failedTimes.length / 5,
|
|
341
|
+
runningTasks: this.dispatches.length,
|
|
342
|
+
maxRate: this.config.rateLimits.maxDispatchesPerSecond,
|
|
343
|
+
maxConcurrent: this.config.rateLimits.maxConcurrentDispatches,
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
exports.TaskQueue = TaskQueue;
|
|
348
|
+
TaskQueue.TASK_QUEUE_INTERVAL = 1000;
|