-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTranscoderService.cs
148 lines (135 loc) · 4.27 KB
/
TranscoderService.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
//#define DEBUG
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using System.Configuration;
using System.Reflection;
using System.Collections.Specialized;
using Common.Logging;
namespace TranscodeProcessor
{
/// <summary>
/// Service container for the application
/// </summary>
public class TranscoderService : ServiceBase
{
private static ILog log = LogManager.GetCurrentClassLogger();
public static void Main(string[] args)
{
Console.WriteLine("Running Transcoder");
if (args.Length == 0)
{
Console.WriteLine("Missing argument");
Console.ReadLine();
return;
}
try
{
#if DEBUG
var service = new TranscoderService();
service.OnStart(args);
#else
ServiceBase.Run(new TranscoderService());
#endif
}
catch (Exception ex)
{
log.Fatal("Failed to start process", ex);
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
if (ex.InnerException != null)
{
Console.WriteLine(ex.InnerException.Message);
Console.WriteLine(ex.InnerException.StackTrace);
}
//throw ex;
}
// System.Diagnostics.Process.GetCurrentProcess().Exited += new EventHandler(TP.TranscodeProcessor_Exited);
#if DEBUG
Console.ReadLine();
#endif
}
public TranscoderService()
{
}
private bool stopped = false;
protected AppDomain appDomain;
protected FFRest proxy;
protected override void OnStart(String[] args)
{
try
{
Console.WriteLine("OnStart");
base.OnStart(args);
// get the name of the assembly
string exeAssembly = Assembly.GetEntryAssembly().FullName;
// setup - there you put the path to the config file
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = System.Environment.CurrentDirectory;
setup.ConfigurationFile = args[0];
// create the app domain
appDomain = AppDomain.CreateDomain("Transcoder", null, setup);
// create proxy used to call the startup method
Console.WriteLine("Initializing");
proxy = (FFRest)appDomain.CreateInstanceAndUnwrap(
exeAssembly, typeof(FFRest).FullName);
Console.WriteLine("Initialized");
// call the startup method - something like alternative main()
proxy.Begin();
}
catch (Exception ex)
{
log.Fatal("A fatal startup exception has occured", ex);
}
}
protected override void OnStop()
{
log.Debug("OnStop Called");
if (!stopped)
{
stopped = true;
Console.WriteLine("OnStop");
proxy.Stop();
AppDomain.Unload(appDomain);
}
base.OnStop();
}
protected override void OnShutdown()
{
log.Debug("OnShutdown Called");
if (!stopped)
{
stopped = true;
proxy.Stop();
AppDomain.Unload(appDomain);
}
base.OnShutdown();
}
new public void Dispose()
{
log.Debug("Dispose Called");
if (!stopped)
{
stopped = true;
proxy.Stop();
AppDomain.Unload(appDomain);
}
base.OnStop();
base.Dispose();
}
protected override void Dispose(bool disposing)
{
log.Debug("Disposing Called");
if (!stopped)
{
stopped = true;
proxy.Stop();
AppDomain.Unload(appDomain);
}
base.OnStop();
base.Dispose(disposing);
}
}
}