Updated readme and tests

Signed-off-by: Alice Gibbons <alice@diagrid.io>
This commit is contained in:
Alice Gibbons 2025-02-27 11:26:24 -06:00
parent 079c7cf0e6
commit 259ef15e9a
3 changed files with 41 additions and 30 deletions

View File

@ -48,10 +48,8 @@ name: Run multi app run template
expected_stdout_lines: expected_stdout_lines:
- '== APP - job-service-sdk == Job Scheduled: R2-D2' - '== APP - job-service-sdk == Job Scheduled: R2-D2'
- '== APP - job-service-sdk == Job Scheduled: C-3PO' - '== APP - job-service-sdk == Job Scheduled: C-3PO'
- '== APP - job-service-sdk == Received invocation for the job R2-D2 with job data droid R2-D2'
- '== APP - job-service-sdk == Starting droid: R2-D2' - '== APP - job-service-sdk == Starting droid: R2-D2'
- '== APP - job-service-sdk == Executing maintenance job: Oil Change' - '== APP - job-service-sdk == Executing maintenance job: Oil Change'
- '== APP - job-service-sdk == Received trigger invocation for job name: C-3PO'
- '== APP - job-service-sdk == Starting droid: C-3PO' - '== APP - job-service-sdk == Starting droid: C-3PO'
- '== APP - job-service-sdk == Executing maintenance job: Limb Calibration' - '== APP - job-service-sdk == Executing maintenance job: Limb Calibration'
expected_stderr_lines: expected_stderr_lines:
@ -76,11 +74,19 @@ The terminal console output should look similar to this, where:
- The `C-3PO` job is being executed after 20 seconds. - The `C-3PO` job is being executed after 20 seconds.
```text ```text
== APP - job-scheduler-sdk == Scheduling job...
== APP - job-service-sdk == Job Scheduled: R2-D2 == APP - job-service-sdk == Job Scheduled: R2-D2
== APP - job-scheduler-sdk == Job scheduled: {"name":"R2-D2","job":"Oil Change","dueTime":15}
== APP - job-scheduler-sdk == Getting job: R2-D2
== APP - job-service-sdk == Getting job...
== APP - job-scheduler-sdk == Job details: {"schedule":"@every 15s","repeatCount":1,"dueTime":null,"ttl":null,"payload":"ChtkYXByLmlvL3NjaGVkdWxlL2pvYnBheWxvYWQSJXsiZHJvaWQiOiJSMi1EMiIsInRhc2siOiJPaWwgQ2hhbmdlIn0="} == APP - job-scheduler-sdk == Job details: {"schedule":"@every 15s","repeatCount":1,"dueTime":null,"ttl":null,"payload":"ChtkYXByLmlvL3NjaGVkdWxlL2pvYnBheWxvYWQSJXsiZHJvaWQiOiJSMi1EMiIsInRhc2siOiJPaWwgQ2hhbmdlIn0="}
== APP - job-scheduler-sdk == Scheduling job...
== APP - job-service-sdk == Job Scheduled: C-3PO == APP - job-service-sdk == Job Scheduled: C-3PO
== APP - job-scheduler-sdk == Job scheduled: {"name":"C-3PO","job":"Limb Calibration","dueTime":20}
== APP - job-scheduler-sdk == Getting job: C-3PO
== APP - job-service-sdk == Getting job...
== APP - job-scheduler-sdk == Job details: {"schedule":"@every 20s","repeatCount":1,"dueTime":null,"ttl":null,"payload":"ChtkYXByLmlvL3NjaGVkdWxlL2pvYnBheWxvYWQSK3siZHJvaWQiOiJDLTNQTyIsInRhc2siOiJMaW1iIENhbGlicmF0aW9uIn0="} == APP - job-scheduler-sdk == Job details: {"schedule":"@every 20s","repeatCount":1,"dueTime":null,"ttl":null,"payload":"ChtkYXByLmlvL3NjaGVkdWxlL2pvYnBheWxvYWQSK3siZHJvaWQiOiJDLTNQTyIsInRhc2siOiJMaW1iIENhbGlicmF0aW9uIn0="}
== APP - job-service-sdk == Received job request... == APP - job-service-sdk == Handling job...
== APP - job-service-sdk == Starting droid: R2-D2 == APP - job-service-sdk == Starting droid: R2-D2
== APP - job-service-sdk == Executing maintenance job: Oil Change == APP - job-service-sdk == Executing maintenance job: Oil Change
``` ```
@ -88,7 +94,7 @@ The terminal console output should look similar to this, where:
After 20 seconds, the terminal output should present the `C-3PO` job being processed: After 20 seconds, the terminal output should present the `C-3PO` job being processed:
```text ```text
== APP - job-service-sdk == Received job request... == APP - job-service-sdk == Handling job...
== APP - job-service-sdk == Starting droid: C-3PO == APP - job-service-sdk == Starting droid: C-3PO
== APP - job-service-sdk == Executing maintenance job: Limb Calibration == APP - job-service-sdk == Executing maintenance job: Limb Calibration
``` ```

