pulyaevskiy / firebase-functions-interop

Firebase Functions Interop Library for Dart.
BSD 3-Clause "New" or "Revised" License
191 stars 52 forks source link

Figuring out how to schedule cron functions #50

Closed Skquark closed 5 years ago

Skquark commented 5 years ago

I love using this so much more than trying to work in javascript instead of Dart, so thanks. I'm using this to check all the upcoming events and send notifications to the members. I got almost everything done, and it should be working, but the hard part was creating a function[] for a scheduledTask to check the event start times. I got it implemented with this cron package https://pub.dartlang.org/packages/cron and it was really easy to use in my main, I can't figure out how to run it as a cloud function. This was my attempt:

functions['scheduleCheck'] = cron.schedule(new Schedule.parse('*/$minuteInterval * * * *'), () async {
    print('Tick every $minuteInterval minutes');
}

However it wasn't as simple as I thought. Is there a trick I might be missing? I'd be so happy to see this cloud function hold together, perfect companion for them Flutter apps. If I were doing it in the javaScript method, I would create the cron.yaml with the repeating event as in this example: https://github.com/FirebaseExtended/functions-cron but I don't know how that translates in Dart. To send out the Firestore Cloud Messaging, I'm using this https://pub.dartlang.org/packages/fcm_push which seems to be compatible so far, but haven't pushed tested yet. I was expecting a FCM method built into this, but was glad to find the solution. Anyways, thanks for this interop, I'm feeling safe using it..

Skquark commented 5 years ago

Update, it actually does work with the cron.schedule command, but not in it's own cloud function task. When I run the cron function from main(), it's actually running multiple times with each functions[''] = job that I guess runs the whole script for all the independent cloud functions. Not what I expected, but understanding it better now. Now I'm trying to figure out how to make a custom Cloud Function task that only runs on init to start the cron job without it getting started on all the function threads.

I'm also facing problems using the Dart fcm_push or the pubsub_push. I installed the gcloud, googleapis and googleapis_auth dependencies, and set up the ServiceAccountCredentials to get the AuthClient, and everything seems to be done right, however I get this uncaught exception in the log: ReferenceError: XMLHttpRequest is not defined at /user_code/build/node/main.dart.js:9351:7 I just can't get past that, no matter what I tried. Is there a trick I'm missing, or a better way to push notifications to subscribers or user token ids using this interop? Thanks. Edit: I'm posting the second part question in separate issue, but first problem still applies..

pulyaevskiy commented 5 years ago

Sorry, I didn't have time to look into this.

Looks like Firebase added support for scheduled functions as described here: https://firebase.google.com/docs/functions/schedule-functions

I will look into adding support for this over the weekend.

jruillier commented 5 years ago

See pull request #53

pulyaevskiy commented 5 years ago

53 has been merged and published as 1.0.2.

Closing this as resolved. Feel free to open a new issue if you still have any questions.