pulumi/sdk/nodejs/runtime/log.ts
joeduffy a5a6c79925 Keep RPC connections alive for as long as we need them
The change to tear down RPC connections after the program exits --
to fix problems on Linux presumably due to the way libuv is implemented --
unfortunately introduces nondeterminism and overzealous termination that
can happen at inopportune times.  Instead, we need to wait for the current
RPC queue to drain.  To fix this, we'll maintain a list of currently active
RPC calls and, only once they have completed, will we close the clients.
2017-09-07 14:50:17 -07:00

59 lines
2.1 KiB
TypeScript

// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
import { getEngine, rpcKeepAlive } from "./settings";
let engproto = require("../proto/engine_pb.js");
// Log offers the ability to log messages in a way that integrate tightly with the resource engine's interface.
export class Log {
// debug logs a debug-level message that is generally hidden from end-users.
public static debug(msg: string): void {
let engine: Object | undefined = getEngine();
if (engine) {
Log.log(engine, engproto.LogSeverity.DEBUG, msg);
} else {
// ignore debug messages when no engine is available.
}
}
// info logs an informational message that is generally printed to stdout during resource operations.
public static info(msg: string): void {
let engine: Object | undefined = getEngine();
if (engine) {
Log.log(engine, engproto.LogSeverity.INFO, msg);
} else {
console.log(`info: [runtime] ${msg}`);
}
}
// warn logs a warning to indicate that something went wrong, but not catastrophically so.
public static warn(msg: string): void {
let engine: Object | undefined = getEngine();
if (engine) {
Log.log(engine, engproto.LogSeverity.WARNING, msg);
} else {
console.warn(`warning: [runtime] ${msg}`);
}
}
// error logs a fatal error to indicate that the tool should stop processing resource operations immediately.
public static error(msg: string): void {
let engine: Object | undefined = getEngine();
if (engine) {
Log.log(engine, engproto.LogSeverity.ERROR, msg);
} else {
console.error(`error: [runtime] ${msg}`);
}
}
private static log(engine: any, sev: any, msg: string): void {
let req = new engproto.LogRequest();
req.setSeverity(sev);
req.setMessage(msg);
let notAlive: () => void = rpcKeepAlive();
engine.log(req, () => {
// keep the process alive until it gets delivered
notAlive();
});
}
}