How to run scheduled reports - CRON Jobs
Description
Define server-side scheduled tasks that run on an interval or CRON expression by calling an .a5w page that executes code on the Alpha Anywhere Application Server.
Overview
You can define CRON Jobs in Alpha Anywhere to run server-side tasks on a schedule. A CRON job calls an *.a5w* page that runs code on the server (typically Xbasic, but it can also call Node, Python, or other server-side technologies).
Typical use cases include running a report every night, emailing summaries to users, or doing periodic maintenance tasks. Previously, scheduled jobs could be created using the Windows Task Scheduler service, but that service is not available on Alpha Cloud. CRON jobs provide a portable, Alpha-managed alternative.
Creating a CRON job
To define a CRON job:
In the Web Projects control panel, select the CRON Jobs category.
Click New to create a new job (or edit an existing job).
In the CRON Job Editor, specify the job name, schedule, and the *.a5w* page that runs when the job executes.
Scheduling a CRON job
Each job has a Schedule section where you choose how often the job runs. You can either:
Select Interval and run the job every N Minutes, Hours, or Days (for example, every 15 minutes).
or
Select CRON expression and specify a standard CRON expression that defines the schedule.
NOTE You cannot define an interval shorter than 15 minutes.
Using CRON expressions
A CRON expression is a standard way to specify when a CRON job should run. The expression consists of five space-separated fields (minute, hour, day of month, month, and day of week).
In the CRON Job Editor, choose the CRON expression option to enter an expression directly.
Click Build CRON Expression to open the CRON Expression builder dialog. The builder shows a human-readable description of the current expression and helps you construct valid schedules.
You can also click Sample CRON Expressions to see a list of common schedules and their corresponding expressions.
Specifying the handler .a5w page
Every CRON job calls an *.a5w* page when it runs. The *.a5w* page should contain the server-side code that performs the scheduled task, typically an Xbasic code block.
Because the code runs on the server, it can use any of the usual server APIs for working with data, generating reports, sending email, or calling external services.
Registering CRON jobs after publish
After publishing a project that contains one or more CRON jobs, you must register the jobs on the target server. Registration is performed by calling the special system page __a5RegisterCronJobs.a5w in the published application.
For example, if your application is published at https://mydomain.com/, you would register the jobs by requesting:
https://mydomain.com/__a5RegisterCronJobs.a5w
The page returns a message indicating how many CRON jobs were registered.
CRON Job Log system table
The CRON feature uses a system table named CRON Job Log to track the success or failure of each job run. The table is configured in Project Properties and is automatically created the first time you define a CRON job if it does not already exist.
When a CRON job fails because of an error in the Xbasic code, Alpha Anywhere automatically inserts a new entry into the CRON Job Log table.
When a job succeeds, you should update the log yourself by calling the helper function a5_updateCRONLog() from the code in your *.a5w* page.
Sample .a5w CRON job handler
The example below shows an *.a5w* page that prints a report, emails it to a list of recipients using email_send_sparkpost(), and then records the result in the CRON Job Log with a5_updateCRONLog().
<%a5 dim report as c = "report1.a5rpt" dim fn as c = a5_default_path + chr(92) + report dim reportFn as c = a5w_report_saveas(fn,"PDF") dim ms as p ms.message_html = "Here is your report" ms.send_to = "[email protected],[email protected],[email protected]" ms.subject = "Your report" ms.from_alias = "AlphaSoftware" ms.from_email = "[email protected]" ms.from_name = "AlphaSoftware" ms.attachments = reportFn dim key as c = "your_sparkpost_key" dim p as p p = email_send_sparkpost(key, ms) ' Update the CRON Job Log table. if p.error = .f. then a5_updateCRONLog(a5_getProjectGuid(), "name_of_cron_job", now(), "success") else a5_updateCRONLog(a5_getProjectGuid(), "name_of_cron_job", now(), "failed") end if %a5
How to Define CRON Jobs
In this video we show how you can define CRON jobs that run server-side code on a schedule.
Xbasic CRON Expressions
These Xbasic functions make it easy to test expressions and show human-readable descriptions in your application's UI.
See Also




