Skip to content

Commit

Permalink
Updated readme and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alicejgibbons committed Feb 27, 2025
1 parent 079c7cf commit ef2cc1f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 30 deletions.
14 changes: 10 additions & 4 deletions jobs/csharp/sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -76,19 +74,27 @@ 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
```

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
```
Expand Down
6 changes: 3 additions & 3 deletions jobs/csharp/sdk/job-scheduler/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@

async Task ScheduleJob(DroidJob job)
{
Console.WriteLine($"Scheduling job: " + job.Name);
Console.WriteLine($"Scheduling job...");

try
{
var response = await httpClient.PostAsJsonAsync("/scheduleJob", 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)
{
Expand All @@ -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
{
Expand Down
51 changes: 28 additions & 23 deletions jobs/csharp/sdk/job-service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

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)
{
Expand All @@ -25,19 +24,22 @@
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;
Expand All @@ -46,25 +48,25 @@
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;
await context.Response.WriteAsync("Job name required");
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");
Expand All @@ -76,44 +78,46 @@
{
var jobName = context.Request.RouteValues["name"]?.ToString();
Console.WriteLine($"Deleting job: " + jobName);

if (string.IsNullOrEmpty(jobName))
{
context.Response.StatusCode = 400;
await context.Response.WriteAsync("Job name required");
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");
}
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.");
Expand All @@ -122,7 +126,8 @@
// 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}");
Expand Down Expand Up @@ -150,7 +155,7 @@ public class DroidJob

[JsonPropertyName("job")]
public string? Job { get; set; }

[JsonPropertyName("dueTime")]
public int DueTime { get; set; }
}
Expand Down

0 comments on commit ef2cc1f

Please sign in to comment.