View Full Version : Lianja Server Background Scripts on Windows

2014-02-21, 12:07
I've had a few questions about the 'Background Scripts' you can create and run via the Lianja Server on Windows, so for anyone who is interested in using these, I hope this will guide you in their use and design.

I also want to flag up a typo in the example.prg background script shipped in v1.0.0. :o

Background scripts can be used to perform data transformation, data synchronization or other custom operation on your data. Background scripts run with no UI. Each runs as a separate LianjaRunScript process. They are primarily used to perform data collection tasks or batch job tasks such as checking for new or updated data and emailing out notifications to users. Typically a background script will sleep for a period of time and then repeat its work. Background scripts have the standard program extensions of prg (Lianja/VFP script source) / dbo (compiled Lianja/VFP script) and reside in the Services folder (Lianja Server Manager Settings tab, by default 'C:\Lianja\server\services\'). The Background Scripts tab in the Lianja Server Manager allows for the creation, modification and configuration of background scripts.


Firstly, there is a typo in example.prg - sorry! This will be corrected in subsequent releases, but if you want to try it out in the meantime:

Fire up the Lianja Server Manager and go into the Background Scripts tab
Select example
Click Edit... to open the script in Notepad

Change this line:

set default to "C:\Lianja\sqlserver\services"
To this:

set default to "C:\Lianja\server\services"

Save and Exit

Now, open up an Explorer Window for C:\Lianja\server\services and keep an eye on it. Back in the Lianja Server Manager Background Scripts tab, select example and click Start and Yes when prompted.
In your Explorer Window, you should see the creation of two new files: example.dbo and example.active. The example.dbo is the compiled script and example.active is a flag file used by the script to check whether it is running.
In the Background Scripts tab, select example and click Stop and Yes when prompted. You will now see the example.active flag file disappear. The compiled version of the script is kept and regenerated automatically when the source version is changed.

So, have a closer look at the example.prg code - select it and click Edit....

Scroll right down to the bottom of the file - the last line is actually the first executable line:

return (handle_action(_para1, 10, procname()))

When you Start the script in the Lianja Server Manager it is called with the argument 'start', which goes into the parameter '_para1'. This gets passed to the function 'handle_action' along with 10 (the specified sleep time in seconds) and the name of the running script, 'EXAMPLE'.

// function to handle the requested script action
function handle_action(action, sleepTime, scriptName)

Handle_action makes sure you're in the right current working directory then calls the 'start' function within the script.

do case
// Start service
case upper(action) = "START"
result = start()

The 'start' function checks if you already have another instance of the script running by checking for the file example.active, returning if it exists and creating it if it doesn't. It then proceeds to call the 'dowork' function in a loop for as long as the example.active file exists, sleeping in between each call for the specified time.

// Start action
function start( )
// process any work for this script
do while file(p_active_file)
sleep p_sleep_seconds
The 'dowork' function is the part that does whatever your script is designed to do. You can use example.prg as a template - just copy it and put your code into the dowork function.

// this is the function that will be executed by this script to handle its work
function doWork( )

// TODO: add your own custom code here

When you Stop the script in the Lianja Server Manager, it is called with the argument 'stop'. This again gets passed to 'handle_action', which in turn, calls the stop function. The stop function deletes the example.active flag file causing the loop in 'start' to exit and the script to terminate.