Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Oct 8, 2024
2 parents f87bd44 + 711aaff commit 6e298db
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 12 deletions.
36 changes: 26 additions & 10 deletions AiServer.ServiceInterface/GenerationServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,19 @@ public static async Task<object> ProcessGeneration(this CreateGeneration diffReq
}

// We know at this point, we definitely have a job
JobResult queuedJob = job;
JobResult? queuedJob = job;

// Return the job status URL
var jobStatusUrl = AppConfig.Instance.ApplicationBaseUrl.CombineWith(
$"/api/{nameof(GetJobStatus)}?RefId=" + diffResponse.RefId);

var queueResponse = new QueueGenerationResponse
{
RefId = diffResponse.RefId,
JobId = diffResponse.Id,
Status = queuedJob.Job?.Status,
JobState = queuedJob.Job?.State ?? queuedJob.Summary.State
JobState = queuedJob.Job?.State ?? queuedJob.Summary.State,
StatusUrl = jobStatusUrl
};

// Handle failed jobs
Expand Down Expand Up @@ -233,21 +238,32 @@ public static async Task<object> ProcessGeneration(this CreateGeneration diffReq
queuedJob = jobs.GetJob(diffResponse.Id);
}

// Handle null job
if (queuedJob?.Job == null || queuedJob.Job.State == BackgroundJobState.Queued ||
queuedJob.Job.State == BackgroundJobState.Started)
throw new Exception("Failed to complete job within timeout");

// Handle failed jobs
if (queuedJob.Failed != null)
throw new Exception($"Job failed: {queuedJob.Failed.Error}");

// Handle cancelled jobs
if (queuedJob.Job?.State == BackgroundJobState.Cancelled)
throw new Exception("Job was cancelled");

// Process successful job results
var jobReqRes = queuedJob.Job!.ExtractRequestResponse<CreateGeneration, GenerationResult>();
var jobReq = jobReqRes.Item1;
var jobRes = jobReqRes.Item2;
if (jobRes != null)
{
var outputs = queuedJob.GetOutputs();
completedResponse.Outputs = outputs.Item1;
completedResponse.TextOutputs = outputs.Item2;
}
if (jobRes == null)
return completedResponse;
var outputs = queuedJob.GetOutputs();
completedResponse.Outputs = outputs.Item1;
completedResponse.TextOutputs = outputs.Item2;

return completedResponse;
}

public static Tuple<List<ArtifactOutput>?,List<TextOutput>?> GetOutputs(this JobResult job)
public static Tuple<List<ArtifactOutput>?,List<TextOutput>?> GetOutputs(this JobResult? job)
{
var outputs = new List<ArtifactOutput>();
var textOutputs = new List<TextOutput>();
Expand Down
4 changes: 4 additions & 0 deletions AiServer.ServiceInterface/OpenAiChatServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,15 @@ public async Task<object> Any(QueueOpenAiChatCompletion request)
},
Worker = useProvider.Name,
});

var jobStatusUrl = AppConfig.Instance.ApplicationBaseUrl
.CombineWith($"/api/{nameof(GetOpenAiChatStatus)}?RefId=" + jobRef.RefId);

var response = new QueueOpenAiChatResponse
{
Id = jobRef.Id,
RefId = jobRef.RefId,
StatusUrl = jobStatusUrl
};

return response;
Expand Down
7 changes: 6 additions & 1 deletion AiServer.ServiceInterface/VideoServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,18 @@ public static async Task<object> ProcessTransform(this CreateMediaTransform medi

// We know at this point, we definitely have a job
var queuedJob = job;

// Return the job status URL
var jobStatusUrl = AppConfig.Instance.ApplicationBaseUrl.CombineWith(
$"/api/{nameof(GetJobStatus)}?RefId=" + transformResponse.RefId);

var queueTransformResponse = new QueueMediaTransformResponse
{
RefId = transformResponse.RefId,
JobId = transformResponse.Id,
Status = queuedJob.Job?.Status,
JobState = queuedJob.Job?.State ?? queuedJob.Summary.State
JobState = queuedJob.Job?.State ?? queuedJob.Summary.State,
StatusUrl = jobStatusUrl
};

// Handle failed jobs
Expand Down
3 changes: 3 additions & 0 deletions AiServer.ServiceModel/OpenAiChatServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public class QueueOpenAiChatResponse
{
public long Id { get; set; }
public string RefId { get; set; }

public string StatusUrl { get; set; }

public ResponseStatus? ResponseStatus { get; set; }
}

Expand Down
6 changes: 5 additions & 1 deletion AiServer.ServiceModel/QueueGenerations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ public interface IQueueGeneration
[Tag(Tag.Jobs)]
[Api("Get job status")]
[Description("Retrieve the status of a background job")]
public class GetJobStatus : IReturn<GetJobStatusResponse>
public class GetJobStatus : IGet, IReturn<GetJobStatusResponse>
{
[ApiMember(Description = "Unique identifier of the background job")]
[Description("Unique identifier of the background job")]
Expand Down Expand Up @@ -390,6 +390,10 @@ public class QueueGenerationResponse
[ApiMember(Description = "Detailed response status information")]
[Description("Detailed response status information")]
public ResponseStatus? ResponseStatus { get; set; }

[ApiMember(Description = "URL to check the status of the generation request")]
[Description("URL to check the status of the generation request")]
public string StatusUrl { get; set; }
}

[Description("Output object for generated artifacts")]
Expand Down
4 changes: 4 additions & 0 deletions AiServer.ServiceModel/QueueMediaTransforms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ public class QueueMediaTransformResponse
[ApiMember(Description = "Detailed response status information")]
[Description("Detailed response status information")]
public ResponseStatus? ResponseStatus { get; set; }

[ApiMember(Description = "URL to check the status of the request")]
[Description("URL to check the status of the request")]
public string StatusUrl { get; set; }
}

[Description("Convert an image to a different format")]
Expand Down
Binary file added AiServer.Tests/files/test_video.mp4
Binary file not shown.

0 comments on commit 6e298db

Please sign in to comment.