~yerinalexey/pcrond

7f411c4626caa5060b6cc27ac57245ed4527560b — Luca Vercelli 2 years ago 9a3aaf8
support for 15w
2 files changed, 29 insertions(+), 3 deletions(-)

M pcrond/job.py
M test_scheduler.py
M pcrond/job.py => pcrond/job.py +25 -1
@@ 228,6 228,29 @@ class Job(object):
        """ true if given date is in the last week of the month """
        return now.day > self.get_last_dom(now) - 7

    def _check_w(self, now):
        """ used for checking 15w """
        w = now.weekday()
        if w >= 5:
            return False
        d = now.day
        if d in self.allowed_wdom:
            return True
        if w == 0:
            if (d - 1) in self.allowed_wdom:
                return True
            # 1w matches monday, 3rd
            if d == 3 and 1 in self.allowed_wdom:
                return True
        elif w == 4:
            if (d + 1) in self.allowed_wdom:
                return True
            # 31w matches friday, 29th
            l = self.get_last_dom(now)
            if d == l - 2 and l in self.allowed_wdom:
                return True
        return False

    def should_run(self):
        """
        :return: ``True`` if the job should be run now.


@@ 254,7 277,8 @@ class Job(object):
                         and self.is_last_wom(now)))
                and (self.allowed_every_dom
                     or now.day in self.allowed_dom
                     or (self.allowed_last_dom and now.day == self.get_last_dom(now)))
                     or (self.allowed_last_dom and now.day == self.get_last_dom(now))
                     or (self.allowed_wdom and self._check_w(now)))
                )

    def run(self):

M test_scheduler.py => test_scheduler.py +4 -2
@@ 107,16 107,18 @@ class SchedulerTests(unittest.TestCase):
        assert job.allowed_wdom == set([15])
        assert job._should_run_at(d(2019, 3, 15))       # was fri
        assert not job._should_run_at(d(2018, 12, 15))  # was sat
        assert job._should_run_at(d(2019, 3, 14))       # was fri
        assert job._should_run_at(d(2018, 12, 14))      # was fri   *
        assert not job._should_run_at(d(2018, 7, 15))   # was sun
        assert job._should_run_at(d(2018, 7, 16))       # was mon
        ###
        job = Job("* * 1w * *")
        assert not job.allowed_every_dom
        assert not job.allowed_last_dom
        assert job.allowed_dom == set()
        assert job.allowed_wdom == set([1])
        assert job._should_run_at(d(2019, 3, 1))        # was fri
        assert not job._should_run_at(d(2019, 6, 3))    # was mon
        assert not job._should_run_at(d(2019, 6, 1))    # was sat
        assert job._should_run_at(d(2019, 6, 3))        # was mon

    def test_job_constructor_L_dow(self):
        job = Job("* * * * 5l")      # 5=friday, l=only the last one of the month