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

HostedInstanceKeeper.cs « Duplicati.GUI.TrayIcon « GUI « Duplicati - github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dc3f88b1ecc919d57a6dc1665914d15bf1df1522 (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Duplicati.GUI.TrayIcon
{    
    /// <summary>
    /// We keep the hosted instance here to allow the TrayIcon executable to load without requiring the Server.exe
    /// </summary>
    internal class HostedInstanceKeeper : IDisposable
    {
        private System.Threading.Thread m_runner;
        private System.Exception m_runnerException = null;
        public event Action InstanceShutdown;

        public HostedInstanceKeeper(string[] args)
        {
            m_runner = new System.Threading.Thread((dummy_arg) => {
                try
                {
                    //When running the hosted instance we do not really care what port we are using,
                    // so we just throw a few out there and try them
                    if (args == null || !args.Any(x => x.Trim().StartsWith("--" + Duplicati.Server.WebServer.Server.OPTION_PORT + "=", StringComparison.InvariantCultureIgnoreCase)))
                        args = (args ?? new string[0]).Union(new string[] { "--" + Duplicati.Server.WebServer.Server.OPTION_PORT + "=8200,8300,8400,8500,8600,8700,8800,8900,8989" }).ToArray();
                        
                    Duplicati.Server.Program.Main(args);
                } catch (Exception ex) {
                    m_runnerException = ex;
                    Duplicati.Server.Program.ServerStartedEvent.Set();
                } finally {
                    if (InstanceShutdown != null)
                        InstanceShutdown();   
                }

            });
            
            m_runner.Start();

            if (!Duplicati.Server.Program.ServerStartedEvent.WaitOne(TimeSpan.FromSeconds(100), true))
            {
                if (m_runnerException != null)
                    throw m_runnerException;
                else
                    throw new Exception("Hosted server startup timed out");
            }

            if (m_runnerException != null)
                throw m_runnerException;
        }

        public void Dispose()
        {
            try
            {
                Duplicati.Server.Program.ApplicationExitEvent.Set();
                if (!m_runner.Join(TimeSpan.FromSeconds(10)))
                {
                    m_runner.Abort();
                    m_runner.Join(TimeSpan.FromSeconds(10));
                }
            }
            catch
            {
            }
        }
    }
}