X
X

Login

Login if you already have an account

LOGIN

Register

Create a new account. join us!

REGISTER

Support

Need some help? Check out our forum.

FORUM

FYI – You can run batch routines (CRON) jobs on FatFractal


As a developer, I want to be able to schedule code to run periodically on my backend so that I can clean up my data, generate reports or any other function that is not generated by an event or API call.

In addition to Event Handlers which execute based on data events and Server Extensions that execute on an API call, FatFractal also supports running backend code on a periodic basis using our Schedule Code capability. Any number of functions can be defined that execute on defined schedules. This has many applications from data administration to reporting, there really is no limit to what you can do with this.

Scheduled Code is defined in the same manner as Event Handlers and Server Extensions in your FFDL (what’s this?). You can define as many scheduled code events to be executed as you wish very easily as follows:

SCHEDULE <task name> <schedule> AS <script type>:<script text>

Where schedule is given in CRON format:

<minute (0-59)> <hour (0-23)> <day of month (1-31)> <month (1-12)> <day of week (0-6)>

For example, if I want to send out a daily report that lists the number of users for my cool new app, I would create a function like this in a file (here called EventHandlers.js):

ff = require('ffef/FatFractal');
exports.userReport = function() {
var n = ff.getArrayFromUri("/FFUser").length;
var msgSubject = "Application user total daily report";
var msgString = "You now have " + n + " users loving your application";
ff.sendEmail({
host: "<mailserver>", port: "<port>",
auth: "<true|false>", authPort: "<port>",
username: "<username>", password: "<password>",
from: "<fromAddress>", to: "<toAddress>",
cc: null, bcc: null, // included for completeness
subject: msgSubject,
text: msgString,
html: null // included for completeness. When supplied, the HTML message is
// sent, the text is also included as fallback if HTML viewing
// is not enabled in the receiver's email client
});
}

And, then to schedule this be sent out daily at midnight, the FFDL would be:

SCHEDULE UserReport 0 0 * * * AS javascript:require(‘scripts/EventHandlers).userReport();

That’s it – an approach that is very consistent with everything else which allows you to easily schedule any code that you like to execute on a defined schedule.

For more details on FFDL, the docs are here.

For more details on Scheduled Code, the docs are here.

Have fun!

Kevin

Contact