async def run_loop(context, creds_key="credentials"): """Split this out of the async_main while loop for easier testing. """ loop = asyncio.get_event_loop() await update_poll_task_urls( context, context.queue.pollTaskUrls, args=(context.config['provisioner_id'], context.config['worker_type']), ) for poll_url, delete_url in get_azure_urls(context): try: claim_task_defn = await find_task(context, poll_url, delete_url, retry_request) except ScriptWorkerException: await asyncio.sleep(context.config['poll_interval']) break if claim_task_defn: log.info("Going to run task!") context.claim_task = claim_task_defn loop.create_task(reclaim_task(context)) running_task = loop.create_task(run_task(context)) status = await running_task await upload_artifacts(context) await complete_task(context, running_task.result()) cleanup(context) await asyncio.sleep(1) return status else: await asyncio.sleep(context.config['poll_interval']) if arrow.utcnow().timestamp - context.credentials_timestamp > context.config['credential_update_interval']: credentials = read_worker_creds(key=creds_key) if credentials and credentials != context.credentials: context.credentials = credentials
async def run_loop(context, creds_key="credentials"): """Split this out of the async_main while loop for easier testing. """ loop = asyncio.get_event_loop() await update_poll_task_urls( context, context.queue.pollTaskUrls, args=(context.config['provisioner_id'], context.config['worker_type']), ) for poll_url, delete_url in get_azure_urls(context): try: claim_task_defn = await find_task(context, poll_url, delete_url, retry_request) except ScriptWorkerException: await asyncio.sleep(context.config['poll_interval']) break if claim_task_defn: log.info("Going to run task!") context.claim_task = claim_task_defn loop.create_task(reclaim_task(context)) running_task = loop.create_task(run_task(context)) status = await running_task await upload_artifacts(context) await complete_task(context, running_task.result()) cleanup(context) await asyncio.sleep(1) return status else: await asyncio.sleep(context.config['poll_interval']) if arrow.utcnow( ).timestamp - context.credentials_timestamp > context.config[ 'credential_update_interval']: credentials = read_worker_creds(key=creds_key) if credentials and credentials != context.credentials: context.credentials = credentials
def test_run_task(context, event_loop): status = event_loop.run_until_complete( task.run_task(context) ) log_file, error_file = log.get_log_filenames(context) assert read(log_file) in ("bar\nfoo\nexit code: 1\n", "foo\nbar\nexit code: 1\n") assert read(error_file) == "bar\n" assert status == 1
def test_max_timeout(context, event_loop): temp_dir = os.path.join(context.config['work_dir'], "timeout") context.config['task_script'] = ( sys.executable, TIMEOUT_SCRIPT, temp_dir ) context.config['task_max_timeout'] = 3 event_loop.run_until_complete(task.run_task(context)) try: event_loop.run_until_complete(asyncio.sleep(10)) # Let kill() calls run except RuntimeError: pass files = {} for path in glob.glob(os.path.join(temp_dir, '*')): files[path] = (time.ctime(os.path.getmtime(path)), os.stat(path).st_size) print("{} {}".format(path, files[path])) for path in glob.glob(os.path.join(temp_dir, '*')): print("Checking {}...".format(path)) assert files[path] == (time.ctime(os.path.getmtime(path)), os.stat(path).st_size) assert len(files.keys()) == 6