Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Easily Automate Your App by Scheduling a Server Code Script to Run Periodically

DZone's Guide to

Easily Automate Your App by Scheduling a Server Code Script to Run Periodically

Add simple push notifications to your Appery.io app with this simple tip

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

Appery.io Server Code enables mobile developers to write any custom app logic that is automatically exposed via a REST API. You develop the script using JavaScript which is nice because you don’t have to learn a new language. The Server Code provides built-in APIs to make it simple to integrate with other Appery.io services. For example, you can query the Appery.io Databasesend a push notification, and invoke any 3rd party REST API service.

For example, you want to develop a script that will query a products database. If an inventory for a particular product falls below a set number, you want to send a push message to the manager notifying him or her that inventory is low.

This is not something you want to check manually, this is something you want to automate. Running the script periodically is actually very simple once you have a finished script. I’m going to show you how to do that starting with the script first

var databaseId = "5625aaf.....";
var collectionName = "Products";
var result = {};

var params = {};
params.criteria = {
  'quantity': {
    "$lt": 5
  }
};

result.query = Collection.query(databaseId, collectionName, params);

var msgText = [];
var productCount = result.query.length;
console.log(result.query);

if (productCount !== 0) {
  for (var i = 0; i < productCount; i++) {
    msgText.push(result.query[i].name + ": " + result.query[i].quantity);
  }

  PN.send("bb26439d.....", {
     "message": msgText.toString();
  });
}
response.success(result.query, "application/json");

Let’s review the script, it’s very simple:

  • Line 1 & 2: you set the database API key and the collection name.
  • Line 5: you set the query. This query will check if quantity for any product is less then 5.
  • Line 12: is where you query the database using the built-in Server Code API.
    • Because a number of products can have a low inventory, you iterate over the result and create the message to send.
  • Line 23: you send a push message. The id is Push API configured for an app that the manager installed.
  • At the end the script returns a response.

You can test the script but first let’s look at the database so you know which product has low inventory. This is the database:

Screen Shot 2015-10-22 at 11.10.56 AM

Products Database

Based on the query parameters on line 8, Coffee has low inventory (less than 5). When you test the script, this is the result you will see:

Screen Shot 2015-10-22 at 11.15.12 AM

Testing the script

The result contains Coffee product which has low inventory.

You want this script to run periodically and automatically, say once a day without any user action. You can very easily schedule this script to run once a day via Jobs.

Switch to Jobs tab where you can schedule the script to run periodically:

Screen Shot 2015-10-22 at 11.19.44 AM

Server Code script scheduler

At the top of the page you configure the schedule. In the table below you will see all the scripts that have been scheduled. Use the toggle to set the schedule to run (or not). You can click on the link in Last result to see the the log trace for the last script execution.

Server Code enables you quickly to expose any app logic via a REST API. With Server Code Jobs, you can quickly schedule the script to run periodically.

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:
appery

Published at DZone with permission of Max Katz, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}