Crontab expressions
A Crontab expression is a special string with five components that builds an expression for a job schedule:
For example :
- run job each 10. th minute:
*/10 * * * *
- run job at 23:00 each Monday:
0 23 * * 1
Tip: let https://crontab.guru help you build your expression.
Set up a crontab job
In development mode, navigate to the System_jobs collection in the left menu.
Each record in the System_jobs collection is a separate job. These are used to schedule tasks that invokes your Codehook on the Crontab expression.
Start by adding a new record and enter a description, a Crontab expression and finally the Codehook JavaScript. Check the active checkbox to indicate the this job is running, or uncheck to pause job.
Example Codehook that prints 5 items from a collection to the console:
var runJob = function(req, res) {
db.get("/rest/product", {},{$max: 5}, function(err, data) {
data.map(function(item){
log.debug(item);
});
res.end();
});
}
Using the REST inspector, we can view the output of a Codehook in realtime.
But what if you don't want to be glued to the REST inspector at all times?
That's where the job logs comes in handy.
Logging
You can check the status of any current job in the System_log collection. It shows the res.end(…) output from a job. Any log.debug()
statements can be viewed with the REST inspector.
Conclusion
Background Codehooks in restdb.io are simple to set up and alleviates the need for a separate server. Creating serverless applications with powerful integrations, custom logic and long running processes is now a reality.
Read more about Codehooks in the docs.