Skip to content

Instantly share code, notes, and snippets.

@etienne-napoleone
Created September 27, 2019 05:03
Show Gist options
  • Select an option

  • Save etienne-napoleone/fdcee13eee38cc2e65fabeb6e3f47754 to your computer and use it in GitHub Desktop.

Select an option

Save etienne-napoleone/fdcee13eee38cc2e65fabeb6e3f47754 to your computer and use it in GitHub Desktop.
from datetime import datetime, timedelta
from huey import RedisHuey
from huey import crontab
from huey.exceptions import HueyException
from huey.constants import WORKER_THREAD
import colorlog
from oracle.config import Config
colorlog.getLogger().setLevel("ERROR")
INTERVAL = 5
only_once = True
config = Config()
huey = RedisHuey(
"test_sub_min1",
host=config.values["redis"]["host"],
port=config.values["redis"]["port"],
password=config.values["redis"]["password"],
)
print(
"Connected to redis on {}:{}.".format(
config.values["redis"]["host"], config.values["redis"]["port"]
)
)
# OPT #1
###################################
# @huey.periodic_task(crontab(minute="*"))
# def scheduler():
# now = datetime.now()
# print(f"Sheduler start: {now}")
# for x in range(0, 60, INTERVAL):
# # print(f"Sheduling for {start_time} (in {delay}s)")
# say.schedule(delay=x)
# @huey.task()
# def say():
# # print(f"say: {datetime.now()}")
# try:
# hello().get(blocking=True, timeout=INTERVAL - 1)
# except HueyException:
# print("expired.")
# @huey.task()
# def hello():
# import time
# print(f"hello: {datetime.now().second}. Sleeping 10")
# time.sleep(10)
# return True
# OPT #2
###################################
@huey.periodic_task(lambda x: only_once)
def scheduler():
import time
global only_once
only_once = False
while True:
try:
hello().get(timeout=INTERVAL)
time.sleep(INTERVAL)
except HueyException:
print("timeout!")
@huey.task()
def hello():
from time import sleep
print(f"hello: {datetime.now().second}, sleeping 10")
sleep(10)
return True
###################################
workers_count = config.values["general"]["workers"]
consumer = huey.create_consumer(
workers=workers_count,
worker_type=WORKER_THREAD,
initial_delay=0.1,
backoff=1.15,
max_delay=10.0,
check_worker_health=True,
health_check_interval=10,
scheduler_interval=1,
periodic=True,
flush_locks=False,
)
consumer.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment