Home > Net >  How to force job to run when another function is running at the same time?
How to force job to run when another function is running at the same time?

Time:01-18

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()

CodePudding user response:

Me personally I'd add an elif statement that checks the total amount of time running of that specific loop, if that value is greater than the amount you want it to run for have it run a break(end the loop)

elif value > value 
 break 

CodePudding user response:

If u need to force stop the current one running try setting it to break through an if statement located in the actual loop? Kinda like a failsafe, which you likely will need to add anyway eventually

  •  Tags:  
  • Related