source: OpenWorkouts-current/ow/tasks/manager.py @ b5d87e0

currentfeature/docs
Last change on this file since b5d87e0 was b5d87e0, checked in by Borja Lopez <borja@…>, 5 years ago

(#29) Added a tasks module, implemented a task to run the mail queue processor,
sending any automatically generated emails in the queue.

Tasks can be run manually, with the virtual env active:

python ow/tasks/run.py development.ini TASK_NAME

(where TASK_NAME is the name of a task, like send_emails)

Running the run.py script without parameters gives an usage-like message
with instructions on how to properly call the script.

  • Property mode set to 100644
File size: 1.6 KB
Line 
1from pyramid.paster import bootstrap
2from pyramid.paster import setup_logging
3
4
5class TasksManager(object):
6
7    def __init__(self):
8        """
9        managers is a dict containing all the available tasks. The keys are
10        the name used to reference this tasks (the action parameter accepted
11        on runtime) while the values are the functions/methods responsible to
12        run the task.
13
14        Those functions/methods have to expect one parameter (the environment
15        we get from bootstraping pyramid, see the run() method to learn more)
16        """
17        self.managers = {}
18
19    def usage(self, script_name):
20        """
21        Prints command line usage.
22
23        The description of the different tasks is gathered from the docstring
24        of the methods that will be called to perform the task.
25        """
26        docs = [t + ': ' + self.managers[t].__doc__ for t in self.managers]
27        msg = """
28        Usage: %s /path/to/settings.ini [%s]
29
30          settings.ini has to be a PasteDeploy .ini file representing your
31          application configuration.
32
33          %s
34
35        """ % (script_name, '|'.join(self.managers), '\n\t  '.join(docs))
36        print(msg)
37
38    def add_task(self, name, method):
39        self.managers[name] = method
40
41    def remove_task(self, name):
42        if name in self.managers:
43            del self.managers[name]
44
45    def run(self, script_name, ini_file, action):
46        env = bootstrap(ini_file)
47        setup_logging(ini_file)
48        if action not in self.managers.keys():
49            self.usage(script_name)
50        else:
51            self.managers[action](env)
52        env['closer']()
Note: See TracBrowser for help on using the repository browser.