Cloud Jobs in Flow

A Cloud Job is some code that runs on the Flow servers, following a schedule that you specify. Cloud Jobs are used whenever you want automatic tasks to be performed. An example could be that you want to email to a team member a nightly report summarizing all scans that were collected throughout the day. It could also be a chore that runs as a Cloud Job and at the end of each week, removes all entries from the Cloud DB that are no longer needed.

Available API

You currently have access to the Flow API made specifically for Cloud Jobs:

Configuration

All you have to do is give a name, specify the schedule and type in some code that you want Flow to run on the defined schedule.

Scheduling Cloud Jobs: CRON expressions

Cloud Job understands the standard crontab format, as well as the following nonstandard predefined scheduling definitions:

Definition Description Equivalent to
@yearly Run once a year 0 0 1 1 *
@monthly Run once a month at midnight of the first day of the month 0 0 1 * *
@weekly Run once a week at midnight on Sunday morning 0 0 * * 0
@daily Run once a day at midnight 0 0 * * *
@hourly Run once an hour at the beginning of the hour 0 * * * *
┌───────────── min (0 - 59)
│ ┌────────────── hour (0 - 23)
│ │ ┌─────────────── day of month (1 - 31)
│ │ │ ┌──────────────── month (1 - 12)
│ │ │ │ ┌───────────────── day of week (0 - 6) (0 to 6 are Sunday to
│ │ │ │ │                  Saturday, or use names; 7 is also Sunday)
│ │ │ │ │
│ │ │ │ │
* * * * *

Note: Cloud Jobs can't run more frequently than once an hour

Examples

Fetching database objects

A simple example showing how to fetch objects that match a query from the database, log them to the console (accessible from the Cloud Job list) and finish the execution of the Cloud Job. This example is simply a demonstration of the basic API to get you familiar with Cloud Job.

Uses the following APIs:

Scandit.Db.Category.find({level: {$eq: 0}}).then(result => {
    let categoryNames = result.map(category => category.name);
    log(`Do something with these: ${categoryNames}`);
    done();
});

Emailing a nightly report

A bit more complex example would be the following: let's assume that a team performs maintenance work on vending machines and scans the machine's barcode when they're finished with it. At the end of each day, you'd like to email out a report that lists all machines on which maintenance work was performed.

Uses the following APIs:

let today = (new Date()).toDateString();
Scandit.Db.Machines.find({lastMaintenanceDate: {$eq: today}}).then(machines => {
  machines = machines.map(machines => machine.uniqueMachineIdentifier);
  Scandit.Fs.writeTempFile('machines.csv', machines.join('\n'))
  .then(path => {
    Scandit.Email.send(
      `Machine maintenance list (${today})`, // email subject
      'CSV attached',                        // email body
      ['youremail@example.com'],             // recipient list
      {                                      // options
        attachments: {file1: path},
        useDefaultTemplate: false
      })
    .then(done);
  });
});

Aggregating data from multiple database classes and generating a CSV to be sent via email

An even more complex example showing how to send an email with a CSV of the five categories with most products: we have to fetch all categories, associate the number of products that belong to them, get only the five that has the most products, create a CSV file and send out an email with it.

Uses the following APIs:

Scandit.Db.Category.all().then(result => {
  let promises = result.map(category => {
    return Scandit.Db.Product.count({category: {$eq: category.path}})
           .then(count => `${category.name},${count}`);
  });
  Promise.all(promises).then(results => {
    let topCategories = results.sort((a,b) => a.split(',')[1] - b.split(',')[1])
                               .slice(-5)
                               .reverse();
    log(topCategories);

    Scandit.Fs.writeTempFile('topcategories.csv', topCategories.join('\n'))
    .then(path => {
      Scandit.Email.send(
        'Top Categories',                 // email subject
        'CSV attached',                   // email body
        ['youremail@example.com'],        // recipient list
        {                                 // options
          attachments: {file1: path},
          useDefaultTemplate: false
        })
      .then(done);
    });
  });
});