Examples of a background task

superman970 
Joined: 2008-03-13
Posts: 9
User is offline
Examples of a background task

I'm thinking about creating a module as a background task. Are there any examples/tutorials you guys could point me to?

Thanks,

superman970

mark  Appistry employee
Joined: 2007-12-21
Posts: 65
User is offline

superman,

I will put something together first thing tomorrow morning.

Mark

mark  Appistry employee
Joined: 2007-12-21
Posts: 65
User is offline

Superman,

This is taking longer than I expected, and I need to get it approved before we can publish it. I will get you more information as soon as I can.

Mark

mark  Appistry employee
Joined: 2007-12-21
Posts: 65
User is offline

Superman970,

A Background Task Tutorial page is in the works, but here is a small portion of what I was able to get from them. As mentioned the Background Task page has to be reviewed prior to publishing so I wanted to give you this information until it is released at a later date.

Background tasks are typically used to run scheduled tasks (like cron), perform database maintenance, wait on a queue for a specific data, respond to various hardware or network activity, and so on. Background tasks are similar to daemons - they have no clients and they get invoked by the Fabric. Because the background tasks have no clients, the request object is not used.

Configuring the Background Task

In order to add a background task to your application, you need to create the task.xml slightly different. By default, your tasks have been using "unlimited" tasks. If you want to have your task run as a background, you must set the task type to 'background'.

background_task.xml

<?xml version='1.0'?>
<!DOCTYPE module SYSTEM "ModuleDefinition.dtd">
<module file='myBackgroundTask.class'  implementation='Java' className='JavaTasks.BackgroundTask'>>
    <task name='queuePull' type='background' instances='1' retryDelay='10000'>
        <method>pullItemFromQueue</method>
    </task>
</module>

Refer to the Task xml documentation for all available options at http://www.appistry.com/developers/wiki/display/eaf38dev/Task+Module+Def....

In this example we are setting the the background task so that each worker has 1 instance running of the background task. You can also set a percentage parameter rather than the instances parameter. The percentage parameter can exceed 100 percent for background tasks. This will allow you to run more than one background task per worker.

You can also have the standard onStartup and onShutdown abilities on background tasks just like you have on normal tasks.

You can determine if a background task is running by looking on the log_monitor. You will see a message when the task is loaded as well as each time it runs.

You will want to design you task so that it can run regularly and be able to be run often. The retryDelay parameter is only used when the background task fails so there is no natural delay between the background task ends and when the next execute of the background task. Therefore, you may want to add a delay of your own if you do not want it run back to back. Sometimes this can result in large CPU usage if there is no delay. On the flip side, if you design your task to run very long, you will experience problems stopping your fabric because your fabric waits for each task to complete before stopping all its services. It will eventually stop, but not until that task is done.

superman970 
Joined: 2008-03-13
Posts: 9
User is offline

Mark,

Thanks so much. That helps me better understand how I can use background tasks.

much appreciated!

superman