mirror of
https://github.com/cupcakearmy/mercatus.git
synced 2024-12-22 08:06:28 +00:00
fixed persistence in the job runner
This commit is contained in:
parent
e1c93963d9
commit
a3a0bef85b
@ -9,7 +9,7 @@ from telegram.ext.dispatcher import run_async
|
|||||||
from commands.config import Section
|
from commands.config import Section
|
||||||
from market import Market
|
from market import Market
|
||||||
from text import INTRO_TEXT
|
from text import INTRO_TEXT
|
||||||
from utils import persistence, updater, current_timestamp, delta_timestamp
|
from utils import persistence, updater, current_timestamp, delta_timestamp, update_updater_data
|
||||||
|
|
||||||
SENDING = False
|
SENDING = False
|
||||||
|
|
||||||
@ -41,38 +41,38 @@ def send_update_to_user(user: str, auto, codes=None):
|
|||||||
try:
|
try:
|
||||||
user_data = persistence.user_data[user]
|
user_data = persistence.user_data[user]
|
||||||
running = user_data.setdefault(Section.Running.value, False)
|
running = user_data.setdefault(Section.Running.value, False)
|
||||||
|
update_updater_data()
|
||||||
|
|
||||||
if Section.API_Key.value not in user_data:
|
if Section.API_Key.value not in user_data:
|
||||||
updater.bot.send_message(user, text='API Key not set ⛔️\nSet in /settings')
|
updater.bot.send_message(user, text='API Key not set ⛔️\nSet in /settings')
|
||||||
return
|
return
|
||||||
|
|
||||||
if running:
|
if running and not auto:
|
||||||
updater.bot.send_message(user, text='Already running 🏃')
|
updater.bot.send_message(user, text='Already running 🏃')
|
||||||
return
|
return
|
||||||
|
|
||||||
user_data[Section.Running.value] = True
|
user_data[Section.Running.value] = True
|
||||||
market = Market(user_data[Section.API_Key.value])
|
market = Market(user_data[Section.API_Key.value])
|
||||||
now = current_timestamp()
|
now = current_timestamp()
|
||||||
if auto:
|
|
||||||
updater.bot.send_message(user, text='Getting updates 🌎')
|
|
||||||
|
|
||||||
first = True
|
first = True
|
||||||
for code in codes if codes else user_data.get(Section.Watchlist.value, {}).keys():
|
for code in codes or user_data.get(Section.Watchlist.value, {}):
|
||||||
try:
|
try:
|
||||||
code_data = persistence.user_data[user][Section.Watchlist.value][code]
|
code_data = persistence.user_data[user][Section.Watchlist.value][code]['value']
|
||||||
code_data = code_data['value']
|
|
||||||
print(code, code_data)
|
|
||||||
last_run = code_data[Section.LastRun.value]
|
last_run = code_data[Section.LastRun.value]
|
||||||
frequency = parse(code_data[Section.Frequency.value])
|
frequency = parse(code_data[Section.Frequency.value])
|
||||||
interval = parse(code_data[Section.Interval.value])
|
interval = parse(code_data[Section.Interval.value])
|
||||||
print(code, last_run + frequency, now, last_run + frequency - now)
|
# print(code, last_run + frequency, now, last_run + frequency - now)
|
||||||
|
|
||||||
if auto and last_run + frequency > now:
|
if auto and last_run + frequency > now:
|
||||||
continue
|
continue
|
||||||
persistence.user_data[user][Section.Watchlist.value][code][Section.LastRun.value] = current_timestamp()
|
|
||||||
persistence.flush()
|
code_data[Section.LastRun.value] = current_timestamp()
|
||||||
|
update_updater_data()
|
||||||
|
|
||||||
if first:
|
if first:
|
||||||
|
if auto:
|
||||||
|
updater.bot.send_message(user, text='Getting updates 🌎')
|
||||||
first = False
|
first = False
|
||||||
else:
|
else:
|
||||||
# Wait to not overload the api
|
# Wait to not overload the api
|
||||||
@ -90,11 +90,12 @@ def send_update_to_user(user: str, auto, codes=None):
|
|||||||
print(f'❌ {user} - {e}')
|
print(f'❌ {user} - {e}')
|
||||||
updater.bot.send_message(user, text=f'There was an error ⚠️\n {e}')
|
updater.bot.send_message(user, text=f'There was an error ⚠️\n {e}')
|
||||||
|
|
||||||
if auto:
|
if not first and auto: # Has run at least once
|
||||||
updater.bot.send_message(user, text=f'Done ✅')
|
updater.bot.send_message(user, text=f'Done ✅')
|
||||||
finally:
|
finally:
|
||||||
if user_data:
|
if user_data:
|
||||||
user_data[Section.Running.value] = False
|
user_data[Section.Running.value] = False
|
||||||
|
update_updater_data()
|
||||||
|
|
||||||
|
|
||||||
def send_updates(context: CallbackContext):
|
def send_updates(context: CallbackContext):
|
||||||
|
@ -23,7 +23,7 @@ def main():
|
|||||||
dp.add_handler(watchlist_handler)
|
dp.add_handler(watchlist_handler)
|
||||||
|
|
||||||
# Cron jobs
|
# Cron jobs
|
||||||
jq.run_repeating(send_updates, interval=30, first=5)
|
jq.run_repeating(send_updates, interval=20, first=3)
|
||||||
|
|
||||||
# Start
|
# Start
|
||||||
print('Started 🚀')
|
print('Started 🚀')
|
||||||
|
@ -13,6 +13,12 @@ persistence = PicklePersistence(DB_FILE)
|
|||||||
updater: Updater = Updater(config['token'], use_context=True, persistence=persistence)
|
updater: Updater = Updater(config['token'], use_context=True, persistence=persistence)
|
||||||
|
|
||||||
|
|
||||||
|
def update_updater_data():
|
||||||
|
updater.dispatcher.user_data = persistence.user_data
|
||||||
|
updater.dispatcher.update_persistence()
|
||||||
|
persistence.flush()
|
||||||
|
|
||||||
|
|
||||||
def current_timestamp():
|
def current_timestamp():
|
||||||
return int(datetime.now().timestamp())
|
return int(datetime.now().timestamp())
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user