Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pass .every(10).minutes as a string to schedule #169

Open
ramstein74 opened this issue Oct 17, 2017 · 5 comments
Open

How to pass .every(10).minutes as a string to schedule #169

ramstein74 opened this issue Oct 17, 2017 · 5 comments

Comments

@ramstein74
Copy link

Hello Dan
i´m using the schedule.
However i would like to know how to pass
".every(10).minutes"
or
".every().day.at('10:30')"
etc
as a string to schedule
because i have a loop over a dict where i have around 15 tasks and each has its field with the "every().." defined.

Regards
António

@grandimam
Copy link

If I am able to understand the problem correctly, this doesn't look like a schedule package problem per-se. However, this are the following ways to solve it:

  1. Explicitly add all the tasks with their following scheduled time -

Code sample:
schedule.every(10).minutes.do(self.task_one)
schedule.every(15).minutes.do(self.task_two)
schedule.every(20).minutes.do(self.task_three)
Because, internally all the tasks are added in an array. And sequentially picked up for execution.

  1. Implicitly control the schedule time flow within a function -

Code sample:
schedule.every(1).minute.do(self.run_all_tasks)

I could provide you a better solution if I you could provide me some code snippets.

@nzjrs
Copy link

nzjrs commented Nov 7, 2017

I'd like something similar - the user to be able to express a scheduled task as a human readable string "every day at 10:30"

@jchacks
Copy link

jchacks commented Dec 20, 2017

@nzjrs I wanted a config file and so wrote something that can parse JSON.

"frequency": {
  "every": "day",
  "at": "10:30"
},

or

"frequency": {
  "every": [ 10, "seconds"]
},

and then when scheduling

def from_json(job_dict):
        frequency = job_dict.get('frequency')
        if frequency is None:
            raise MalformedJobException(job_dict['name'], "Frequency is missing from job definition!")

        if type(frequency['every']) is list:
            job = scheduler.every(frequency['every'][0])
            job = getattr(job, frequency['every'][1])
        else:
            job = scheduler.every()
            job = getattr(job, frequency['every'])

        if frequency.get('at', None):
            job = job.at(frequency.get('at'))

where scheduler is your instance of the Scheduler class or the default scheduler.
To actually schedule the job you need to call do on the job.
E.g. job.do(my_method, arguments)

@marcelo-dalmeida
Copy link

I was looking for some native support and then I found this issue.

I needed more of a line in the properties file than a json per se, so I made this based on @jchacks 's work:

SCHEDULE = "every.tuesday.at_17:33"
import schedule

def parse_from_configuration(schedule_strategy):

    actual_schedule = schedule
    schedule_strategy_list = schedule_strategy.split('.')

    for stategy_element in schedule_strategy_list:

        elements = stategy_element.split('_')

        attribute = getattr(actual_schedule, elements[0])
        if callable(attribute):

            method = attribute

            if len(elements) == 2:
                actual_schedule = method(elements[1])
            else:
                actual_schedule = method()
        else:
            actual_schedule = attribute

    return actual_schedule

It would be a good addition to have native support for this, as we need to change schedule settings in production sometimes

@Timopheym
Copy link

This is really neat feature, what does core team thinks about it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants