I have a firebase scheduled function with the crontab expression: 0 2-22/2 * * *
As I understand it, this function should be run every two hours every day, apart from at midnight.
However, the function was just triggered at midnight, so I must be doing something wrong.
Here is the code I wrote for the function scheduling:
exports.everyTwoHourRunGame = functions
.runWith({
timeoutSeconds: 540,
})
.pubsub.schedule("0 2-22/2 * * *")
.onRun(async () => {
On the firebase website, I can view the logs for the function and see that it ran the second after 00:00, other than this it has been working fine, executing every two hours as expected.
Any help would be much appreciated.
Related
I am wondering how to write a proper code for runnning a job on particular date on Firebase.
For cron-type job, I know these situation should be realized as follows:
exports.scheduledFunctionCrontab = functions.pubsub.schedule('5 11 * * *')
.timeZone('America/New_York') // Users can choose timezone - default is America/Los_Angeles
.onRun((context) => {
console.log('This will be run every day at 11:05 AM Eastern!');
return null;
});
but this for "repeated" job and not for "one-time" job.
Is there any way to run particular job on a particular date using Firebase?
Thanks!
I found a great answer here by myself.
https://medium.com/firebase-developers/how-to-schedule-a-cloud-function-to-run-in-the-future-in-order-to-build-a-firestore-document-ttl-754f9bf3214a
Keeping a cron job pub/sub function (functions.pubsub.schedule), within a cloud function (functions.https.OnRequest) and exporting it, does not execute.
A complete example is as follows:
export const sayHelloWhen = functions.https.onRequest((request, response) => {
cors(request, response, () => {
const scheduleExpression = request.body.data.scheduleExpression;
functions.logger.log(`Called sayHelloWhen with ${scheduleExpression}`);
functions.pubsub.schedule(scheduleExpression).onRun((context) => {
functions.logger.log(`Executed sayHelloWhen with ${scheduleExpression}`)
});
response.send({
status: "success",
data: `scheduled at ${scheduleExpression}`
})
})
})
The problem is pub/sub does not trigger. Other codes are executed.
I would like to have HTTP request body scheduleExpression bring into pubsub.schedule's parameter. I don't want a static schedule expression in corn job.
In client, I would like to define a schedule expression in client side as follows:
function scheduleFunction() {
const functions = getFunctions();
const sayHello = httpsCallable(functions, "sayHelloWhen");
sayHello({ scheduleExpression: "every 1 minute" }).then((result) => {
// const data = result.data;
console.log("Result:", result);
});
}
The example below works only for a static schedule expression, meaning that a cloud function itself has a fixed schedule expression:
exports.scheduledFunction = functions.pubsub.schedule('every 5 minutes').onRun((context) => {
console.log('This will be run every 5 minutes!');
return null;
});
It can be exported as cron job trigger and it executes.
But keeping pub/sub cron job function, within onRequest cloud function, as in the first code example, does not execute.
I think it is very interesting what you are trying to do, but I would like to point out that you are missing some steps that could cause your app not to execute the way you need it to.
First, you need to Terminate your HTTP functions
Always end an HTTP function with send(), redirect(), or end(). Otherwise, your function might continue to run and be forcibly terminated by the system. See also Sync, Async and Promises.
When you do not terminate them, you might end up in a deeper level in which the following code will not execute unless the previous code has finished. I would also like to say I have not found any application with nested functions like you are doing.
In the Sync, async, and promises page you can find a very explicative video to understand the lifecycle of your functions, and in the How promises work with functions section we have:
When you return a JavaScript promise to a function, that function keeps running until the promise is resolved or rejected. To indicate that a function has completed its work successfully, the promise should be resolved. To indicate an error, the promise should be rejected. This means you only need to handle errors that you want to.
In addition to all this, I would suggest using a separate file to Organize multiple functions for a more organized code, but this is only a suggestion not really necessary.
Finally, I am concerned about the scheduledExpression parameter, I think if none of the above works, you might want to check and share what this value is.
My firebase collection that I'm trying to query is literally empty. Zero documents inside. Even then, when querying it, I'm getting this error
I'm doing this inside a LAMBDA function on AWS. I know that the query works because it does return results sometimes but it's very random. Mostly it's just coming up with this error
Here are my lambda logs with following ENV variables turned on
GRPC_TRACE=all
GRPC_VERBOSITY=DEBUG
I have even tried this as I found this online somewhere but it didn't make any difference
db.settings({
clientConfig: {
interfaces: {
'google.firestore.v1.Firestore': {
methods: {
RunQuery: {
timeout_millis: 5 * 60 * 1000
}
}
}
}
}
});
Here's what my query code looks like
let snap = await db.collection('notifications').where("siteID", "==", msg.siteId).where("procCode", "==", code).where("aptNum", "==", msg.affectedRows[0].after.AptNum).get();
Here's the output I get eventually. Not even in the same lambda execution but in a separate one which is also strange.
I figured out what my issue was here. I was calling the Firebase API inside an async function but the caller of that function wasn't "await"ing that call which led to this. It all works now.
I have written a scheduled function like described here: https://firebase.google.com/docs/functions/schedule-functions
How can I test this function now? When I wrap this function (as I would with any other cloud function), there is an error that says: "Property 'wrap' does not exist on type 'TestFunction'."
const functionWrapper = test.wrap(function);
Is there any other way to test these functions?
One workaround I found is isolating my code in a function and calling that function from the scheduled function. When I test, instead of calling the scheduled function, I call the isolated function directly.
Ex:
export const dailyJob = functions.pubsub
.schedule('0 0 * * *')
.onRun(async context => {
return isolatedFunction();
})
export function isolatedFunction() {
...
}
> firebase functions:shell
> firebase> RUN_NAME_OF_THE_FUCTION()
Not sure since when - but this is my way of handling the scheduler function. The problem I have is I do not have a context nor I do not know how to pass params to these functions.
WIP but at least I can easily manually run it on my local env.
You can write like this
exports.scheduledFunction = () => functions.pubsub.schedule('every 1 minutes').onRun((context) => {
console.log('Start scheduledFunction every 1 minutes');
sendEmail();
return null;
});
async function sendEmail() {
console.log('Start sendEmail');
}
Hi I have a url which imports data from csv to database on wordpress.I want this to run every 2 minutes .Is tried a plugin for this cron job scheduer but this did not work.
How can I write a action script function make this possibe.Please help
You can do the following
First you need to add the 2m interval to the schedule
add_filter('cron_schedules', 'my_schedules');
function my_schedules($schedules)
{
$schedules['once_every_2m'] = array('interval' => 120, 'display' => 'Once every 2 minutes');
return $schedules;
}
Then you add your job using newly created interval
if (!wp_next_scheduled('name_of_your_job'))
{
wp_schedule_event(1481799444, 'once_every_2m', 'name_of_your_job');
}
add_action('name_of_your_job', 'function_that_should_be_executed');
function function_that_should_be_executed()
{
//do what you need to do
}
Also, keep in mind, due to how WP cron works, it might be in accurate with timing. Docs