summaryrefslogtreecommitdiff
path: root/desktop-cronish
blob: 738d29cb446f2e79a46eb74bd914c11e75e3a6f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/python
# Copyright 2013  Lars Wirzenius
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import cliapp
import time
import sys
import yaml


__version__ = '0.0'


class DesktopCronish(cliapp.Application):

    def add_settings(self):
        self.settings.integer(
            ['max-jobs'],
            'run at least N jobs, then quit (use 0 for infinite)',
            metavar='N',
            default=0)
    
    def process_args(self, args):
        self.jobs = {}
        self.previously = {}
        self.process_inputs(args)
        self.execute_jobs()
    
    def process_input(self, filename):
        with self.open_input(filename, 'r') as f:
            job_dict = yaml.safe_load(f)
        self.jobs.update(job_dict)

    def execute_jobs(self):
        n = 0
        max_jobs = self.settings['max-jobs']
        while max_jobs == 0 or n < max_jobs:
            job_name, when = self.choose_job()
            self.wait_until(when)
            self.execute_job(job_name)
            n += 1
    
    def choose_job(self):
        next_job_name = None
        next_when = 0
        for job_name, job in self.jobs.items():
            job_when = self.previously.get(job_name, 0) + job['interval']
            if next_job_name is None or job_when <= next_when:
                next_job_name = job_name
                next_when = job_when
        return next_job_name, next_when
    
    def wait_until(self, when):
        while self.now() < when:
            time.sleep(when - self.now())
    
    def execute_job(self, job_name):
        job = self.jobs[job_name]
        argv = ['sh', '-c', job['command']]
        if 'timeout' in job:
            argv = ['timeout', str(job['timeout'])] + argv
        exit, out, err = cliapp.runcmd_unchecked(argv)
        self.output.write(out)
        if exit != 0:
            sys.stderr.write(
                'ERROR: Job %s: Command failed: %s\n' %
                    (job_name, ' '.join(argv)))
            sys.stderr.write(err)
        self.previously[job_name] = self.now()

    def now(self):
        return time.time()

DesktopCronish(version=__version__).run()