|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using Microsoft.AspNetCore.Mvc; |
| 5 | +using Microsoft.DurableTask; |
| 6 | +using Microsoft.DurableTask.Client; |
| 7 | +using Microsoft.DurableTask.ExportHistory; |
| 8 | +using ExportHistoryWebApp.Models; |
| 9 | + |
| 10 | +namespace ExportHistoryWebApp.Controllers; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Controller for managing export history jobs through a REST API. |
| 14 | +/// Provides endpoints for creating, reading, listing, and deleting export jobs. |
| 15 | +/// </summary> |
| 16 | +[ApiController] |
| 17 | +[Route("export-jobs")] |
| 18 | +public class ExportJobController : ControllerBase |
| 19 | +{ |
| 20 | + readonly ExportHistoryClient exportHistoryClient; |
| 21 | + readonly ILogger<ExportJobController> logger; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Initializes a new instance of the <see cref="ExportJobController"/> class. |
| 25 | + /// </summary> |
| 26 | + /// <param name="exportHistoryClient">Client for managing export history jobs.</param> |
| 27 | + /// <param name="logger">Logger for recording controller operations.</param> |
| 28 | + public ExportJobController( |
| 29 | + ExportHistoryClient exportHistoryClient, |
| 30 | + ILogger<ExportJobController> logger) |
| 31 | + { |
| 32 | + this.exportHistoryClient = exportHistoryClient ?? throw new ArgumentNullException(nameof(exportHistoryClient)); |
| 33 | + this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Creates a new export job based on the provided configuration. |
| 38 | + /// </summary> |
| 39 | + /// <param name="request">The export job creation request.</param> |
| 40 | + /// <returns>The created export job description.</returns> |
| 41 | + [HttpPost] |
| 42 | + public async Task<ActionResult<ExportJobDescription>> CreateExportJob([FromBody] CreateExportJobRequest request) |
| 43 | + { |
| 44 | + if (request == null) |
| 45 | + { |
| 46 | + return this.BadRequest("createExportJobRequest cannot be null"); |
| 47 | + } |
| 48 | + |
| 49 | + try |
| 50 | + { |
| 51 | + ExportDestination? destination = null; |
| 52 | + if (!string.IsNullOrEmpty(request.Container)) |
| 53 | + { |
| 54 | + destination = new ExportDestination(request.Container) |
| 55 | + { |
| 56 | + Prefix = request.Prefix, |
| 57 | + }; |
| 58 | + } |
| 59 | + |
| 60 | + ExportJobCreationOptions creationOptions = new ExportJobCreationOptions( |
| 61 | + mode: request.Mode, |
| 62 | + completedTimeFrom: request.CompletedTimeFrom, |
| 63 | + completedTimeTo: request.CompletedTimeTo, |
| 64 | + destination: destination, |
| 65 | + jobId: request.JobId, |
| 66 | + format: request.Format, |
| 67 | + runtimeStatus: request.RuntimeStatus, |
| 68 | + maxInstancesPerBatch: request.MaxInstancesPerBatch); |
| 69 | + |
| 70 | + ExportHistoryJobClient jobClient = await this.exportHistoryClient.CreateJobAsync(creationOptions); |
| 71 | + ExportJobDescription description = await jobClient.DescribeAsync(); |
| 72 | + |
| 73 | + this.logger.LogInformation("Created new export job with ID: {JobId}", description.JobId); |
| 74 | + |
| 75 | + return this.CreatedAtAction(nameof(GetExportJob), new { id = description.JobId }, description); |
| 76 | + } |
| 77 | + catch (ArgumentException ex) |
| 78 | + { |
| 79 | + this.logger.LogError(ex, "Validation failed while creating export job {JobId}", request.JobId); |
| 80 | + return this.BadRequest(ex.Message); |
| 81 | + } |
| 82 | + catch (Exception ex) |
| 83 | + { |
| 84 | + this.logger.LogError(ex, "Error creating export job {JobId}", request.JobId); |
| 85 | + return this.StatusCode(500, "An error occurred while creating the export job"); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Retrieves a specific export job by its ID. |
| 91 | + /// </summary> |
| 92 | + /// <param name="id">The ID of the export job to retrieve.</param> |
| 93 | + /// <returns>The export job description if found.</returns> |
| 94 | + [HttpGet("{id}")] |
| 95 | + public async Task<ActionResult<ExportJobDescription>> GetExportJob(string id) |
| 96 | + { |
| 97 | + try |
| 98 | + { |
| 99 | + ExportJobDescription? job = await this.exportHistoryClient.GetJobAsync(id); |
| 100 | + return this.Ok(job); |
| 101 | + } |
| 102 | + catch (ExportJobNotFoundException) |
| 103 | + { |
| 104 | + return this.NotFound(); |
| 105 | + } |
| 106 | + catch (Exception ex) |
| 107 | + { |
| 108 | + this.logger.LogError(ex, "Error retrieving export job {JobId}", id); |
| 109 | + return this.StatusCode(500, "An error occurred while retrieving the export job"); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// Lists all export jobs, optionally filtered by query parameters. |
| 115 | + /// </summary> |
| 116 | + /// <param name="status">Optional filter by job status.</param> |
| 117 | + /// <param name="jobIdPrefix">Optional filter by job ID prefix.</param> |
| 118 | + /// <param name="createdFrom">Optional filter for jobs created after this time.</param> |
| 119 | + /// <param name="createdTo">Optional filter for jobs created before this time.</param> |
| 120 | + /// <param name="pageSize">Optional page size for pagination.</param> |
| 121 | + /// <param name="continuationToken">Optional continuation token for pagination.</param> |
| 122 | + /// <returns>A collection of export job descriptions.</returns> |
| 123 | + [HttpGet("list")] |
| 124 | + public async Task<ActionResult<IEnumerable<ExportJobDescription>>> ListExportJobs( |
| 125 | + [FromQuery] ExportJobStatus? status = null, |
| 126 | + [FromQuery] string? jobIdPrefix = null, |
| 127 | + [FromQuery] DateTimeOffset? createdFrom = null, |
| 128 | + [FromQuery] DateTimeOffset? createdTo = null, |
| 129 | + [FromQuery] int? pageSize = null, |
| 130 | + [FromQuery] string? continuationToken = null) |
| 131 | + { |
| 132 | + this.logger.LogInformation("GET list endpoint called with method: {Method}", this.HttpContext.Request.Method); |
| 133 | + try |
| 134 | + { |
| 135 | + ExportJobQuery? query = null; |
| 136 | + if ( |
| 137 | + status.HasValue || |
| 138 | + !string.IsNullOrEmpty(jobIdPrefix) || |
| 139 | + createdFrom.HasValue || |
| 140 | + createdTo.HasValue || |
| 141 | + pageSize.HasValue || |
| 142 | + !string.IsNullOrEmpty(continuationToken) |
| 143 | + ) |
| 144 | + { |
| 145 | + query = new ExportJobQuery |
| 146 | + { |
| 147 | + Status = status, |
| 148 | + JobIdPrefix = jobIdPrefix, |
| 149 | + CreatedFrom = createdFrom, |
| 150 | + CreatedTo = createdTo, |
| 151 | + PageSize = pageSize, |
| 152 | + ContinuationToken = continuationToken, |
| 153 | + }; |
| 154 | + } |
| 155 | + |
| 156 | + AsyncPageable<ExportJobDescription> jobs = this.exportHistoryClient.ListJobsAsync(query); |
| 157 | + |
| 158 | + // Collect all jobs from the async pageable |
| 159 | + List<ExportJobDescription> jobList = new List<ExportJobDescription>(); |
| 160 | + await foreach (ExportJobDescription job in jobs) |
| 161 | + { |
| 162 | + jobList.Add(job); |
| 163 | + } |
| 164 | + |
| 165 | + return this.Ok(jobList); |
| 166 | + } |
| 167 | + catch (Exception ex) |
| 168 | + { |
| 169 | + this.logger.LogError(ex, "Error retrieving export jobs"); |
| 170 | + return this.StatusCode(500, "An error occurred while retrieving export jobs"); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + /// <summary> |
| 175 | + /// Deletes an export job by its ID. |
| 176 | + /// </summary> |
| 177 | + /// <param name="id">The ID of the export job to delete.</param> |
| 178 | + /// <returns>No content if successful.</returns> |
| 179 | + [HttpDelete("{id}")] |
| 180 | + public async Task<IActionResult> DeleteExportJob(string id) |
| 181 | + { |
| 182 | + this.logger.LogInformation("DELETE endpoint called for job ID: {JobId}", id); |
| 183 | + try |
| 184 | + { |
| 185 | + ExportHistoryJobClient jobClient = this.exportHistoryClient.GetJobClient(id); |
| 186 | + await jobClient.DeleteAsync(); |
| 187 | + this.logger.LogInformation("Successfully deleted export job {JobId}", id); |
| 188 | + return this.NoContent(); |
| 189 | + } |
| 190 | + catch (ExportJobNotFoundException) |
| 191 | + { |
| 192 | + this.logger.LogWarning("Export job {JobId} not found for deletion", id); |
| 193 | + return this.NotFound(); |
| 194 | + } |
| 195 | + catch (Exception ex) |
| 196 | + { |
| 197 | + this.logger.LogError(ex, "Error deleting export job {JobId}", id); |
| 198 | + return this.StatusCode(500, "An error occurred while deleting the export job"); |
| 199 | + } |
| 200 | + } |
| 201 | +} |
| 202 | + |
0 commit comments