Issue
I have a while True
statement that is running the same functions over and over, and also two more functions that is scheduled to run every 5 minutes.
But when the job is triggered to run, but also another function from the loop is already running the job is missed, and I get the following message:
Run time of job "SendHeroesToWork (trigger: interval[0:03:00], next run at: 2022-01-14 15:33:27 -03)" was missed by 0:00:03.169182
Is there anywat to force the job scheduled to run by anyaway? Or even stop the current function and start the schedule when hit the proper time.
My script from main.py
:
import asyncio
import tzlocal
from bot import ConnectWallet, LoginMetamask, TreasureHuntGame, NewMap, SkipErrorOnGame, RefreshHeroesPositions, SendHeroesToWork
from controllers import countdownTimer, setup_logger, initializePyAutoGUI, ReadConfigs, DeleteLogFiles
from apscheduler.schedulers.asyncio import AsyncIOScheduler
try:
streamConfig = ReadConfigs()
refresh_heroes_time = streamConfig['heroes_options']['refresh_heroes_time']
refresh_heroes_only = streamConfig['heroes_options']['refresh_heroes_only']
work_heroes_time = streamConfig['heroes_options']['work_heroes_time']
except FileNotFoundError:
print('Error: config.yaml file not found, make sure config.yaml are placed in the folder..')
exit()
async def main():
# Init message
print('\nPress Ctrl-C to quit at anytime!\n' )
# Initialize pyautogui library
await asyncio.create_task(initializePyAutoGUI())
# Countdown timer before start the bot
await asyncio.create_task(countdownTimer())
# Delete old log files
await asyncio.create_task(DeleteLogFiles())
# Create a scheduler for certain functions
scheduler = AsyncIOScheduler(timezone=str(tzlocal.get_localzone()))
if (refresh_heroes_time*60) > 9:
scheduler.add_job(RefreshHeroesPositions, 'interval', seconds=(refresh_heroes_time*60)+120, id='1')
if (work_heroes_time*60) > 0:
scheduler.add_job(SendHeroesToWork, 'interval', seconds=(work_heroes_time*60), id='2')
if len(scheduler.get_jobs()) > 0:
scheduler.start()
elif refresh_heroes_only != True:
while True:
# Steps of this bot:
# - Connect Wallet on BomberCypto game
await asyncio.create_task(ConnectWallet())
# - Login Metamask
await asyncio.create_task(LoginMetamask())
# - Treasure Hunt game mode
await asyncio.create_task(TreasureHuntGame())
# - New map feature
await asyncio.create_task(NewMap())
# - Check for errors on game
await asyncio.create_task(SkipErrorOnGame())
if __name__ == "__main__":
try:
loop = asyncio.get_event_loop()
loop.create_task(main())
loop.run_forever()
except Exception as e:
#logger = setup_logger()
#logger.error("Exception: " + str(e))
print("Exception: " + str(e))
exit()
Solution
Actually it was a really simple solution that I didn't notice a paremeter from apscheduler
which is explained in the documentation as:
misfire_grace_time (int) – the time (in seconds) how much this job’s execution is allowed to be late (None means “allow the job to run no matter how late it is”)
So I added this parameter to my scheduler as below:
scheduler.add_job(send_heroes_to_work, 'interval', seconds=(work_heroes_time*60), id='2', name='send_heroes_to_work', misfire_grace_time=300)
Answered By - Guilherme Matheus
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.