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:
- '== APP - job-service-sdk == Job Scheduled: R2-D2'
- '== 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 == 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 == Executing maintenance job: Limb Calibration'
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.
```text
== APP - job-scheduler-sdk == Scheduling job...
== 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 == Scheduling job...
== 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-service-sdk == Received job request...
== APP - job-service-sdk == Handling job...
== APP - job-service-sdk == Starting droid: R2-D2
== 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:
```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 == Executing maintenance job: Limb Calibration
```

View File

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

View File

@ -16,7 +16,6 @@ var jobsClient = app.Services.GetRequiredService<DaprJobsClient>();
app.MapPost("/scheduleJob", async (HttpContext context) =>
{
Console.WriteLine("Scheduling job...");
var droidJob = await JsonSerializer.DeserializeAsync<DroidJob>(context.Request.Body);
if (droidJob?.Name is null || droidJob?.Job is null)
{
@ -25,19 +24,22 @@ app.MapPost("/scheduleJob", async (HttpContext context) =>
return;
}
try {
var jobData = new JobData {
try
{
var jobData = new JobData
{
Droid = droidJob.Name,
Task = droidJob.Job
};
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;
await context.Response.WriteAsJsonAsync(droidJob);
} catch (Exception e) {
}
catch (Exception e)
{
Console.WriteLine($"Error scheduling job: " + e);
}
return;
@ -46,8 +48,8 @@ app.MapPost("/scheduleJob", async (HttpContext context) =>
app.MapGet("/getJob/{name}", async (HttpContext context) =>
{
var jobName = context.Request.RouteValues["name"]?.ToString();
Console.WriteLine($"Getting job: " + jobName);
Console.WriteLine($"Getting job...");
if (string.IsNullOrEmpty(jobName))
{
context.Response.StatusCode = 400;
@ -55,16 +57,16 @@ app.MapGet("/getJob/{name}", async (HttpContext context) =>
return;
}
try {
// Error here: ---> System.FormatException: String '' was not recognized as a valid DateTime.
try
{
var jobDetails = await jobsClient.GetJobAsync(jobName);
Console.WriteLine($"Job schedule: {jobDetails?.Schedule}");
Console.WriteLine($"Job payload: {jobDetails?.Payload}");
context.Response.StatusCode = 200;
await context.Response.WriteAsJsonAsync(jobDetails);
} catch (Exception e) {
}
catch (Exception e)
{
Console.WriteLine($"Error getting job: " + e);
context.Response.StatusCode = 400;
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();
Console.WriteLine($"Deleting job: " + jobName);
if (string.IsNullOrEmpty(jobName))
{
context.Response.StatusCode = 400;
@ -84,14 +86,17 @@ app.MapDelete("/deleteJob/{name}", async (HttpContext context) =>
return;
}
try {
try
{
await jobsClient.DeleteJobAsync(jobName);
Console.WriteLine($"Job deleted: {jobName}");
context.Response.StatusCode = 200;
await context.Response.WriteAsync("Job deleted");
} catch (Exception e) {
}
catch (Exception e)
{
Console.WriteLine($"Error deleting job: " + e);
context.Response.StatusCode = 400;
await context.Response.WriteAsync($"Error deleting job");
@ -99,21 +104,20 @@ app.MapDelete("/deleteJob/{name}", async (HttpContext context) =>
return;
});
//Job handler route to capture incoming jobs
// Job handler route to capture incoming jobs
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);
try
{
if (deserializedPayload is null){
if (deserializedPayload is null)
{
throw new Exception("Payload is null");
}
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)
{
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
Console.WriteLine($"Starting droid: {jobData.Droid}");
Console.WriteLine($"Executing maintenance job: {jobData.Task}");
} catch (Exception ex)
}
catch (Exception ex)
{
Console.WriteLine($"Failed to handle job {jobName}");
Console.Error.WriteLine($"Error handling job: {ex.Message}");
@ -150,7 +155,7 @@ public class DroidJob
[JsonPropertyName("job")]
public string? Job { get; set; }
[JsonPropertyName("dueTime")]
public int DueTime { get; set; }
}