简体   繁体   中英

Challenges getting simple Huey example started with Crontab

I'm attempting to implement the suggested organization of code from the Huey docs into an existing app, and also following the simple example . The goal is to build a crontab that will run the task every day at 3:00 am.

I powered up two terminal tabs, the first one with the consumer running the script from the example:

PYTHONPATH=.:$PYTHONPATH
export WORKER_CLASS=${1:-thread}
huey_consumer.py main.huey --workers=4 -k $WORKER_CLASS -C -S

Then, in the other tab, I run the main.py script:

python main.py

config.py

from huey import SqliteHuey

huey = SqliteHuey(filename='/tmp/huey.db')

tasks.py

from config import huey

# Note that this time is 1 minute before whenever I'm debugging.
# I'm using the 3 am example as what we're aiming for in our final product.
@huey.periodic_task(crontab(minute='0', hour='3'))
def run_this_function():
    system = New_Class() # Class instantiation since there's a bunch that happens when a new class is set up 
    system.run_method # Runs a bunch of methods from the class in one location

main.py

from config import huey
from tasks import run_this_function

def main:
    run_this_function()

if __name__ == "__main__":
    main()

The task runs immediately, and as I'm brand new to Huey, not sure what I might be missing to make it work on a schedule. I've tried so many crontab combinations, not sure if the challenge is there, or in how I call the run_this_function in the main method. Any help is appreciated!

So first of all, you don't actually want to call run_this_function() yourself, so there's no need to run your main.py script in another tab. You only need to have the huey consumer running as you want it to be responsible for executing the task at requested times.

Your consumer needs to be able to discover the task, which you can do by importing it to that main file as you're doing and then starting the huey instance through that file (which you are also doing). A simple test could be putting a print statement in the same file as where you're defining the periodic task. Once you run the consumer, you should see your print statement. If not, your task won't be picked up by the consumer either.

Secondly, I would suggest not using the crontab function. I have not been able to get it working in the past and instead you're better of writing your own function to configure the 3 am. Huey calls the supplied function regularly with the current timestamp. So as a reusable example you could do something like:

def run_at_my_preferred_hour(hour_to_run):
    last_day_it_ran_for = None

    def check_if_task_needs_to_run_for_this_time(current_timestamp):
        if current_timestamp.hour == hour_to_run and current_timestamp.day != last_day_it_ran_for:
            # Save that you already ran the task for this day
            last_day_it_ran_for = current_timestamp.day
            return True
        return False

    return check_if_task_needs_to_run_for_this_time

    
@huey.periodic_task(run_at_my_preferred_hour(3))
def run_this_function():
   ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM