Skip to content

Commit 259ef15

Browse files
committed
Updated readme and tests
Signed-off-by: Alice Gibbons <[email protected]>
1 parent 079c7cf commit 259ef15

File tree

3 files changed

+41
-30
lines changed

3 files changed

+41
-30
lines changed

jobs/csharp/sdk/README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,8 @@ name: Run multi app run template
4848
expected_stdout_lines:
4949
- '== APP - job-service-sdk == Job Scheduled: R2-D2'
5050
- '== APP - job-service-sdk == Job Scheduled: C-3PO'
51-
- '== APP - job-service-sdk == Received invocation for the job R2-D2 with job data droid R2-D2'
5251
- '== APP - job-service-sdk == Starting droid: R2-D2'
5352
- '== APP - job-service-sdk == Executing maintenance job: Oil Change'
54-
- '== APP - job-service-sdk == Received trigger invocation for job name: C-3PO'
5553
- '== APP - job-service-sdk == Starting droid: C-3PO'
5654
- '== APP - job-service-sdk == Executing maintenance job: Limb Calibration'
5755
expected_stderr_lines:
@@ -76,19 +74,27 @@ The terminal console output should look similar to this, where:
7674
- The `C-3PO` job is being executed after 20 seconds.
7775

7876
```text
77+
== APP - job-scheduler-sdk == Scheduling job...
7978
== APP - job-service-sdk == Job Scheduled: R2-D2
79+
== APP - job-scheduler-sdk == Job scheduled: {"name":"R2-D2","job":"Oil Change","dueTime":15}
80+
== APP - job-scheduler-sdk == Getting job: R2-D2
81+
== APP - job-service-sdk == Getting job...
8082
== APP - job-scheduler-sdk == Job details: {"schedule":"@every 15s","repeatCount":1,"dueTime":null,"ttl":null,"payload":"ChtkYXByLmlvL3NjaGVkdWxlL2pvYnBheWxvYWQSJXsiZHJvaWQiOiJSMi1EMiIsInRhc2siOiJPaWwgQ2hhbmdlIn0="}
83+
== APP - job-scheduler-sdk == Scheduling job...
8184
== APP - job-service-sdk == Job Scheduled: C-3PO
85+
== APP - job-scheduler-sdk == Job scheduled: {"name":"C-3PO","job":"Limb Calibration","dueTime":20}
86+
== APP - job-scheduler-sdk == Getting job: C-3PO
87+
== APP - job-service-sdk == Getting job...
8288
== APP - job-scheduler-sdk == Job details: {"schedule":"@every 20s","repeatCount":1,"dueTime":null,"ttl":null,"payload":"ChtkYXByLmlvL3NjaGVkdWxlL2pvYnBheWxvYWQSK3siZHJvaWQiOiJDLTNQTyIsInRhc2siOiJMaW1iIENhbGlicmF0aW9uIn0="}
83-
== APP - job-service-sdk == Received job request...
89+
== APP - job-service-sdk == Handling job...
8490
== APP - job-service-sdk == Starting droid: R2-D2
8591
== APP - job-service-sdk == Executing maintenance job: Oil Change
8692
```
8793

8894
After 20 seconds, the terminal output should present the `C-3PO` job being processed:
8995

9096
```text
91-
== APP - job-service-sdk == Received job request...
97+
== APP - job-service-sdk == Handling job...
9298
== APP - job-service-sdk == Starting droid: C-3PO
9399
== APP - job-service-sdk == Executing maintenance job: Limb Calibration
94100
```

jobs/csharp/sdk/job-scheduler/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@
5252

