def test_validate_expiration3(worker): """ Test validating the expiration of a Job returns False if the job has expiration and has expired. It also tests that the job is marked as expired with the proper message as error. """ app = worker.app.app with app.app_context(): task, job, execution = JobExecutionFixture.new_defaults() unow = unix_now() exp = unow - 10 job.metadata["expiration"] = exp job.save() result = job_mod.validate_expiration(job, execution, app.logger) expect(result).to_be_false() expect(execution.status).to_equal(JobExecution.Status.expired) expiration_utc = from_unix(job.metadata["expiration"]) error = ( f"Job was supposed to be done before {expiration_utc.isoformat()}, " f"but was started at {from_unix(unow).isoformat()}." ) expect(execution.error).to_equal(error) expect(execution.finished_at).not_to_be_null()
def validate_expiration(job, ex, logger): now = datetime.utcnow() unixtime = to_unix(now) if ( job.metadata.get("expiration") is not None and job.metadata["expiration"] < unixtime ): expiration_utc = from_unix(job.metadata["expiration"]) ex.status = JobExecution.Status.expired ex.error = ( f"Job was supposed to be done before {expiration_utc.isoformat()}, " f"but was started at {from_unix(unixtime).isoformat()}." ) ex.finished_at = now job.save() logger.info( "Job execution canceled due to being expired.", job_expiration=job.metadata["expiration"], current_ts=unixtime, ) return False return True
def test_job_details1(client): """Tests get job details returns proper details and last 20 execs by last updated at.""" task, job, execution = JobExecutionFixture.new_defaults() now = unix_now() for i in range(30): ex = job.create_execution("ubuntu", "ls") ex.created_at = from_unix(now + (i * 10)) ex.save() resp1 = client.get(f"/tasks/{task.task_id}/jobs/{job.job_id}/") expect(resp1.status_code).to_equal(200) obj = loads(resp1.data) expect(obj["job"]["executions"]).to_length(20) executions = obj["job"]["executions"] for i in range(20): ex = executions[i] dt = from_unix(now + (29 - i) * 10) expect(ex["createdAt"]).to_equal(dt.isoformat())
def status(): executor = current_app.executor version = pkg_resources.get_distribution("fastlane").version metadata = {"hosts": [], "containers": {"running": []}} containers = executor.get_running_containers() for host, port, container_id in containers["running"]: metadata["containers"]["running"].append({ "host": host, "port": port, "id": container_id }) metadata[ "hosts"] = [] + containers["available"] + containers["unavailable"] metadata["queues"] = {} for queue_name in [ QueueNames.Job, QueueNames.Monitor, QueueNames.Webhook, QueueNames.Notify, ]: queue = getattr(current_app, f"{queue_name}_queue") jobs_queue_size = current_app.redis.llen(queue.queue_name) metadata["queues"][queue_name] = {"length": jobs_queue_size} next_scheduled = current_app.redis.zrange(Queue.SCHEDULED_QUEUE_NAME, 0, 0, withscores=True) if not next_scheduled: next_timestamp = None next_human = None else: next_timestamp = next_scheduled[0][1] next_human = from_unix(next_timestamp).isoformat() metadata["queues"]["scheduled"] = { "length": current_app.redis.zcard(Queue.SCHEDULED_QUEUE_NAME), "nextTimeStamp": next_timestamp, "nextHumanReadableDate": next_human, } metadata["tasks"] = {"count": Task.objects.count()} metadata["jobs"] = {"count": Job.objects.count()} metadata["jobs"]["scheduled"] = [] scheduled_jobs = Job.objects(scheduled=True).all() metadata["fastlane"] = { "version": version, "executor": current_app.config["EXECUTOR"], } for job in scheduled_jobs: j = job.to_dict(include_executions=False, blacklist_fn=current_app.blacklist_words_fn) itr = croniter.croniter(job.metadata["cron"], datetime.utcnow()) j["nextScheduledAt"] = itr.get_next(datetime).isoformat() task_id = job.task.task_id job_url = url_for("task.get_job", task_id=task_id, job_id=str(job.job_id), _external=True) j["url"] = job_url stop_job_url = url_for("task.stop_job", task_id=task_id, job_id=str(job.job_id), _external=True) j["stopUrl"] = stop_job_url task_url = url_for("task.get_task", task_id=task_id, _external=True) del j["taskId"] j["task"] = {"id": task_id, "url": task_url} metadata["jobs"]["scheduled"].append(j) return jsonify(metadata), 200