View File

@ -52,7 +52,7 @@ catch (Exception ex)
async Task ScheduleJob(DroidJob job) async Task ScheduleJob(DroidJob job)
{ {
Console.WriteLine($"Scheduling job: " + job.Name); Console.WriteLine($"Scheduling job...");
try try
{ {
@ -60,7 +60,7 @@ async Task ScheduleJob(DroidJob job)
var result = await response.Content.ReadAsStringAsync(); var result = await response.Content.ReadAsStringAsync();
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
Console.WriteLine($"Job scheduled successfully: {job.Name}, {result}"); Console.WriteLine($"Job scheduled: {result}");
} }
catch (Exception e) catch (Exception e)
{ {
@ -70,7 +70,7 @@ async Task ScheduleJob(DroidJob job)
async Task GetJobDetails(DroidJob job) async Task GetJobDetails(DroidJob job)
{ {
Console.WriteLine($"Getting job details: " + job.Name); Console.WriteLine($"Getting job: " + job.Name);
try try
{ {

View File

@ -16,7 +16,6 @@ var jobsClient = app.Services.GetRequiredService<DaprJobsClient>();
app.MapPost("/scheduleJob", async (HttpContext context) => app.MapPost("/scheduleJob", async (HttpContext context) =>
{ {
Console.WriteLine("Scheduling job...");
var droidJob = await JsonSerializer.DeserializeAsync<DroidJob>(context.Request.Body); var droidJob = await JsonSerializer.DeserializeAsync<DroidJob>(context.Request.Body);
if (droidJob?.Name is null || droidJob?.Job is null) if (droidJob?.Name is null || droidJob?.Job is null)
{ {
@ -25,19 +24,22 @@ app.MapPost("/scheduleJob", async (HttpContext context) =>
return; return;
} }
try { try
var jobData = new JobData { {
var jobData = new JobData
{
Droid = droidJob.Name, Droid = droidJob.Name,
Task = droidJob.Job Task = droidJob.Job
}; };
await jobsClient.ScheduleJobWithPayloadAsync(droidJob.Name, DaprJobSchedule.FromDuration(TimeSpan.FromSeconds(droidJob.DueTime)), payload: jobData, repeats: 1); //Schedule cron job that repeats once await jobsClient.ScheduleJobWithPayloadAsync(droidJob.Name, DaprJobSchedule.FromDuration(TimeSpan.FromSeconds(droidJob.DueTime)), payload: jobData, repeats: 1); //Schedule cron job that repeats once
Console.WriteLine($"Job Scheduled: {droidJob.Name}");
Console.WriteLine($"Job Scheduled: {droidJob.Name}");
context.Response.StatusCode = 200; context.Response.StatusCode = 200;
await context.Response.WriteAsJsonAsync(droidJob); await context.Response.WriteAsJsonAsync(droidJob);
}
} catch (Exception e) { catch (Exception e)
{
Console.WriteLine($"Error scheduling job: " + e); Console.WriteLine($"Error scheduling job: " + e);
} }
return; return;
@ -46,8 +48,8 @@ app.MapPost("/scheduleJob", async (HttpContext context) =>
app.MapGet("/getJob/{name}", async (HttpContext context) => app.MapGet("/getJob/{name}", async (HttpContext context) =>
{ {
var jobName = context.Request.RouteValues["name"]?.ToString(); var jobName = context.Request.RouteValues["name"]?.ToString();
Console.WriteLine($"Getting job: " + jobName); Console.WriteLine($"Getting job...");
if (string.IsNullOrEmpty(jobName)) if (string.IsNullOrEmpty(jobName))
{ {
context.Response.StatusCode = 400; context.Response.StatusCode = 400;
@ -55,16 +57,16 @@ app.MapGet("/getJob/{name}", async (HttpContext context) =>
return; return;
} }
try { try
// Error here: ---> System.FormatException: String '' was not recognized as a valid DateTime. {
var jobDetails = await jobsClient.GetJobAsync(jobName); var jobDetails = await jobsClient.GetJobAsync(jobName);
Console.WriteLine($"Job schedule: {jobDetails?.Schedule}");
Console.WriteLine($"Job payload: {jobDetails?.Payload}");
context.Response.StatusCode = 200; context.Response.StatusCode = 200;
await context.Response.WriteAsJsonAsync(jobDetails); await context.Response.WriteAsJsonAsync(jobDetails);
} catch (Exception e) { }
catch (Exception e)
{
Console.WriteLine($"Error getting job: " + e); Console.WriteLine($"Error getting job: " + e);
context.Response.StatusCode = 400; context.Response.StatusCode = 400;
await context.Response.WriteAsync($"Error getting job"); await context.Response.WriteAsync($"Error getting job");
@ -76,7 +78,7 @@ app.MapDelete("/deleteJob/{name}", async (HttpContext context) =>
{ {
var jobName = context.Request.RouteValues["name"]?.ToString(); var jobName = context.Request.RouteValues["name"]?.ToString();
Console.WriteLine($"Deleting job: " + jobName); Console.WriteLine($"Deleting job: " + jobName);
if (string.IsNullOrEmpty(jobName)) if (string.IsNullOrEmpty(jobName))
{ {
context.Response.StatusCode = 400; context.Response.StatusCode = 400;
@ -84,14 +86,17 @@ app.MapDelete("/deleteJob/{name}", async (HttpContext context) =>
return; return;
} }
try { try
{
await jobsClient.DeleteJobAsync(jobName); await jobsClient.DeleteJobAsync(jobName);
Console.WriteLine($"Job deleted: {jobName}"); Console.WriteLine($"Job deleted: {jobName}");
context.Response.StatusCode = 200; context.Response.StatusCode = 200;
await context.Response.WriteAsync("Job deleted"); await context.Response.WriteAsync("Job deleted");
} catch (Exception e) { }
catch (Exception e)
{
Console.WriteLine($"Error deleting job: " + e); Console.WriteLine($"Error deleting job: " + e);
context.Response.StatusCode = 400; context.Response.StatusCode = 400;
await context.Response.WriteAsync($"Error deleting job"); await context.Response.WriteAsync($"Error deleting job");
@ -99,21 +104,20 @@ app.MapDelete("/deleteJob/{name}", async (HttpContext context) =>
return; return;
}); });
//Job handler route to capture incoming jobs // Job handler route to capture incoming jobs
app.MapDaprScheduledJobHandler((string jobName, ReadOnlyMemory<byte> jobPayload) => app.MapDaprScheduledJobHandler((string jobName, ReadOnlyMemory<byte> jobPayload) =>
{ {
Console.WriteLine($"Received trigger invocation for job name: {jobName}"); Console.WriteLine("Handling job...");
var deserializedPayload = Encoding.UTF8.GetString(jobPayload.Span); var deserializedPayload = Encoding.UTF8.GetString(jobPayload.Span);
try try
{ {
if (deserializedPayload is null){ if (deserializedPayload is null)
{
throw new Exception("Payload is null"); throw new Exception("Payload is null");
} }
var jobData = JsonSerializer.Deserialize<JobData>(deserializedPayload); var jobData = JsonSerializer.Deserialize<JobData>(deserializedPayload);
Console.WriteLine($"Received invocation for the job {jobName} with job data droid {jobData?.Droid}");
if (jobData?.Droid is null || jobData?.Task is null) if (jobData?.Droid is null || jobData?.Task is null)
{ {
throw new Exception("Invalid format of job data."); throw new Exception("Invalid format of job data.");
@ -122,7 +126,8 @@ app.MapDaprScheduledJobHandler((string jobName, ReadOnlyMemory<byte> jobPayload)
// Handling Droid Job from decoded value // Handling Droid Job from decoded value
Console.WriteLine($"Starting droid: {jobData.Droid}"); Console.WriteLine($"Starting droid: {jobData.Droid}");
Console.WriteLine($"Executing maintenance job: {jobData.Task}"); Console.WriteLine($"Executing maintenance job: {jobData.Task}");
} catch (Exception ex) }
catch (Exception ex)
{ {
Console.WriteLine($"Failed to handle job {jobName}"); Console.WriteLine($"Failed to handle job {jobName}");
Console.Error.WriteLine($"Error handling job: {ex.Message}"); Console.Error.WriteLine($"Error handling job: {ex.Message}");
@ -150,7 +155,7 @@ public class DroidJob
[JsonPropertyName("job")] [JsonPropertyName("job")]
public string? Job { get; set; } public string? Job { get; set; }
[JsonPropertyName("dueTime")] [JsonPropertyName("dueTime")]
public int DueTime { get; set; } public int DueTime { get; set; }
} }