The data browser is built for Chrome, Firefox, Safari, and IE9 and above. Please upgrade your browser, or download Google Chrome to get the best experience.
Back to Questions

Is there any way to schedule a Cloud Code function to be invoked periodically?

83 votes     9 answers     9.42k views     

83

Keywords: cron, background, job

Is there a way to have you guys fire off one of my Cloud Code functions on a predetermined schedule?

Something as simply as hourly, daily, weekly, monthly would be fantastic.

I think this falls in line with your mission of us not needing servers.

Example: I have a game in which a weekly tournament must have a query run to determine the winners. I currently have to run this query manually (blech! I'll forget!); or run my server which calls into the backend.

Example: I have a turn-based game in which matches that have a player that decides to not show up any more after a few days should auto-forfeit.

Thanks!

9 Answers

37

This would be a great feature to have in Cloud Code. For now, you'd need to set up a cronjob somewhere else, and I'll update this answer if we release such a feature for Cloud Code.

ok thanks

- Krish over 1 year ago

hope to see this feature soon..!!

- Newton Guimarães Filho over 1 year ago

Any update about this possible feature? Like, if you are working on it or have a possible date?

- Newton Guimarães Filho over 1 year ago

We generally don't post ETAs for unreleased features - if we miss that date, people will be disappointed.

- Héctor Ramos over 1 year ago

+1 on this feature.

- Juan Bermudez over 1 year ago

would be nice thanks.

- Nicolas Eypert about 1 year ago

+1 This would be a great feature

- Jose Ines Cantu Arrambide about 1 year ago

+1 on this

- Jess Bowers about 1 year ago

Hector can you give me some example about where I should write my cron script to do some task periodically . You have answered that we need to set up a cronjob somewhere else to schedule some task periodically,It would be a great favour if you give example that where exactly we should write cron script.

- Rapidsoft about 1 year ago

Our Cloud Code guide (https://www.parse.com/docs/cloud_code_guide#functions) contains an example of how you can use curl to call a Cloud Function via the REST API. Set this up in a shell script that's called from a cronjob.

- Héctor Ramos about 1 year ago

ashu, the code from simone (see at the bottom of the thread ) works. Mine was missing some parts. also i have a sample worker (iron.io) in python working (will post it later as Im from home now)

- Nicolas Eypert about 1 year ago
8

I use iron.io for this purpose. That way I'm still "serverless". Disclosure: I don't work for them, I'm just a happy customer of both iron.io and parse.

is it ironWorker ?

- Nicolas Eypert about 1 year ago

Yes, I use an IronWorker script written in Python. It checks an rss feed for new entries, and when one is found, sends off a push notification via Parse. The script also stores a list of processed rss entries in Parse so duplicate notifications don't get sent.

- James Richards about 1 year ago

k thanks. iron.io looks great. I tried to use it thru javascript (node.js stuff), Althrough I'm a long time game developer, I'm really new to this web service / javascript stuff.

after browsing a bit I assembled this source (see in next comment):

I tried to launch a cloud code function (hello which prints to console).

but looks like it cannot reach the api.parse.com thru https, looks like a authentification issue to me.

Any idea to kickstart me ? Thanks in advance Nick

- Nicolas Eypert about 1 year ago

var http = require('http'); var net = require('net');

var post_options = {

  host: 'api.parse.com',
  port: '443',
  path: '/1/classes/hello',
  method: 'POST',
  headers: {
      'Content-Type': 'application/json',
      'X-Parse-Application-Id': 'MYAPPKEY',
      'X-Parse-REST-API-Key': 'MYRESTKEY'
  }};

http.request(post_options, function(res) { console.log("Got response: " + res.statusCode); }).on('error', function(e) { console.log("Got error: " + e.message); });

- Nicolas Eypert about 1 year ago
4

hi, this is a complete working example of a Cloud Code function call which takes no arguments:

var https = require('https'); 
var net = require('net'); 

var headers = { 
    'Content-Type': 'application/json', 
    'X-Parse-Application-Id': '***YOUR APP ID***', 
    'X-Parse-REST-API-Key': '***YOUR REST API KEY***' 
};

var post_options = { 
    host: 'api.parse.com', 
    port: '443', 
    path: '/1/functions/***YOUR FUNCTION NAME***', 
    method: 'POST', 
    headers: headers
}; 

var post_req = https.request(post_options, function(res) {
    console.log("Parse post statusCode: ", res.statusCode);

    res.on('data', function(d) {
      process.stdout.write(d);
    });

});

post_req.write("{}");
post_req.end();
post_req.on('error', function(e) {
    console.error(e);
});

sorry, I forgot to mention that this is of course this is an ironWorker example

- GoApp about 1 year ago

thanks, now I see the part I was missing (actually sending the request). thx for helping.

- Nicolas Eypert about 1 year ago
3

+1, this would really help. Otherwise, I'm forced to setup a second server to do this, and that really defeats the purpose of using Parse. It's not the end of the world, but it would be awesome-sauce to have it built-in.

2

Python Worker which works for me.

    import json, urllib2

print "Hello!"

in_data = {"score": "666"}
in_headers = {
          'Content-Type': "application/json",
          'X-Parse-Application-Id': "APPID",
          'X-Parse-REST-API-Key': "PARSERESTKEY"
      }

req = urllib2.Request("https://api.parse.com/1/classes/GameScore", data=json.dumps(in_data), headers= in_headers)

print urllib2.urlopen(req).read()
2

this is something I'd love to see, also.

Example: My app needs to send a push notification to a group of users a certain number of hours before an event. I would like to have a job run hourly that would query for events about to happen and trigger the necessary notifications.

I would like to see this as well, I'm looking to do something along the same lines as Drew.

- Joe over 1 year ago
0

Hi,

this is something that we would very much enjoy having built in

Bye,

Jean

0

1+ On this feature. Much valuable.

0

I'd love to see this happen for an app I'm working on. If I could schedule a function to run some time after (say) a user uploads some data to Parse, I'd be able to build the entire back end using Parse - which would make life significantly easier for me!