5353
async Task ScheduleJob(DroidJob job)
5454
{
55-
Console.WriteLine($"Scheduling job: " + job.Name);
55+
Console.WriteLine($"Scheduling job...");
5656

5757
try
5858
{
5959
var response = await httpClient.PostAsJsonAsync("/scheduleJob", job);
6060
var result = await response.Content.ReadAsStringAsync();
6161

6262
response.EnsureSuccessStatusCode();
63-
Console.WriteLine($"Job scheduled successfully: {job.Name}, {result}");
63+
Console.WriteLine($"Job scheduled: {result}");
6464
}
6565
catch (Exception e)
6666
{
@@ -70,7 +70,7 @@ async Task ScheduleJob(DroidJob job)
7070

7171
async Task GetJobDetails(DroidJob job)
7272
{
73-
Console.WriteLine($"Getting job details: " + job.Name);
73+
Console.WriteLine($"Getting job: " + job.Name);
7474

7575
try
7676
{

jobs/csharp/sdk/job-service/Program.cs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
app.MapPost("/scheduleJob", async (HttpContext context) =>
1818
{
19-
Console.WriteLine("Scheduling job...");
2019
var droidJob = await JsonSerializer.DeserializeAsync<DroidJob>(context.Request.Body);
2120
if (droidJob?.Name is null || droidJob?.Job is null)
2221
{
@@ -25,19 +24,22 @@
2524
return;
2625
}
2726

28-
try {
29-
var jobData = new JobData {
27+
try
28+
{
29+
var jobData = new JobData
30+
{
3031
Droid = droidJob.Name,
3132
Task = droidJob.Job
3233
};
3334

3435
await jobsClient.ScheduleJobWithPayloadAsync(droidJob.Name, DaprJobSchedule.FromDuration(TimeSpan.FromSeconds(droidJob.DueTime)), payload: jobData, repeats: 1); //Schedule cron job that repeats once
35-
Console.WriteLine($"Job Scheduled: {droidJob.Name}");
3636

37+
Console.WriteLine($"Job Scheduled: {droidJob.Name}");
3738
context.Response.StatusCode = 200;
3839
await context.Response.WriteAsJsonAsync(droidJob);
39-
40-
} catch (Exception e) {
40+
}
41+
catch (Exception e)
42+
{
4143
Console.WriteLine($"Error scheduling job: " + e);
4244
}
4345
return;
@@ -46,25 +48,25 @@
4648
app.MapGet("/getJob/{name}", async (HttpContext context) =>
4749
{
4850
var jobName = context.Request.RouteValues["name"]?.ToString();
49-
Console.WriteLine($"Getting job: " + jobName);
50-
51+
Console.WriteLine($"Getting job...");
52+
5153
if (string.IsNullOrEmpty(jobName))
5254
{
5355
context.Response.StatusCode = 400;
5456
await context.Response.WriteAsync("Job name required");
5557
return;
5658
}
5759

58-
try {
59-
// Error here: ---> System.FormatException: String '' was not recognized as a valid DateTime.
60+
try
61+
{
6062
var jobDetails = await jobsClient.GetJobAsync(jobName);
61-
Console.WriteLine($"Job schedule: {jobDetails?.Schedule}");
62-
Console.WriteLine($"Job payload: {jobDetails?.Payload}");
6363

6464
context.Response.StatusCode = 200;
6565
await context.Response.WriteAsJsonAsync(jobDetails);
6666

67-
} catch (Exception e) {
67+
}
68+
catch (Exception e)
69+
{
6870
Console.WriteLine($"Error getting job: " + e);
6971
context.Response.StatusCode = 400;
7072
await context.Response.WriteAsync($"Error getting job");
@@ -76,44 +78,46 @@
7678
{
7779
var jobName = context.Request.RouteValues["name"]?.ToString();
7880
Console.WriteLine($"Deleting job: " + jobName);
79-
81+
8082
if (string.IsNullOrEmpty(jobName))
8183
{
8284
context.Response.StatusCode = 400;
8385
await context.Response.WriteAsync("Job name required");
8486
return;
8587
}
8688

87-
try {
89+
try
90+
{
8891
await jobsClient.DeleteJobAsync(jobName);
8992
Console.WriteLine($"Job deleted: {jobName}");
9093

9194
context.Response.StatusCode = 200;
9295
await context.Response.WriteAsync("Job deleted");
9396

94-
} catch (Exception e) {
97+
}
98+
catch (Exception e)
99+
{
95100
Console.WriteLine($"Error deleting job: " + e);
96101
context.Response.StatusCode = 400;
97102
await context.Response.WriteAsync($"Error deleting job");
98103
}
99104
return;
100105
});
101106

102-
//Job handler route to capture incoming jobs
107+
// Job handler route to capture incoming jobs
103108
app.MapDaprScheduledJobHandler((string jobName, ReadOnlyMemory<byte> jobPayload) =>
104109
{
105-
Console.WriteLine($"Received trigger invocation for job name: {jobName}");
110+
Console.WriteLine("Handling job...");
106111
var deserializedPayload = Encoding.UTF8.GetString(jobPayload.Span);
107112

108113
try
109114
{
110-
if (deserializedPayload is null){
115+
if (deserializedPayload is null)
116+
{
111117
throw new Exception("Payload is null");
112118
}
113119

114120
var jobData = JsonSerializer.Deserialize<JobData>(deserializedPayload);
115-
Console.WriteLine($"Received invocation for the job {jobName} with job data droid {jobData?.Droid}");
116-
117121
if (jobData?.Droid is null || jobData?.Task is null)
118122
{
119123
throw new Exception("Invalid format of job data.");
@@ -122,7 +126,8 @@
122126
// Handling Droid Job from decoded value
123127
Console.WriteLine($"Starting droid: {jobData.Droid}");
124128
Console.WriteLine($"Executing maintenance job: {jobData.Task}");
125-
} catch (Exception ex)
129+
}
130+
catch (Exception ex)
126131
{
127132
Console.WriteLine($"Failed to handle job {jobName}");
128133
Console.Error.WriteLine($"Error handling job: {ex.Message}");
@@ -150,7 +155,7 @@ public class DroidJob
150155

151156
[JsonPropertyName("job")]
152157
public string? Job { get; set; }
153-
158+
154159
[JsonPropertyName("dueTime")]
155160
public int DueTime { get; set; }
156161
}

0 commit comments

Comments
 (0)