I have a Cloud Run service(Express Endpoint) that is triggered by a Pub/Sub push notification when a file is uploaded to a Cloud Storage bucket. However, the Cloud Run service is getting triggered multiple times for each file upload, which is causing duplicate processing. I have set the acknowledgment deadline to 10 minutes, but it does not help.
Here's how the code looks like(ignoring functions implementations):
app.post("/", async (req, res) => {
try {
console.log("Post endpoint is hited")
const file = decodeBase64Json(req.body.message.data);
const csvData = await downloadFile(storage, file.bucket, file.name);
await removeHydrofoilsConfigurations();
console.log("Procesiong of csv started")
await processData(csvData);
res.status(200).send("Cloud run finished csv processing successfully");
} catch (ex) {
console.log(`Error: ${ex}`);
res.status(400).send("Cloud run failed during csv processing");
}
});
The downloadFile function is this:
async function downloadFile(storage, bucketName, fileName) {
const options = { destination: `/tmp/${fileName}` };
await storage.bucket(bucketName).file(fileName).download(options);
const filePath = `/tmp/${fileName}`;
const csvData = [];
return new Promise((resolve, reject) => {
fs.createReadStream(filePath)
.pipe(csv())
.on("data", (data) => csvData.push(data))
.on("end", () => {
try {
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
console.log("File deleted successfully");
} else {
console.log("File does not exist");
}
} catch (err) {
console.error(err);
}
resolve(csvData);
})
.on("error", (error) => reject(error));
});
}