-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPresets.cs
134 lines (129 loc) · 4.25 KB
/
Presets.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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Common.Logging;
namespace TranscodeProcessor
{
/// <summary>
/// Container class for trancoding presets
/// Presets are saved between restarts in a file calls presets.json
/// </summary>
public class Presets
{
protected ILog log = LogManager.GetCurrentClassLogger();
protected ConcurrentDictionary<string, string> presets;
protected Task presetSaveTask;
protected CancellationTokenSource cancelToken;
protected bool saving = false;
public Presets()
{
presets = new ConcurrentDictionary<string,string>();
// Check if we have an existing preset file
if (File.Exists("presets.json"))
{
FastSerialize.Serializer serializer = new FastSerialize.Serializer(typeof(FastSerialize.JsonSerializerGeneric));
StreamReader sr = new StreamReader(File.Open("presets.json", FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
string data = sr.ReadToEnd();
sr.Close();
Dictionary<string, string> saved = serializer.Deserialize<Dictionary<string, string>>(data);
foreach (var val in saved)
{
presets.TryAdd(val.Key, val.Value);
}
}
cancelToken = new CancellationTokenSource();
// Start a background task to save the preset file on a regular basis
presetSaveTask = TaskExecutorFactory.Begin(SaveAll, 5000, 100,Timeout.Infinite,-1,cancelToken.Token);
}
protected void SaveAll()
{
try {
saving = true;
FastSerialize.Serializer serializer = new FastSerialize.Serializer(typeof(FastSerialize.JsonSerializerGeneric));
var data = serializer.Serialize(presets);
StreamWriter sw = new StreamWriter(File.Open("presets.json", FileMode.Create, FileAccess.Write, FileShare.Read));
using (sw)
{
sw.Write(data);
}
saving = false;
}
catch (OperationCanceledException)
{
log.Debug("Shutting down presets");
}
catch (ThreadInterruptedException)
{
log.Debug("Shutting down presets");
}
catch (ThreadAbortException)
{
log.Debug("Shutting down presets");
}
catch (Exception e)
{
log.Error("Exception in prests file saving occured", e);
}
finally
{
saving = false;
}
}
public void Shutdown()
{
try
{
// Attempt to gracefully shutdown
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
{
}
}
public void Add(string presetName, string preset)
{
presets.AddOrUpdate(presetName, preset, (key, oldValue) => preset);
}
public void Remove(string presetName)
{
string existing;
presets.TryRemove(presetName, out existing);
}
public string Get(string presetName)
{
string existing;
if (presets.TryGetValue(presetName, out existing))
{
return existing;
}
return null;
}
public IDictionary<string,string> GetAll()
{
return presets;
}
}
}