Skip to content
This repository has been archived by the owner on Aug 19, 2018. It is now read-only.

Commit

Permalink
Added the PID file process to all servers and added --pidfile
Browse files Browse the repository at this point in the history
Only the user server was set up to create PID files for some reason.  Now all servers create PID files, and the file they should create can be set via the --pidfile commandline flag, but will default to the pre-existing style file naming if that is not given.

The various server startup processes were diverging badly, brought them back into lockstep so they can be maintained correctly.
  • Loading branch information
kf6kjg committed May 12, 2017
1 parent ce76b1e commit fae1793
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 34 deletions.
8 changes: 7 additions & 1 deletion InWorldz/Halcyon/Application.cs
Expand Up @@ -72,8 +72,12 @@ public static void Main(string[] args)
ServicePointManager.DefaultConnectionLimit = 12;

// Add the arguments supplied when running the application to the configuration
ArgvConfigSource configSource = new ArgvConfigSource(args);
var configSource = new ArgvConfigSource(args);

configSource.AddSwitch("Startup", "pidfile");
var pidFile = new PIDFileManager(configSource.Configs["Startup"].GetString("pidfile", string.Empty));

pidFile.SetStatus(PIDFileManager.Status.Starting);
// Configure Log4Net
configSource.AddSwitch("Startup", "logconfig");
string logConfigFile = configSource.Configs["Startup"].GetString("logconfig", String.Empty);
Expand Down Expand Up @@ -136,6 +140,7 @@ public static void Main(string[] args)
if (background)
{
m_sim = new OpenSimBackground(configSource);
pidFile.SetStatus(PIDFileManager.Status.Running);
m_sim.Startup();
}
else
Expand All @@ -144,6 +149,7 @@ public static void Main(string[] args)

m_sim.Startup();

