Skip to content

Commit

Permalink
Add Watchog to try to recover from outages
Browse files Browse the repository at this point in the history
  • Loading branch information
evandixon committed Apr 30, 2018
1 parent ecdd275 commit 7a5de35
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,7 @@ __pycache__/
*.xsd.cs
/Ditto/discord.json
/Ditto/irc.json
/Ditto/projectpokemon.irc.json
/Ditto/projectpokemon.discord.json
/Ditto/pp-staff.irc.json
/Ditto/pp-staff.discord.json
13 changes: 13 additions & 0 deletions Ditto.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ditto", "Ditto\Ditto.csproj
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IrcDotNet", "IrcDotNet\IrcDotNet.csproj", "{5A8EF7A9-4DFB-4DA6-A73B-C0028E26DBFA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Watchog", "Watchog\Watchog.csproj", "{4E71E8AA-FA91-4D78-A770-2E080E2D8263}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{0E2536C6-C76A-46E2-99EA-2398F2437739}"
ProjectSection(SolutionItems) = preProject
README.MD = README.MD
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -26,6 +33,12 @@ Global
{5A8EF7A9-4DFB-4DA6-A73B-C0028E26DBFA}.Debug-IPv4|Any CPU.Build.0 = Debug-IPv4|Any CPU
{5A8EF7A9-4DFB-4DA6-A73B-C0028E26DBFA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5A8EF7A9-4DFB-4DA6-A73B-C0028E26DBFA}.Release|Any CPU.Build.0 = Release|Any CPU
{4E71E8AA-FA91-4D78-A770-2E080E2D8263}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4E71E8AA-FA91-4D78-A770-2E080E2D8263}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4E71E8AA-FA91-4D78-A770-2E080E2D8263}.Debug-IPv4|Any CPU.ActiveCfg = Debug|Any CPU
{4E71E8AA-FA91-4D78-A770-2E080E2D8263}.Debug-IPv4|Any CPU.Build.0 = Debug|Any CPU
{4E71E8AA-FA91-4D78-A770-2E080E2D8263}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4E71E8AA-FA91-4D78-A770-2E080E2D8263}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion Ditto/startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ else
echo -n "" > $pidFile
cd ${basePath}
echo "Starting Ditto"
nohup ./Ditto noprompt >/dev/null 2>&1 &
nohup ./Watchog Ditto noprompt >>WatchogExecution.log 2>&1 &
echo -n "$! " >> $pidFile
fi
13 changes: 13 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Ditto
.Net Core application to mirror messages between IRC and Discord.

## Projects

* Ditto - the main application
* IrcDotNet - Slightly customized version of [IrcDotNet](https://github.com/IrcDotNet/IrcDotNet).
* Watchog - Starts Ditto and watches to make sure it's online, restarting it if necessary. This is needed because on some systems, Ditto dies so hard its error handling doesn't kick in, whenever there's a Discord outage for any length.

## Usage
1. Create the json settings files. Instructions coming eventually.
2. Run startup.sh
3. Run shutdown.sh to shutdown
71 changes: 71 additions & 0 deletions Watchog/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Diagnostics;
using System.IO;

namespace Watchog
{
class Program
{
static void Main(string[] args)
{
try
{
if (args.Length < 2)
{
Console.WriteLine("Usage: Watchog <filename> [args]");
return;
}

Console.WriteLine($"[{DateTime.Now.ToString()}] Watchog started.");

var p = new Process();

Console.CancelKeyPress += delegate
{
Console.WriteLine($"[{DateTime.Now.ToString()}] Received Control+C. Closing program.");
p.Close();
if (!p.HasExited)
{
Console.WriteLine($"[{DateTime.Now.ToString()}] Program not running. Waiting for up to 10 seconds.");
p.WaitForExit(10 * 1000);
if (!p.HasExited)
{
p.Kill();
Console.WriteLine($"[{DateTime.Now.ToString()}] Killed program.");
}
else
{
Console.WriteLine($"[{DateTime.Now.ToString()}] Program exited on its own. Program not killed.");
}
}
Console.WriteLine($"[{DateTime.Now.ToString()}] Watchog exiting.");
};

if (File.Exists(args[0]))
{
while (true)
{
p = new Process();
p.StartInfo.FileName = args[0];
p.StartInfo.Arguments = args[1];
p.WaitForExit();
p.Dispose();
p = null;

// Program exited, and Watchog should restart
Console.WriteLine($"[{DateTime.Now.ToString()}] Program exited. Restarting...");
}
}
else
{
throw new FileNotFoundException("Can't find the file on disk", args[0]);
}
}
catch (Exception ex)
{
Console.WriteLine($"[{DateTime.Now.ToString()}] Watchog encountered an exception: {ex.ToString()}");
throw;
}
}
}
}
16 changes: 16 additions & 0 deletions Watchog/Watchog.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath>..\Ditto\bin\Debug\netcoreapp2.0\</OutputPath>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<OutputPath>..\Ditto\bin\Release\netcoreapp2.0\</OutputPath>
</PropertyGroup>

</Project>

0 comments on commit 7a5de35

Please sign in to comment.