ahawker / crython

Lightweight task scheduler using cron expressions
Other
202 stars 22 forks source link

Reserved keywords are mapped to wrong expressions #394

Open bynr opened 4 years ago

bynr commented 4 years ago

Description of bug

Documentation in README mismatch for weekly cronjob expression.

# Fire once a week.

@crython.job(expr='0 0 0 0 *') def foo(): print "Back in line, maggot! - Kearny"

and

@weekly | Run once a week at midnight in the morning of Sunday | 0 0 0 0 0

Looking in details of the expression in the code for @weekly keyword, we can see the actual expression used: https://github.com/ahawker/crython/blob/12d9ca929a653ca5f67d1c857bcb2172600dc349/crython/expression.py#L34

As shown in below section, the expression is not correct.

According to the crython docstring, this is how an expression is constructed:

    Represents an entire cron expression.
    An expression consists of seven, space delimited fields that represent the following values::
        +------------- second (0 - 59)
        | +------------- minute (0 - 59)
        | | +------------- hour (0 - 23)
        | | | +------------- day (1 - 31)
        | | | | +------------- month (1 - 12)
        | | | | | +------------- weekday (0 - 6) (Sunday to Saturday; 7 is also Sunday)
        | | | | | | +------------- year (1970 - 2099)
        | | | | | | |
        | | | | | | |
        * * * * * * *

I believe all keywords using incorrectly the day_in_month part of expressions set to 0 should fail? I only tested it for the weekly keyword as shown below.

How to reproduce

Python 3.6.9 (default, Nov  7 2019, 10:44:02) 

In [1]: import pandas as pd
In [2]: import crython
In [3]: crython.__version__
Out[3]: '0.2.0'

In [5]: crython.expression.CronExpression.new('0 0 0 * * 0 *').matches(pd.Timestamp('2020-01-26 00:00:00'))
Out[5]: True

In [6]: crython.expression.CronExpression.new('0 0 0 0 * 0 *').matches(pd.Timestamp('2020-01-26 00:00:00'))
Out[6]: False

In [7]: crython.expression.CronExpression.new('@weekly').matches(pd.Timestamp('2020-01-26 00:00:00'))
Out[7]: False