aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Web/Tango.MachineService/Web.Release.config
blob: 28a4d5fcc327efda9352a8a0995eb323d7010c41 (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
<?xml version="1.0"?>

<!-- For more information on using Web.config transformation visit https://go.microsoft.com/fwlink/?LinkId=301874 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <!--
    In the example below, the "SetAttributes" transform will change the value of
    "connectionString" to use "ReleaseSQLServer" only when the "Match" locator
    finds an attribute "name" that has a value of "MyDB".

    <connectionStrings>
      <add name="MyDB"
        connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    </connectionStrings>
  -->
  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
    <!--
      In the example below, the "Replace" transform will replace the entire
      <customErrors> section of your Web.config file.
      Note that because there is only one customErrors section under the
      <system.web> node, there is no need to use the "xdt:Locator" attribute.

      <customErrors defaultRedirect="GenericError.htm"
        mode="RemoteOnly" xdt:Transform="Replace">
        <error statusCode="500" redirect="InternalError.htm"/>
      </customErrors>
    -->
  </system.web>
</configuration>
#fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Threading;
using Tango.Core;
using Tango.Core.Helpers;

namespace Tango.PPC.Common.WatchDog
{
    public class WatchDogServer : ExtendedObject, IDisposable
    {
        private NamedPipeServerStream _server;
        private StreamReader _reader;
        private StreamWriter _writer;
        private Thread _thread;
        private Dispatcher _dispatcher;
        private String _watchdogAppPath;
        private Process _watchdogProcess;

        public bool IsStarted { get; private set; }

        public WatchDogServer(Dispatcher uiDispatcher)
        {
            _watchdogAppPath = Path.Combine(AssemblyHelper.GetCurrentAssemblyFolder(), "Tango.PPC.WatchDog.exe");

            _dispatcher = uiDispatcher;
        }

        public void Start()
        {
            LogManager.Log("Starting watchdog server...");

            try
            {
                _watchdogProcess = Process.GetProcessesByName("Tango.PPC.WatchDog").FirstOrDefault();

                _server = new NamedPipeServerStream("Tango_Watch_Dog_Pipe");
                _reader = new StreamReader(_server);
                _writer = new StreamWriter(_server);

                IsStarted = true;
                _thread = new Thread(ThreadMethod);
                _thread.IsBackground = true;
                _thread.Start();

                LogManager.Log("Watchdog server started.");
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "Error starting watchdog server!");
            }
        }

        private void ThreadMethod()
        {
            IsStarted = true;

            try
            {
                while (IsStarted)
                {
                    if (_watchdogProcess == null)
                    {
                        LogManager.Log($"Starting watchdog process at '{_watchdogAppPath}'...");

                        try
                        {
                            _watchdogProcess = Process.Start(_watchdogAppPath);
                        }
                        catch (Exception ex)
                        {
                            LogManager.Log(ex, "Error starting watchdog process!");
                            return;
                        }
                    }

                    _server.WaitForConnection();

                    var line = _reader.ReadLine();

                    _dispatcher.Invoke(() =>
                    {
                        _writer.WriteLine(line);
                        _writer.Flush();
                    });

                    _server.Disconnect();
                }
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "Error in watchdog server. Restarting watchdog...");
                IsStarted = false;

                try
                {
                    _server.Dispose();
                }
                catch { }

                Start();
                return;
            }
        }

        public void Stop()
        {
            if (IsStarted)
            {
                try
                {
                    _watchdogProcess.Kill();
                    _watchdogProcess = null;
                }
                catch (Exception ex)
                {
                    LogManager.Log(ex, "Error killing watchdog process!");
                }
                IsStarted = false;
                _server.Dispose();
            }
        }

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