apscheduler icon indicating copy to clipboard operation
apscheduler copied to clipboard

Export crontab line from CronTrigger

Open fcrozetta opened this issue 1 year ago • 7 comments

Things to check first

  • [X] I have searched the existing issues and didn't find my feature already requested there

Feature description

I've been looking for a way to export crontab line from the trigger and it seems this feature does not exist. In the CronTrigger class, there is a from_crontab method that receives a crontab string. It would be good to have a to_crontab() method that would return a crontab line.

Here are some examples in how I would assume this would work:


trigger = CronTrigger.from_crontab("20 04 25 12 *")
trigger.to_crontab() // returns: "20 04 25 12 *"

In case this feature gets approve, I would like to contribute to the project and work on it. Thanks :)

Use case

Example of usage


trigger = CronTrigger.from_crontab("20 04 25 12 *")
trigger.to_crontab() // returns: "20 04 25 12 *"

fcrozetta avatar Jul 26 '24 12:07 fcrozetta

And what will it do with the year and second fields?

agronholm avatar Jul 27 '24 16:07 agronholm

My original idea was to have it crontab compatible, but I can add an optional parameter strict (name to be defined) that would return a line that is unix compatible, or return all the fields.

I did not saw any standard that APScheduler uses in reading the year and secondfields in the from_crontab() method, but if there is one then the new method should follow the same rules IMHO

fcrozetta avatar Jul 27 '24 21:07 fcrozetta

The point I was trying to make is that since APScheduler's cron trigger has more options than the original cron spec, the reverse conversion doesn't work, so I'm not going to provide such a method.

I took a look at the CronTrigger code and noticed that it's not really possible to implement such a thing using its public interface, so I'm going to refactor it a bit to make it possible. After that, you could fairly easily implement this feature yourself if you're only using crontab-compatible triggers in your own app.

agronholm avatar Jul 28 '24 23:07 agronholm

I was poking around and ended up with a small piece of code that could retrieve all the information into a dictionary:

trigger = CronTrigger.from_crontab("20 04 25 12 *")
cron_dict = { k:str(v) for (k,v) in zip(trigger.FIELD_NAMES, trigger.fields)}

It is not the most convenient, or straightforward way of getting the information, but it seems to work as expected. Are you planning to change the code to have something like this? If I could export as a dict, it could be possible to format the string as well

fcrozetta avatar Jul 30 '24 10:07 fcrozetta

Did you mean _fields and not fields? Because I can't find a fields attribute in CronTrigger. Other than that, you could instead do:

trigger = CronTrigger.from_crontab("20 04 25 12 *")
cron_dict = {f.name:  str(f) in trigger._fields}

agronholm avatar Jul 31 '24 08:07 agronholm

yes, _fields indeed. mistype on my side I believe the f variable in your snippet should be the trigger, right? I didn't knew I could use .name to retrieve the field name! Thanks for pointing it out for me.

fcrozetta avatar Jul 31 '24 17:07 fcrozetta

If the feature to export the crontab line won't be implemented, should I close this issue already, or is this going to be related to any other changes you will do in the code?

fcrozetta avatar Jul 31 '24 17:07 fcrozetta