Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Runner.cs « Service « Duplicati - github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3922f791077f29535580b101b81e81aea5855327 (plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//  Copyright (C) 2015, The Duplicati Team

//  http://www.duplicati.com, info@duplicati.com
//
//  This library is free software; you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as
//  published by the Free Software Foundation; either version 2.1 of the
//  License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful, but
//  WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Linq;

namespace Duplicati.Service
{
    public class Runner : IDisposable
    {
        private readonly System.Threading.Thread m_thread;
        private volatile bool m_terminate = false;
        private volatile bool m_softstop = false;
        private System.Diagnostics.Process m_process;
        private readonly Action m_onStartedAction;
        private readonly Action m_onStoppedAction;
        private readonly Action<string, bool> m_reportMessage;

        private readonly object m_writelock = new object();
        private readonly string[] m_cmdargs;


        private readonly int WAIT_POLL_TIME = (int)TimeSpan.FromMinutes(15).TotalMilliseconds;

        public Runner(string[] cmdargs, Action onStartedAction = null, Action onStoppedAction = null, Action<string, bool> logMessage = null)
        {
            m_onStartedAction = onStartedAction;
            m_onStoppedAction = onStoppedAction;
            m_reportMessage = logMessage;
            if (m_reportMessage == null)
                m_reportMessage = (x,y) => Console.WriteLine(x);

            m_cmdargs = cmdargs;
            m_thread = new System.Threading.Thread(Run);
            m_thread.IsBackground = true;
            m_thread.Name = "Server Runner";
            m_thread.Start();
        }

        private void Run()
        {
            var path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            var exec = System.IO.Path.Combine(path, "Duplicati.Server.exe");
            var cmdargs = "--ping-pong-keepalive=true";
            if (m_cmdargs != null && m_cmdargs.Length > 0)
                cmdargs = Duplicati.Library.Utility.Utility.WrapAsCommandLine(new string[] { cmdargs }.Concat(m_cmdargs));

            var firstRun = true;
            var startAttempts = 0;

            try
            {
                while (!m_terminate && !m_softstop)
                {
                    if (!System.IO.File.Exists(exec))
                    {
                        m_reportMessage(string.Format("File not found {0}", exec), true);
                        return;
                    }

                    try
                    {
                        if (!firstRun)
                            m_reportMessage(string.Format("Attempting to restart server process: {0}", exec), true);

                        m_reportMessage(string.Format("Starting process {0} with cmd args {1}", exec, cmdargs), false);

                        var pr = new System.Diagnostics.ProcessStartInfo(exec, cmdargs)
                        {
                            UseShellExecute = false,
                            RedirectStandardInput = true,
                            RedirectStandardOutput = true,
                            RedirectStandardError = false,
                            WorkingDirectory = path
                        };

                        if (!m_terminate)
                            m_process = System.Diagnostics.Process.Start(pr);

                        if (firstRun && m_onStartedAction != null)
                        {
                            PingProcess();
                            m_onStartedAction();
                        }
                        firstRun = false;

                        while (!m_process.HasExited)
                        {
                            m_process.WaitForExit(WAIT_POLL_TIME);
                            if (!m_process.HasExited)
                            {
                                if (m_terminate)
                                    m_process.Kill();
                                else
                                    PingProcess();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        m_reportMessage(string.Format("Process has failed with error message: {0}", ex), true);

                        if (firstRun)
                        {
                            startAttempts++;
                            if (startAttempts > 5)
                            {
                                m_reportMessage("Too many startup attempts, giving up", true);
                                m_terminate = true;
                            }
                        }

                        // Throttle restarts
                        if (!m_terminate)
                            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(10));
                    }
                }
            }
            finally
            {
                if (m_onStoppedAction != null)
                    m_onStoppedAction();
            }
        }

        private void PingProcess()
        {
            for(var n = 0; n < 5; n++)
            {
                lock(m_writelock)
                {
                    m_process.StandardInput.WriteLine("ping");
                    m_process.StandardInput.Flush();
                }

                using (var t = m_process.StandardOutput.ReadLineAsync())
                {
                    t.Wait(TimeSpan.FromMinutes(1));

                    if (t.IsCompleted && !t.IsFaulted && !t.IsCanceled)
                        return;
                }
            }

            // Not responding, stop it
            m_process.Kill();
            throw new Exception("Process timed out!");
        }

        public void Wait()
        {
            m_thread.Join();
        }

        public void Stop(bool force = true)
        {
            if (force)
            {
                m_terminate = true;
                var p = m_process;
                if (p != null)
                    p.Kill();
            }
            else
            {
                m_softstop = true;
                lock (m_writelock)
                {
                    if (m_process != null)
                    {
                        m_process.StandardInput.WriteLine("shutdown");
                        m_process.StandardInput.Flush();
                    }
                }
            }
        }

        public void Dispose()
        {
            Stop();
        }
    }
}