mirror of
https://github.com/microsoft/onefuzz.git
synced 2025-06-19 04:58:09 +00:00
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:
@ -40,8 +40,6 @@ public class Jobs {
|
|||||||
UserInfo = userInfo.OkV,
|
UserInfo = userInfo.OkV,
|
||||||
};
|
};
|
||||||
|
|
||||||
await _context.JobOperations.Insert(job);
|
|
||||||
|
|
||||||
// create the job logs container
|
// create the job logs container
|
||||||
var metadata = new Dictionary<string, string>{
|
var metadata = new Dictionary<string, string>{
|
||||||
{ "container_type", "logs" }, // TODO: use ContainerType.Logs enum somehow; needs snake case name
|
{ "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
|
// log container must not have the SAS included
|
||||||
var logContainerUri = new UriBuilder(containerSas) { Query = "" }.Uri;
|
var logContainerUri = new UriBuilder(containerSas) { Query = "" }.Uri;
|
||||||
job = job with { Config = job.Config with { Logs = logContainerUri.ToString() } };
|
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));
|
return await RequestHandling.Ok(req, JobResponse.ForJob(job));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ namespace Microsoft.OneFuzz.Service;
|
|||||||
|
|
||||||
|
|
||||||
public interface IConfig {
|
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);
|
Task<ResultVoid<TaskConfigError>> CheckConfig(TaskConfig config);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,14 +49,15 @@ public class Config : IConfig {
|
|||||||
return blobPermissions;
|
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)) {
|
if (!Defs.TASK_DEFINITIONS.ContainsKey(task.Config.Task.Type)) {
|
||||||
throw new Exception($"unsupported task type: {task.Config.Task.Type}");
|
throw new Exception($"unsupported task type: {task.Config.Task.Type}");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (job.Config.Logs == null) {
|
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];
|
var definition = Defs.TASK_DEFINITIONS[task.Config.Task.Type];
|
||||||
|
@ -136,6 +136,10 @@ public class Scheduler : IScheduler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var taskConfig = await _config.BuildTaskConfig(job, task);
|
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 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);
|
var setupPs1Exist = _containers.BlobExists(setupContainer.Name, "setup.ps1", StorageType.Corpus);
|
||||||
@ -177,8 +181,6 @@ public class Scheduler : IScheduler {
|
|||||||
setupScript,
|
setupScript,
|
||||||
pool with { ETag = null });
|
pool with { ETag = null });
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return (bucketConfig, workUnit);
|
return (bucketConfig, workUnit);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
# Copyright (c) Microsoft Corporation.
|
# Copyright (c) Microsoft Corporation.
|
||||||
# Licensed under the MIT License.
|
# Licensed under the MIT License.
|
||||||
|
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
import azure.functions as func
|
import azure.functions as func
|
||||||
from onefuzztypes.enums import ContainerType, ErrorCode, JobState
|
from onefuzztypes.enums import ContainerType, ErrorCode, JobState
|
||||||
from onefuzztypes.models import Error, JobConfig, JobTaskInfo
|
from onefuzztypes.models import Error, JobConfig, JobTaskInfo
|
||||||
@ -53,9 +55,7 @@ def post(req: func.HttpRequest) -> func.HttpResponse:
|
|||||||
if isinstance(user_info, Error):
|
if isinstance(user_info, Error):
|
||||||
return not_ok(user_info, context="jobs create")
|
return not_ok(user_info, context="jobs create")
|
||||||
|
|
||||||
job = Job(config=request, user_info=user_info)
|
job = Job(job_id=uuid4(), config=request, user_info=user_info)
|
||||||
job.save()
|
|
||||||
|
|
||||||
# create the job logs container
|
# create the job logs container
|
||||||
log_container_sas = create_container(
|
log_container_sas = create_container(
|
||||||
Container(f"logs-{job.job_id}"),
|
Container(f"logs-{job.job_id}"),
|
||||||
|
Reference in New Issue
Block a user