pidFile.SetStatus(PIDFileManager.Status.Running);
while (true)
{
try
Expand Down
29 changes: 17 additions & 12 deletions OpenSim/Framework/PIDFileManager.cs
Expand Up @@ -28,17 +28,14 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace OpenSim.Framework
{
/// <summary>
/// Creates a file with this processes PID and status as processname.pid
/// Creates a file with this processes PID and status as the given pidfile or, if null, processname.pid
/// </summary>
public class PIDFileManager
{
Expand All @@ -49,21 +46,29 @@ public enum Status
Running
}

public PIDFileManager()
public string PidFile { get; private set; }

public PIDFileManager(string pidFile)
{
var thisProcess = Process.GetCurrentProcess();

PidFile = pidFile;
if (string.IsNullOrWhiteSpace(pidFile))
{
PidFile = $"{Path.GetFileName(thisProcess.MainModule.FileName)}.pid";
}

this.SetStatus(Status.Init);
}

public void SetStatus(Status status)
{
Process thisProcess = Process.GetCurrentProcess();
string pidfileName = Path.GetFileName(thisProcess.MainModule.FileName) + ".pid";
int pid = Process.GetCurrentProcess().Id;
var pid = Process.GetCurrentProcess().Id;

using (FileStream pidFile = File.OpenWrite(pidfileName))
using (FileStream pidFile = File.OpenWrite(PidFile))
{
string pidInfo = ((int)status).ToString() + " " + pid.ToString();
byte[] utf8bytes = System.Text.Encoding.UTF8.GetBytes(pidInfo);
var pidInfo = $"{((int)status)} {pid}";
var utf8bytes = Encoding.UTF8.GetBytes(pidInfo);

pidFile.Write(utf8bytes, 0, utf8bytes.Length);
}
Expand Down
32 changes: 20 additions & 12 deletions OpenSim/Grid/GridServer/Program.cs
Expand Up @@ -29,6 +29,7 @@
using log4net.Config;
using log4net;
using Nini.Config;
using OpenSim.Framework;

namespace OpenSim.Grid.GridServer
{
Expand All @@ -38,14 +39,11 @@ public static class Program

public static void Main(string[] args)
{
// Please note that if you are changing something in this function you should check to see if you need to change the other server's Main functions as well.
ServicePointManager.DefaultConnectionLimit = 12;

m_log.Info("starting up");

XmlConfigurator.Configure();

// Add the arguments supplied when running the application to the configuration
ArgvConfigSource configSource = new ArgvConfigSource(args);
var configSource = new ArgvConfigSource(args);

configSource.Alias.AddAlias("On", true);
configSource.Alias.AddAlias("Off", false);
Expand All @@ -55,22 +53,32 @@ public static void Main(string[] args)
configSource.Alias.AddAlias("No", false);

configSource.AddSwitch("Startup", "background");
configSource.AddSwitch("Startup", "pidfile");

m_log.Info("[SERVER]: Launching GridServer...");

var pidFile = new PIDFileManager(configSource.Configs["Startup"].GetString("pidfile", string.Empty));
XmlConfigurator.Configure();

bool background = configSource.Configs["Startup"].GetBoolean("background", false);

GridServerBase app;
if (background)
{
m_log.Info("[GridServer MAIN]: set to background");
GridServerBackground app = new GridServerBackground();
app.Startup();
app.Work();
app = new GridServerBackground();
}
else {
else
{
m_log.Info("[GridServer MAIN]: set to foreground");
GridServerBase app = new GridServerBase();
app.Startup();
app.Work();
app = new GridServerBase();
}

pidFile.SetStatus(PIDFileManager.Status.Starting);
app.Startup();

pidFile.SetStatus(PIDFileManager.Status.Running);
app.Work();
}
}
}
16 changes: 13 additions & 3 deletions OpenSim/Grid/MessagingServer/Main.cs
Expand Up @@ -69,24 +69,33 @@ public class OpenMessage_Main : BaseOpenSimServer, IGridServiceCore

public static void Main(string[] args)
{
// Please note that if you are changing something in this function you should check to see if you need to change the other server's Main functions as well.
ServicePointManager.DefaultConnectionLimit = 12;

XmlConfigurator.Configure();
// Add the arguments supplied when running the application to the configuration
var configSource = new ArgvConfigSource(args);

ArgvConfigSource configSource = new ArgvConfigSource(args);
configSource.Alias.AddAlias("On", true);
configSource.Alias.AddAlias("Off", false);
configSource.Alias.AddAlias("True", true);
configSource.Alias.AddAlias("False", false);
configSource.Alias.AddAlias("Yes", true);
configSource.Alias.AddAlias("No", false);

configSource.AddSwitch("Startup", "background");
configSource.AddSwitch("Startup", "pidfile");

m_log.Info("[SERVER]: Launching MessagingServer...");

var pidFile = new PIDFileManager(configSource.Configs["Startup"].GetString("pidfile", string.Empty));
XmlConfigurator.Configure();

OpenMessage_Main messageserver = new OpenMessage_Main();

pidFile.SetStatus(PIDFileManager.Status.Starting);
messageserver.Startup();

pidFile.SetStatus(PIDFileManager.Status.Running);
messageserver.Work(configSource.Configs["Startup"].GetBoolean("background", false));
}

Expand All @@ -103,7 +112,8 @@ private void Work(bool background)
Terminating.WaitOne();
Terminating.Close();
}
else {
else
{
m_console.Notice("Enter help for a list of commands\n");

while (true)
Expand Down
18 changes: 12 additions & 6 deletions OpenSim/Grid/UserServer/Main.cs
Expand Up @@ -87,23 +87,28 @@ public class OpenUser_Main : BaseOpenSimServer, IGridServiceCore

public static void Main(string[] args)
{
// Please note that if you are changing something in this function you should check to see if you need to change the other server's Main functions as well.
ServicePointManager.DefaultConnectionLimit = 12;

var pidFile = new PIDFileManager();
XmlConfigurator.Configure();
// Add the arguments supplied when running the application to the configuration
var configSource = new ArgvConfigSource(args);

ArgvConfigSource configSource = new ArgvConfigSource(args);
configSource.Alias.AddAlias("On", true);
configSource.Alias.AddAlias("Off", false);
configSource.Alias.AddAlias("True", true);
configSource.Alias.AddAlias("False", false);
configSource.Alias.AddAlias("Yes", true);
configSource.Alias.AddAlias("No", false);

configSource.AddSwitch("Startup", "background");
configSource.AddSwitch("Startup", "pidfile");

m_log.Info("Launching UserServer...");
m_log.Info("[SERVER]: Launching UserServer...");

OpenUser_Main userserver = new OpenUser_Main();
var pidFile = new PIDFileManager(configSource.Configs["Startup"].GetString("pidfile", string.Empty));
XmlConfigurator.Configure();

var userserver = new OpenUser_Main();

pidFile.SetStatus(PIDFileManager.Status.Starting);
userserver.Startup();
Expand All @@ -125,7 +130,8 @@ public void Work(bool background)
Terminating.WaitOne();
Terminating.Close();
}
else {
else
{
m_console.Notice("Enter help for a list of commands\n");

while (true)
Expand Down

0 comments on commit fae1793

Please sign in to comment.