-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCronicleJob.cs
80 lines (69 loc) · 1.68 KB
/
CronicleJob.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Text.Json;
using CronicleDotnetSDK.Models;
namespace CronicleDotnetSDK
{
public abstract class CronicleJob
{
private bool _jobStatusSent { get; set; } = false;
private bool _failIfNoJobStatusSent { get; set; }
public CronicleJob(bool debugMode = false, bool failIfNoJobStatusSent = false)
{
_failIfNoJobStatusSent = failIfNoJobStatusSent;
}
private Job? GetJobDescription()
{
string input = Console.ReadLine() ?? string.Empty;
return JsonSerializer.Deserialize<Job>(input);
}
public void SendJobStatus(bool success, int code = 0)
{
JobStatus jobStatus = new JobStatus()
{
Complete = success ? 1 : 0,
Code = code,
};
string output = JsonSerializer.Serialize(jobStatus);
Console.WriteLine(output);
_jobStatusSent = jobStatus.Complete == 1;
}
public void SendTable(string title, List<List<string>> data)
{
Table table = new Table()
{
Title = title,
Header = data[0],
Rows = data.Skip(1).ToList(),
Caption = "This is a caption",
};
TableContainer tableContainer = new TableContainer(table);
string output = JsonSerializer.Serialize(tableContainer);
Console.WriteLine(output);
}
public abstract void JobWork(Job jobDescription);
public void Run()
{
Job? jobDescription = GetJobDescription();
if (jobDescription != null)
{
JobWork(jobDescription);
}
else
{
SendJobStatus(false, code: 1);
}
if (_failIfNoJobStatusSent && !_jobStatusSent)
{
SendJobStatus(false, code: 1);
}
else
{
SendJobStatus(true);
}
}
public void RunDebugMode(Job jobDescription)
{
JobWork(jobDescription);
}
}
}