I am using GKE cron job and Node.js. I am doubt that is it necessary to exit the process after cron job done? I know GKE will destroy the pod after the cron job done.
Here is my code:
try {
await job();
} catch (error) {
console.error(`Run ${context.jobName} cron job failed.`);
console.error(error);
process.exit(1);
}
process.exit(0)
And, I found another issue. If you use @google_cloud/logging-winston
, if you exit the process after cron job done. It will throw an error:
Error: 7 PERMISSION_DENIED: The caller does not have permission at Object.exports.createStatusError (/app/node_modules/grpc/src/common.js:91:15) at Object.onReceiveStatus (/app/node_modules/grpc/src/client_interceptors.js:1204:28) at InterceptingListener._callNext (/app/node_modules/grpc/src/client_interceptors.js:568:42) at InterceptingListener.onReceiveStatus (/app/node_modules/grpc/src/client_interceptors.js:618:8) at callback (/app/node_modules/grpc/src/client_interceptors.js:845:24)
I guess @google_cloud/logging-winston
try to log but the pod is destroyed.
Using console.log
, the error gone. I guess @google_cloud/logging-winston
log asynchronously.
The problem is your application is exiting before winston could write file log. We resolve this issue in cron tasks by adding a sleep at the end of execution to ensure winston had finished.
Also, winston provide a logger.on('finish') event that is triggered when all pending logs has been writted. To work with finish event you need to invoke logger.end() at the end of logger line