-
Notifications
You must be signed in to change notification settings - Fork 955
Closed
Description
[REQUIRED] Describe your environment
Ubuntu 20.04 LTS, kernel 5.11.0-38-generic
NodeJS 14.16.0
Firebase 9.2.0
TypeScript 4.4.4
[REQUIRED] Describe the problem
App never terminates. Regardless of whether deleteApp() is called or not, node is left waiting for something and hangs.
This may be the a duplicate of #4987 , which is closed and locked so I can't add this as a comment there.
Relevant Code:
import { CloudFunctionsServiceClient } from "@google-cloud/functions";
import { initializeApp, deleteApp } from "firebase/app";
import { getFunctions, httpsCallable } from "firebase/functions";
import minimist from "minimist";
import fs from "fs";
import { JsonMap } from "./functions/src/generic/lib";
function delay(ms: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(() => { resolve(); }, ms);
});
}
class App {
private client = new CloudFunctionsServiceClient();
private fbApp = initializeApp({
projectId: "xxxx-xxxx",
apiKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
authDomain: "xxxx-xxxx.firebaseapp.com",
databaseURL: "https://xxxx-xxxx-default-rtdb.firebaseio.com",
storageBucket: "xxxx-xxxx.appspot.com",
messagingSenderId: "xxxxxxxxxxxx",
appId: "1:xxxxxxxxxxxx:web:xxxxxxxxxxxxxxxxxxxxxx",
});
private functions = getFunctions(this.fbApp);
private deviceId = "";
private app: JsonMap = {};
private verbose = false;
private command = "";
private filenames: string[] = [];
constructor() {
const argv = minimist(process.argv.slice(2), {
boolean: [ "v" ],
string: [ "d", "c", "_" ],
});
if (argv["v"]) this.verbose = true;
if (argv["c"]) this.command = argv["c"];
if (argv["d"]) this.deviceId = argv["d"];
this.filenames = argv["_"];
if (0 === this.filenames.length && "" === this.command) {
throw new Error("No operation specified");
}
if (! this.deviceId) {
throw new Error("No deviceId specified");
}
if (! (this.deviceId.startsWith("d"))) this.deviceId = "d" + this.deviceId;
}
async getConfig(): Promise<void> {
const cfgText = await fs.promises.readFile("functions/.runtimeconfig.json");
const cfg = JSON.parse(cfgText.toString());
// console.log(cfg);
if (! (cfg && "app" in cfg && "name" in cfg.app)) {
throw new Error("Inadequate local config");
}
this.app = cfg.app;
}
async run(): Promise<void> {
await this.getConfig();
/*
const [functions] = await this.client.listFunctions({
parent: `projects/${this.app.name}/locations/-`,
});
for (var f of functions) {
console.info(f.name);
}
*/
const sendCmd = httpsCallable(this.functions, "sendDeviceCommand2");
if (this.command) {
let result = await sendCmd({
deviceId: this.deviceId,
do: [ this.command ],
});
console.info(result);
}
for (const f of this.filenames) {
const buf = await fs.promises.readFile(f);
const lines = buf.toString().split("\n");
for (const line of lines) {
const lt = line.trim();
if ("" === lt) continue;
await delay(2000);
let result = await sendCmd({
deviceId: this.deviceId,
do: [ lt ],
});
}
}
await deleteApp(this.fbApp);
// process.exit(0);
}
}
(new App()).run().catch((e) => { console.error(e); });