~launchpad-pqm/launchpad/devel

7675.845.17 by Edwin Grubbs
Working as two separate cron scripts.
1
#!/usr/bin/python -S
2
#
3
# Copyright 2009, 2010 Canonical Ltd.  This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
5
6
"""Handle jobs for a specified job source class."""
7
8
__metaclass__ = type
9
7675.845.20 by Edwin Grubbs
Formatted imports and delinted.
10
import sys
11
7675.845.17 by Edwin Grubbs
Working as two separate cron scripts.
12
import _pythonpath
13
from twisted.python import log
14
15
from canonical.config import config
7675.845.20 by Edwin Grubbs
Formatted imports and delinted.
16
from lp.services.job import runner
7675.845.22 by Edwin Grubbs
Suggestions from reviewer.
17
from lp.services.job.runner import JobCronScript
7675.845.17 by Edwin Grubbs
Working as two separate cron scripts.
18
19
20
class ProcessJobSource(JobCronScript):
7675.845.23 by Edwin Grubbs
Addressed more review comments.
21
    """Run jobs for a specified job source class."""
22
    usage = (
23
        "Usage: %prog [options] JOB_SOURCE\n\n"
24
        "For more help, run:\n"
25
        "    cronscripts/process-job-source-groups.py --help")
7675.845.17 by Edwin Grubbs
Working as two separate cron scripts.
26
7675.845.26 by Edwin Grubbs
Simplified cron script.
27
    def __init__(self):
28
        super(ProcessJobSource, self).__init__()
7675.845.17 by Edwin Grubbs
Working as two separate cron scripts.
29
        # The fromlist argument is necessary so that __import__()
30
        # returns the bottom submodule instead of the top one.
7675.845.27 by Edwin Grubbs
Merged in db-devel.
31
        module = __import__(self.config_section.module,
32
                            fromlist=[self.job_source_name])
33
        self.source_interface = getattr(module, self.job_source_name)
34
35
    @property
36
    def config_name(self):
37
        return self.job_source_name
38
39
    @property
7675.845.28 by Edwin Grubbs
Fixed tests after re-organizing command-line overrides of LaunchpadScript attributes.
40
    def name(self):
7675.845.27 by Edwin Grubbs
Merged in db-devel.
41
        return 'process-job-source-%s' % self.job_source_name
42
43
    @property
7675.845.28 by Edwin Grubbs
Fixed tests after re-organizing command-line overrides of LaunchpadScript attributes.
44
    def runner_class(self):
7675.845.27 by Edwin Grubbs
Merged in db-devel.
45
        runner_class_name = getattr(
46
            self.config_section, 'runner_class', 'JobRunner')
7675.845.22 by Edwin Grubbs
Suggestions from reviewer.
47
        # Override attributes that are normally set in __init__().
7675.845.28 by Edwin Grubbs
Fixed tests after re-organizing command-line overrides of LaunchpadScript attributes.
48
        return getattr(runner, runner_class_name)
7675.845.17 by Edwin Grubbs
Working as two separate cron scripts.
49
13701.1.6 by Aaron Bentley
Special-case process-job-source.
50
    def add_my_options(self):
51
        self.add_log_twisted_option()
52
7675.845.27 by Edwin Grubbs
Merged in db-devel.
53
    def handle_options(self):
54
        if len(self.args) != 1:
55
            self.parser.print_help()
56
            sys.exit(1)
57
        self.job_source_name = self.args[0]
7675.845.28 by Edwin Grubbs
Fixed tests after re-organizing command-line overrides of LaunchpadScript attributes.
58
        super(ProcessJobSource, self).handle_options()
7675.845.27 by Edwin Grubbs
Merged in db-devel.
59
7675.845.17 by Edwin Grubbs
Working as two separate cron scripts.
60
    def main(self):
61
        if self.options.verbose:
62
            log.startLogging(sys.stdout)
63
        super(ProcessJobSource, self).main()
64
65
66
if __name__ == '__main__':
7675.845.22 by Edwin Grubbs
Suggestions from reviewer.
67
    script = ProcessJobSource()
7675.845.17 by Edwin Grubbs
Working as two separate cron scripts.
68
    script.lock_and_run()