The Zen documentation gives a small synopsis on running and monitoring a background process from a Zen Page.
Would someone happen to have an example of this?
Thanks.
Randy L Stewart
Sales Engineer
InterSystems Corporation
Here is example code for running and monitoring a background process (job, task, etc) in the Caché ZENDemo package in the Samples Namespace. I am using Cache for Windows (Intel) 2007.1 (Build 369) Fri Jun 15 2007 15:25:42 EDT.
The functionality this example provides is the ability to start a background process and monitor the process during the execution. The user can also navigate away from the page and come back and the background process monitoring will resume (this is not built in, I had to add it).
The following modifications to the ZENDemo.FormDemo class which is attached.
A text field is placed just under the Memo field to display background progress.
<text label="BG
Task:" id="Monitor" name="Monitor"
size="100%" />
<spacer height="5"
/>
A new button is added above the save button to begin the background process.
<spacer width="5"/>
<button caption="Background"
id="BtnBackground" containerStyle="font-size=x-small;"
onclick="zenPage.BtnBackgroundClicked()"/>
Then the following methods are added at the end of the class. The background process is just a dummy process that wastes some time. Real work can be easily substituted. Zen currently only supports tracking one BG process at a time and I am working on extending this.
/// [RLS] -
Run background task
Method BtnBackgroundClicked() [ ZenMethod ]
{
// This is the method
called by my "Background" button
Set ErrorMessage = ""
Set BackgroundMethod = "MyBGTask"
// Start the
BackgroundMethod ClassMethod with one empty parameter
Set Status = ##this.%RunBackgroundMethod(BackgroundMethod,"")
// Report on intial BG
task status
// Subsequent updates
are handled in the %OnMonitorBackgroundTask callback
if (Status)
{
&js<var monitor = zenPage.getComponentById('Monitor');;>
&js<monitor.setValue('Background task #(BackgroundMethod)# started!');>
}
else
{
&js<alert('BackgroundMethod #(BackgroundMethod)# failed to start!');>
}
}
/// [RLS] - Run background task
ClassMethod MyBGTask(arg As %String) [ ZenMethod
]
{
// Run background
process here or
// Call additional
outside Class methods
// Pprovide for the
update of ^ZEN.BackgroundStatus in called methods,
// Either assume that
^ZEN.BackgroundStatus will be the global to update or pass
"^ZEN.BackgroundStatus" as a parameter to the method
// Do anything you want
to here including calling out to any desired methods or classmethods
// Do nothing for 20
seconds in 5 second increments
Do ..%SetBackgroundMethodStatus("Running", 0)
// Pause 5 seconds
Hang 10
Do ..%SetBackgroundMethodStatus("Pass
1", 33)
// Pause 5 seconds
Hang 10
Do ..%SetBackgroundMethodStatus("Pass
2",66)
// Pause 5 seconds
Hang 10
Do ..%SetBackgroundMethodStatus("Finished", 100)
// Pause 5 seconds
Hang 10
Do ..%EndBackgroundMethod()
}
/// [RLS] - Update Monitor text field
/// This server-side callback method
is called whenever the client
/// calls to monitor the current
background task.
/// Typically a subclass uses this to
send back JavaScript to update
/// a progress bar.
ClassMethod %OnMonitorBackgroundTask(pTaskID As %String, pStatus As %String, pPercentComplete As %Float)
{
// The ZEN page
automatically calls this callback method at the interval specified by the
//
backgroundTimerInterval property in milliseconds (default=1000 which is 1
second)
// Here I have added
code to update the Monitor text control on this page.
&js<var monitor = zenPage.getComponentById('Monitor');>
&js<monitor.setValue('Background task #(pTaskID)# #(pStatus)#!');>
}
/// [RLS] - Clear Monitor text field
/// This server-side callback method
is called whenever the client
/// calls to monitor the current
background task and the task is complete.
/// Typically a subclass uses this to
send back JavaScript to update
/// the page.
ClassMethod %OnFinishBackgroundTask(pTaskID As %String)
{
// The ZEN page
automatically calls this callback method when the BG task completes
// Here I have added
code to clear the Monitor text control on this page.
&js<var monitor = zenPage.getComponentById('Monitor');>
&js<monitor.setValue('');>
}
/// This method is called when the
page loads, checks for an existing background task
/// and triggers the background timer
if one is found
ClassMethod CheckBackground() [ ZenMethod ]
{
set t="", tJobID=""
set t=$Q(^ZEN.BackgroundStatus(""))
if ((%zenContext="method")&&(""'=t)) {
set tJobID=$QS(t,1)
if (""'=tJobID) {
&js<
zenPage.triggerBackgroundTimer('#(tJobID)#');
>
}
}
}
/// This method is called to trigger
the Background task timer when background tasks are active.
Method triggerBackgroundTimer(taskid) [ Language = javascript ]
{
this._bgTaskID = taskid;
this._bgTimerID = self.setTimeout("zenPage.backgroundTimerHandler()",this.backgroundTimerInterval);
}
/// This client event, if present, is
fired when the page is loaded.
Method onloadHandler() [ Language = javascript ]
{
zenPage.CheckBackground()
}
Randy,
Thank you – this is great!
Would you be willing to create an article in the Code Collection for this? It would make it easier for people to find this code later via browsing for examples.
The Code Collection table of contents article can be found here:
http://groups.google.com/group/InterSystems-ZEN/web/Code%20Collection?hl=en
Let us know if you have any questions J
Cheers,
Ben
Sure. I will review the Code collection criteria and get it started.
Thank you. It will be as simple as:
Let me know if you have any questions J
Thanks!