-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathStats.cs
170 lines (159 loc) · 4.95 KB
/
Stats.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Common.Logging;
namespace TranscodeProcessor
{
/// <summary>
/// Keep track of transcoding job stats
/// TODO Expand this to keep more information like total hours transcoded
/// average bit rates, etc
/// </summary>
public class Stats : IHttpRequestHandler
{
private ILog log = LogManager.GetCurrentClassLogger();
public int totalJobs;
public int totalFilesTranscoded;
public int runningJobs;
public int failedJobs;
public bool saving;
CancellationTokenSource cancelToken;
public Stats()
{
log.Debug("Initializing stats service");
StreamReader sr = null;
try
{
sr = new StreamReader(FFRest.config["statsfile"]);
if (!int.TryParse(sr.ReadLine(), out totalJobs))
{
totalJobs = 0;
}
if (!int.TryParse(sr.ReadLine(), out totalFilesTranscoded))
{
totalFilesTranscoded = 0;
}
}
catch (Exception e)
{
log.Error("Exception in file loading occured", e);
totalJobs = 0;
totalFilesTranscoded = 0;
}
finally
{
if (sr != null)
sr.Close();
}
runningJobs = 0;
cancelToken = new CancellationTokenSource();
TaskExecutorFactory.Begin(SaveFile, 100000, 100, Timeout.Infinite, -1, cancelToken.Token);
}
private void SaveFile()
{
try
{
saving = true;
log.Trace("Running Stats Save");
if (File.Exists(FFRest.config["statsfile"]))
{
File.Delete(FFRest.config["statsfile"]);
}
StreamWriter sw = new StreamWriter(FFRest.config["statsfile"]);
sw.WriteLine(totalJobs);
sw.WriteLine(totalFilesTranscoded);
sw.Close();
saving = false;
}
catch (OperationCanceledException)
{
log.Debug("Shutting down stats");
}
catch (ThreadInterruptedException)
{
log.Debug("Shutting down stats");
}
catch (ThreadAbortException)
{
log.Debug("Shutting down stats");
}
catch (Exception e)
{
log.Error("Exception in file saving occured", e);
}
finally
{
saving = false;
}
}
public void HandleGet(HttpListenerRequest request, HttpListenerResponse response)
{
try
{
StringBuilder bld = new StringBuilder();
bld.Append("Total Jobs: " + totalJobs + "\n");
bld.Append("Total Files Transcoded: " + totalFilesTranscoded + "\n");
bld.Append("Active: " + runningJobs + "\n");
response.StatusCode = 200;
response.WriteResponse(bld.ToString());
}
catch (Exception ex)
{
log.Error("Stats Request Exception Occured", ex);
}
// return new Httpd.HttpResponse(200,bld.ToString());
}
public void HandlePost(HttpListenerRequest request, HttpListenerResponse response, Server.RequestData data)
{
response.StatusCode = 400;
response.WriteResponse("Bad Request");
//return new Httpd.HttpResponse(400, "Bad Request");
}
public void AddJob()
{
Interlocked.Increment(ref totalJobs);
}
public void AddTask()
{
Interlocked.Increment(ref totalFilesTranscoded);
Interlocked.Increment(ref runningJobs);
}
public void CompleteTask()
{
Interlocked.Decrement(ref runningJobs);
}
public void Shutdown()
{
try
{
cancelToken.Cancel(true);
int cnt = 0;
while (saving == true && cnt < 20)
{
Thread.Sleep(15);
cnt++;
}
}
catch (ThreadInterruptedException)
{
log.Debug("Shutting down stats");
}
catch (ThreadAbortException)
{
log.Debug("Shutting down starts");
}
catch (Exception e)
{
log.Error("Exception in data stream occured", e);
}
finally
{
}
}
}
}