Only save the job once when creating it (#2289)

* Convert exception to warning

* don't save job before creating the log container

* same as the python code

* test fix

* format

* format

* don't schedule the task if the log container is missing
This commit is contained in:
Cheick Keita
2022-08-24 10:19:18 -07:00
committed by GitHub
parent 07fb794f8a
commit 39e22b060a
4 changed files with 13 additions and 12 deletions

View File

@ -40,8 +40,6 @@ public class Jobs {
UserInfo = userInfo.OkV,
};
await _context.JobOperations.Insert(job);
// create the job logs container
var metadata = new Dictionary<string, string>{
{ "container_type", "logs" }, // TODO: use ContainerType.Logs enum somehow; needs snake case name
@ -60,7 +58,7 @@ public class Jobs {
// log container must not have the SAS included
var logContainerUri = new UriBuilder(containerSas) { Query = "" }.Uri;
job = job with { Config = job.Config with { Logs = logContainerUri.ToString() } };
await _context.JobOperations.Update(job);
await _context.JobOperations.Insert(job);
return await RequestHandling.Ok(req, JobResponse.ForJob(job));
}

View File

@ -6,7 +6,7 @@ namespace Microsoft.OneFuzz.Service;
public interface IConfig {
Async.Task<TaskUnitConfig> BuildTaskConfig(Job job, Task task);
Async.Task<TaskUnitConfig?> BuildTaskConfig(Job job, Task task);
Task<ResultVoid<TaskConfigError>> CheckConfig(TaskConfig config);
}
@ -49,14 +49,15 @@ public class Config : IConfig {
return blobPermissions;
}
public async Async.Task<TaskUnitConfig> BuildTaskConfig(Job job, Task task) {
public async Async.Task<TaskUnitConfig?> BuildTaskConfig(Job job, Task task) {
if (!Defs.TASK_DEFINITIONS.ContainsKey(task.Config.Task.Type)) {
throw new Exception($"unsupported task type: {task.Config.Task.Type}");
}
if (job.Config.Logs == null) {
throw new Exception($"Missing log container: job_id {job.JobId}, task_id {task.TaskId}");
_logTracer.Warning($"Missing log container: job_id {job.JobId}, task_id {task.TaskId}");
return null;
}
var definition = Defs.TASK_DEFINITIONS[task.Config.Task.Type];

View File

@ -136,6 +136,10 @@ public class Scheduler : IScheduler {
}
var taskConfig = await _config.BuildTaskConfig(job, task);
if (taskConfig == null) {
_logTracer.Info($"unable to build task config for task: {task.TaskId}");
return null;
}
var setupContainer = task.Config.Containers?.FirstOrDefault(c => c.Type == ContainerType.Setup) ?? throw new Exception($"task missing setup container: task_type = {task.Config.Task.Type}");
var setupPs1Exist = _containers.BlobExists(setupContainer.Name, "setup.ps1", StorageType.Corpus);
@ -177,8 +181,6 @@ public class Scheduler : IScheduler {
setupScript,
pool with { ETag = null });
return (bucketConfig, workUnit);
}

View File

@ -3,6 +3,8 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
from uuid import uuid4
import azure.functions as func
from onefuzztypes.enums import ContainerType, ErrorCode, JobState
from onefuzztypes.models import Error, JobConfig, JobTaskInfo
@ -53,9 +55,7 @@ def post(req: func.HttpRequest) -> func.HttpResponse:
if isinstance(user_info, Error):
return not_ok(user_info, context="jobs create")
job = Job(config=request, user_info=user_info)
job.save()
job = Job(job_id=uuid4(), config=request, user_info=user_info)
# create the job logs container
log_container_sas = create_container(
Container(f"logs-{job.job_id}"),