This is a straight forward article on how to get started with running scheduled tasks with #Firebase. There is actually no built in support for you to set up timers or schedules similar to a cron job. But that does not mean that you can't get scheduling into your Firebase project
<p dir="auto"><center><br /> <a href="https://steemit.com/firebase/@dennisalund/bagaimana-caranya-untuk-bikin-scheduling-dengan-cloud-functions-for-firebase" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Klik di sini untuk baca artikel ini dalam bahasa Indonesia<br /> <p dir="auto">You can leverage integration with Google App Engine, which has a very <a href="https://cloud.google.com/appengine/docs/standard/python/config/cron" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">easy to manage task scheduler and Python is a good choice of language since it's easy to write very compact yet powerful applications. <p dir="auto">In this example we'll be sending push notifications from Google App Engine's scheduler to a HTTPS Cloud Function endpoint. And, oh... did I mention that we'll accomplish this in <strong>less than 40 lines of code, including configuration files!!! <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/DQmPGJZoc1TWM4okQGrg2BzffcRB1QHuBNUh5jj2nrNqzix/dennis-got-some-serious-design-skillz.png" alt="Firebase o clock" srcset="https://images.hive.blog/768x0/https://steemitimages.com/DQmPGJZoc1TWM4okQGrg2BzffcRB1QHuBNUh5jj2nrNqzix/dennis-got-some-serious-design-skillz.png 1x, https://images.hive.blog/1536x0/https://steemitimages.com/DQmPGJZoc1TWM4okQGrg2BzffcRB1QHuBNUh5jj2nrNqzix/dennis-got-some-serious-design-skillz.png 2x" /><br /> Tik tok let's go <h1>Google App Engine <p dir="auto">Make sure that you know the basics behind Google App Engine and <a href="https://cloud.google.com/appengine/docs/standard/python/getting-started/python-standard-env" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">how to set up a project. You should be able to copy paste all the code from this article and get it up and running, even if you just skim through the documentation... let me just clarify: skim Google's documentation, read my article with tender love and care ;-) <h2>app.yaml <p dir="auto">This file is the main configuration file for you GAE project. The only part that you should change in this file is the <code>SecretApiKeypart on the last row. Replace that with some random string, such as a <a href="https://www.guidgenerator.com" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">random GUID makes a pretty good API key. But remember to keep it secret. You will use that same string later to configure your cloud functions. <pre><code>runtime: python27 api_version: 1 threadsafe: true handlers: - url: /cron script: main.app login: admin env_variables: CRON_API_KEY: 'SecretApiKey' <h2>cron.yaml <p dir="auto">This file is the cron job configuration. The frequency configuration <a href="https://cloud.google.com/appengine/docs/standard/python/config/cronref" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">really reads like natural language. It's not a mistake. <pre><code>cron: - description: "Run cron job" url: /cron schedule: every 30 minutes <h2>main.app <p dir="auto">Finally, the good part. This is the only actual code that we have to write to execute regular push notifications to our Firebase project. <p dir="auto">The script is fishing out the your Google Cloud Platform project automatically, so it's important that you're targeting a cloud function with the same GCP project id. Otherwise you have to edit the file and replace with the URL of your cloud function. <pre><code>from google.appengine.api import urlfetch import webapp2 import urllib import os class Sheduler(webapp2.RequestHandler): def get(self): appId = os.environ.get('GCLOUD_PROJECT', 'n-a') try: response = urlfetch.fetch( url='https://us-central1-' + appId + '.cloudfunctions.net/cronEndpoint', payload=urllib.urlencode({'key': os.environ.get('CRON_API_KEY')}), method=urlfetch.POST, headers={'Content-Type': 'application/x-www-form-urlencoded'}) self.response.set_status(response.status_code, response.content) except urlfetch.Error: self.response.set_status(500, 'Caught exception while calling cloud function') app = webapp2.WSGIApplication([('/cron', Sheduler)], debug=True) <h1>Cloud Function <p dir="auto">Now we're actually already outside the scope of the scheduler. So, we've just created a cloud hosted, zero downtime scheduled webhook service that runs absolutely free (free as in gratis)... all in less than 40 lines of code. Give yourself a pat on the back. <p dir="auto">The last part is to create a Cloud Function endpoint that consumes the request. <p dir="auto">First we need to also configure Firebase with the API key that we created in the GAE configuration. <pre><code>$ firebase functions:config:set cron.apikey=SecretApiKey <h2>index.ts <p dir="auto">Please note that the name of the function <code>cronEndpoint must match the URL that you configured in the Python scheduler above. You can rename this to anything you want as long as you update it in both places. <pre><code>export const cronEndpoint = functions.https.onRequest((request, response) => { if (functions.config().cron.apikey !== request.body.key) { response.status(401).send("I'm sorry Dave, I'm afraid I can't do that"); return; } response.status(200).send("The time is now."); }); <h1>Finally <p dir="auto">That's all folks, if you want to read more about scheduling, then please check out the <a href="https://firebase.googleblog.com/2017/03/how-to-schedule-cron-jobs-with-cloud.html" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Firebase blog for how to set up scheduling with pub/sub or my <a href="https://medium.com/evenbit/1-on-my-christmas-wish-list-for-firebase-cloud-functions-d031994618d9" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">original article on Medium, which was re-written into this article to be more timeless. <p dir="auto">You can find all this code in a <a href="https://github.com/DennisAlund/firebase-cron" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">github repo that I am not really maintaining. But I'm happy and grateful for feedback and comments that can help to improve the article or scripts. <p dir="auto">If you want to keep learning about Firebase, then please <a href="https://steemit.com/@dennisalund" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">follow me on Steemit<span> or get your feed tuned in to the <a href="/trending/firebase"> #Firebase tag.
Hi! I'm a robot! I noticed that you took your payout as 100% power-up and I just wanted to let you know that right now, you will potentially make a LOT more money if you take the 50/50 option that pays you some SBD. You can read about why this is true here. If you want me to leave you alone, just reply with the word STOP.