forked from dotnet/crank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJobsApis.cs
61 lines (48 loc) · 1.8 KB
/
JobsApis.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Crank.Models;
using Microsoft.Extensions.DependencyInjection;
using Repository;
namespace Microsoft.Crank.Agent
{
public class JobsApis
{
private static Job GetJob(HttpContext context)
{
var id = Convert.ToInt32(context.Request.RouteValues["id"]);
var jobRepository = context.RequestServices.GetRequiredService<IJobRepository>();
var job = jobRepository.Find(id);
return job;
}
public static Task GetState(HttpContext context)
{
var job = GetJob(context);
if (job == null)
{
context.Response.StatusCode = 404;
return Task.CompletedTask;
}
// Mark when the job was last read to notify that the driver is still connected
job.LastDriverCommunicationUtc = DateTime.UtcNow;
context.Response.StatusCode = 200;
return context.Response.WriteAsync(job.State.ToString());
}
public static Task GetTouch(HttpContext context)
{
var job = GetJob(context);
if (job == null)
{
context.Response.StatusCode = 404;
return Task.CompletedTask;
}
// Mark when the job was last read to notify that the driver is still connected
job.LastDriverCommunicationUtc = DateTime.UtcNow;
context.Response.StatusCode = 200;
return Task.CompletedTask;
}
}
}