schedule icon indicating copy to clipboard operation
schedule copied to clipboard

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

Open ramstein74 opened this issue 7 years ago • 5 comments

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

ramstein74 avatar Oct 17 '17 09:10 ramstein74

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.

grandimam avatar Oct 27 '17 05:10 grandimam

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"

nzjrs avatar Nov 07 '17 10:11 nzjrs

@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)

jchacks avatar Dec 20 '17 15:12 jchacks

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

marcelo-dalmeida avatar Aug 13 '19 20:08 marcelo-dalmeida

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

Timopheym avatar Feb 21 '22 10:02 Timopheym