~yerinalexey/pcrond

19d99b9b1266fb3331344f5dc9abad355d1b6fb1 — Luca Vercelli 2 years ago fa4eb09
bugfix and test
3 files changed, 12 insertions(+), 9 deletions(-)

M pcrond/sched.py
M test_scheduler.py
M tests/crontab.txt
M pcrond/sched.py => pcrond/sched.py +9 -8
@@ 100,32 100,33 @@ class Scheduler(object):
        """
        pieces = crontab_line.split()

        # is pattern using aliases?
        if pieces[0] in ALIASES.keys():
            try:
                # pattern using alias
                job = self.cron(pieces[0:1], job_func_func(pieces[1:]))
                # CASE 1 - pattern using alias
                job = self.cron(pieces[0], job_func_func(pieces[1:]))
                return job
            except ValueError:
                # shouldn't happen
                print("Error at line %d, cannot parse pattern" % rownum)
                logger.error("Error at line %d, cannot parse pattern, the line will be ignored" % rownum)
                logger.exception('Caused by:')
                return None
        if len(pieces) < 6:
            print("Error at line %d, expected at least 6 tokens" % rownum)
            logger.error("Error at line %d, expected at least 6 tokens" % rownum)
            return None
        if len(pieces) >= 7:
            try:
                # pattern including year
                # CASE 2 - pattern including year
                job = self.cron(" ".join(pieces[0:6]), job_func_func(pieces[6:]))
                return job
            except ValueError:
                pass
        try:
            # pattern not including  year
            # CASE 3 - pattern not including  year
            job = self.cron(" ".join(pieces[0:5]), job_func_func(pieces[5:]))
            return job
        except ValueError:
            print("Error at line %d, cannot parse pattern" % rownum)
            logger.error("Error at line %d, cannot parse pattern, the line will be ignored" % rownum)
            logger.exception('Caused by:')
            return None

    def _split_input_line(self, s):

M test_scheduler.py => test_scheduler.py +1 -1
@@ 220,7 220,7 @@ class SchedulerTests(unittest.TestCase):
        """ load test crontab file """
        import os
        scheduler.load_crontab_file(os.path.join("tests", "crontab.txt"))
        assert len(scheduler.jobs) == 3
        assert len(scheduler.jobs) == 4


if __name__ == '__main__':

M tests/crontab.txt => tests/crontab.txt +2 -0
@@ 1,3 1,5 @@
# crontab entries used during tests
30 4 * * mon echo goofy
30 5-6 * * mon echo donald duck
30 * */3 * mon 2020 echo mickey mouse
@daily echo it's midnight