Script doesn't run on import but does run when I trigger manually #1495
Replies: 2 comments 1 reply
-
Is it possible that on IMPORT - there is a timeout issue that is stopping the script to wait 60 seconds / 120 seconds? But when I MANUALLY run the same script - there is no such timeout? |
Beta Was this translation helpful? Give feedback.
-
@njproductionsus When you import rows, the script runs almost instantaneously, which means that any delays specified in the code may not have enough time to execute before the script completes. One possible solution could be to modify your code to handle the delays outside of the Cloud Function. Instead of using Here's an example of how you can modify your code to achieve this: const derivative: Derivative = async ({ row, ref, db, storage, auth, logging }) => {
try {
const delayValue = row.testdelay;
if (delayValue === 1) {
await ref.update({ testoutput: "Ran with no delay." });
logging.log("Ran with no delay.");
} else if (delayValue === 2) {
// Schedule a delayed update using Cloud Scheduler or Cloud Pub/Sub
await scheduleDelayedUpdate(ref, "Ran with a 60-second delay.", 60);
} else if (delayValue === 3) {
// Schedule a delayed update using Cloud Scheduler or Cloud Pub/Sub
await scheduleDelayedUpdate(ref, "Ran with a 120-second delay.", 120);
} else {
await ref.update({ testoutput: "Invalid delay value. No delay applied." });
logging.log("Invalid delay value. No delay applied.");
}
} catch (error) {
await ref.update({ testoutput: "An error occurred: " + error.message });
logging.error("An error occurred: " + error.message);
}
};
const scheduleDelayedUpdate = async (ref, message, delaySeconds) => {
// Use Cloud Scheduler or Cloud Pub/Sub to schedule the delayed update
// Example code for using Cloud Scheduler
// Replace 'YOUR_PROJECT_ID' with your actual project ID
const projectId = 'YOUR_PROJECT_ID';
const scheduler = require('@google-cloud/scheduler');
const client = new scheduler.CloudSchedulerClient();
const parent = client.locationPath(projectId, 'us-central1');
const job = {
name: `delayed-update-${Date.now()}`,
schedule: `*/${delaySeconds} * * * *`, // Run every delaySeconds minutes
timeZone: 'America/Los_Angeles',
httpTarget: {
uri: `https://${projectId}.cloudfunctions.net/updateFunction?ref=${ref.path}&message=${message}`,
httpMethod: 'POST',
},
};
await client.createJob({
parent: parent,
job: job,
});
}; |
Beta Was this translation helpful? Give feedback.
-
I am testing a simple scenario for something and am stumped by these results:
SO, that brings me to my question - why does 2/3 not work on import but it does when manually run?
Here is the code for testoutput (note logs are also matching the results and no errors being noted):
const derivative: Derivative = async ({ row, ref, db, storage, auth, logging }) => {
try {
// Add a delay based on the value in the 'testdelay' column
const delayValue = row.testdelay;
if (delayValue === 1) {
await ref.update({ testoutput: "Ran with no delay." });
logging.log("Ran with no delay.");
} else if (delayValue === 2) {
await new Promise((resolve) => setTimeout(resolve, 60000)); // 60 seconds
await ref.update({ testoutput: "Ran with a 60-second delay." });
logging.log("Ran with a 60-second delay.");
} else if (delayValue === 3) {
await new Promise((resolve) => setTimeout(resolve, 120000)); // 120 seconds
await ref.update({ testoutput: "Ran with a 120-second delay." });
logging.log("Ran with a 120-second delay.");
} else {
await ref.update({ testoutput: "Invalid delay value. No delay applied." });
logging.log("Invalid delay value. No delay applied.");
}
} catch (error) {
await ref.update({ testoutput: "An error occurred: " + error.message });
logging.error("An error occurred: " + error.message);
}
};
Beta Was this translation helpful? Give feedback.
All reactions