[EMAIL PROTECTED] wrote:
>         I am new user of turbogears. I am facing problem of
> "add_weekday_task scheduler".
>         Can someone give me example. How will i use this funciton.

Hi Sanjeet,

what I usually do is, I create a "task.py" file in my project's package,
where I put all the functions that should be run periodically or on
application startup. I also add a function called "add_tasks" to it,
that is run at application start-up and adds all functions to the
scheduler. If you want, you can also add another function, called e.g.
"run_startup_tasks" that runs all tasks that should be run, when your
app starts.

HTH, Chris

-----> task.py <-----
import logging
from datetime import datetime
from turbogears import config, scheduler

log = logging.getLogger("myapp.scheduler")

# global module variable to hold all scheduled tasks
tasks = dict()

def do_something():
    """Example task - logs the date and time when it is run."""
    log.info("Runnning 'do_something' at %s" % datetime.now())

def add_tasks()
    """Schedule all task - called on application startup."""

    # global is not strictly necessary, since 'tasks' is mutable
    # we add it here, so it does not get overwritten accidentally
    global tasks

    # Get days of the week where this task should run
    # from config file. Default: Mon-Sun
    weekdays = config.get('myapp.tasks.do_something.weekdays',
        range(1,8))

    # Get time of the day where this task should run
    # from config file. Default: 2:00 p.m.
    timeonday = config.get('myapp.tasks.do_something.timeonday',
        (14,00))

    # Now schedule the task
    task = scheduler.add_weekday_task(taskname='do_something',
      action=do_something, weekdays=weekdays, timeonday=timeonday)
    # and store it in global module dict 'tasks' under its name
    tasks[task.name] = task
    log.debug("Interval task '%s added." % task.name)

    # Repeat the above steps for every function you want to run
    # periodically.

def run_startup_tasks():
    """Runs all task that should ru at application startup."""

    log.debug("Running 'do_something' start-up task.")
    do_something()
    # Add more function calls here...

----> end task.py <-----

----> controllers.py <-----
[...]
from turbogears import startup
from myapp.tasks import add_tasks, run_startup_tasks

startup.call_on_startup.append(add_tasks)
startup.call_on_startup.append(run_startup_tasks)
[...]
----> end controllers.py <-----

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to