rq / rq-scheduler

A lightweight library that adds job scheduling capabilities to RQ (Redis Queue)
MIT License
1.45k stars 229 forks source link

How does "use_local_timezone" work? Seems that it's not using the local timezone #247

Closed BartlomiejSkwira closed 3 years ago

BartlomiejSkwira commented 4 years ago

How should use_local_timezone work in cron method?

scheduler.cron(
    cron_string,                # A cron string (e.g. "0 0 * * 0")
    func=func,                  # Function to be queued
    args=[arg1, arg2],          # Arguments passed into function when executed
    kwargs={'foo': 'bar'},      # Keyword arguments passed into function when executed
    repeat=10,                  # Repeat this number of times (None means repeat forever)
    queue_name=queue_name,      # In which queue the job should be put in
    meta={'foo': 'bar'},        # Arbitrary pickleable data on the job itself
    use_local_timezone=False    # Interpret hours in the local timezone
)

From what I understand the docs it should use the server timezone (TZ env variable) to interpret the scheduled task. But when I look into the code it seems that it is using UTC even if the setting is set to True. Relevant code: https://github.com/rq/rq-scheduler/blob/c79288c1046e5e4e79a46cba2bb6dc035250ff84/rq_scheduler/utils.py#L26

I read the above code as: Use get_utc_timezone if use_local_timezone else use datetime.utcnow() <- look like UTC in both situations.

Am I correct?

My case: I have a task that I want to fire daily at midnight (00:00) California time. Unfortunatelly California has 2 timezones that change (PDT and PST). My workaround was to schedule the task at 8am UTC time which will be 01:00am PDT and 00:00 PST. But maybe there is a better way to solve it with rq-scheduler

BartlomiejSkwira commented 4 years ago

@Eugeny maybe you could help out here?

BartlomiejSkwira commented 4 years ago

The get_utc_timezone() func also seems to be fetching just the UTC timezone. https://github.com/rq/rq-scheduler/blob/061488e79b82af215f7f98fa1554a0aa84dfd62f/rq_scheduler/utils.py#L71-L74

Eugeny commented 4 years ago

Holy crap 😨 - my bad! I'm passing a TZ-aware date to croniter when local time is enabled, so get_next_scheduled_time() will either return TZ-aware date or "UTC-assumed" objects, but the TZ info is simply ignored by to_unix afterwards

Fixed in the PR: https://github.com/rq/rq-scheduler/pull/248

waldner commented 3 years ago

Will this make it to a release in a reasonably near future?

waldner commented 3 years ago

I'm not sure I understand how this is supposed to work. With the (supposedly fixed) rel 0.11.0, I have the following cron line:

"0 5 * * *"

with use_local_timezone=True. I'm in CEST timezone (currently UTC+2), and I see the job is running every day at 7 am my time. Shouldn't it be running at 5 am my time instead?