我正在使用Cloud Run@google-cloud/logging
从我的express
应用程序注销一些内容。
像这样的东西:
routeHandler.ts
import { Logging } from "@google-cloud/logging";
const logging = new Logging({ projectId: process.env.PROJECT_ID });
const logName = LOG_NAME;
const log = logging.log(logName);
const resource = {
type: "cloud_run_revision",
labels: { ... }
};
export const routeHandler: RequestHandler = (req,res,next) => {
try {
// EXAMPLE: LOG A WARNING
const metadata = { resource, severity: "WARNING" };
const entry = log.entry(metadata,"SOME WARNING MSG");
await log.write(entry);
return res.sendStatus(200);
}
catch(err) {
// EXAMPLE: LOG AN ERROR
const metadata = { resource, severity: "ERROR" };
const entry = log.entry(metadata,"SOME ERROR MSG");
await log.write(entry);
return res.sendStatus(500);
}
};
您可以看到的log.write(entry)
是异步的。因此,从理论上讲,建议您等待它。但是,这里的文档@google-cloud/logging
说的是:
我对此没有任何问题。在我的真实情况下,即使log.write()
失败,它也位于a内try-catch
,任何错误都将得到很好的处理。
我的问题是,它与Cloud Run文档有点冲突:
Note: If I don't wait for the log.write()
call, I'll end the request cycle by responding to the request
And Cloud Run does behave like that. A couple weeks back, I tried to respond immediately to the request and fire some long background job. And the process kind of halted for a while, and I think it restarted once it got another request. Completely unpredictable. And when I ran this test I'm mentioning here, I even had a MIN_INSTANCE=1
set on my cloud run service container. Even that didn't allow my background job to run smoothly. Therefore, I don't think it's fine to leave the process doing background stuff when I've finished handling a request (by doing the "fire and forget" approach).
So, what should I do here?
根据评论中@Karl-JorhanSjögren的正确假设,将此答案发布为社区Wiki。
对于在Cloud Run中运行的应用程序进行Log调用,确实建议您采用即弃即用的方法,因为您实际上并不需要强制同步。
如评论中提到的那样,在您完成请求后,您将对CPU禁用感到担忧,CPU将首先受到限制,以便在长时间不活动后可以快速恢复实例并完全禁用该实例。因此,触发小型日志记录调用(大多数情况下将在几毫秒内完成)应该不是问题。
文档中提到的内容针对的是运行时间较长的流程。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句