我需要正常关闭在 RoleEntryPoint.OnStop()
方法中使用 System.Diagnostics.Process
启动的 mongod.exe。
我受到一篇文章的启发Running MongoDb on Microsoft Windows Azure with CloudDrive 。 一切似乎都工作正常,但是在 WorkerRole 重新启动 mongod 后说:
**************
old lock file: .\mongod.lock. probably means unclean shutdown
recommend removing file and running --repair
see: http://dochub.mongodb.org/core/repair for more information
*************
因此,我创建了简单的控制台应用程序,代码如下,并在 mongod.exe 被杀死时模拟相同的结果。仅当控制台窗口(父进程)关闭时,锁定文件才会被释放。由于 CloudDrive 的卸载早于父进程终止 (RoleEntryPoint
),mongod.lock 文件在 Windows Azure WorkerRole 环境中永远不会被释放。
static void Main(string[] args)
{
StartMongo();
Console.ReadLine();
_mongoProcess.Close();
}
private static void StartMongo()
{
_mongoProcess = new Process();
var startInfo = _mongoProcess.StartInfo;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = false;
startInfo.FileName = @"mongod.exe";
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.Arguments = "--dbpath .";
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
_mongoProcess.ErrorDataReceived += (sender, evt) => WriteLine(evt.Data);
_mongoProcess.OutputDataReceived += (sender, evt) => WriteLine(evt.Data);
_mongoProcess.Start();
_mongoProcess.BeginErrorReadLine();
_mongoProcess.BeginOutputReadLine();
}
我如何意识到父进程正在保持锁定?我只是将进程更改为在新的 shell 窗口中运行,其中没有重定向输出 (startInfo.UseShellExecute = true
)。两个控制台窗口启动,当 mongod 关闭时,它在主应用程序终止之前释放锁定。我需要实现此行为才能在 Windows Azure 中的 RoleEntryPoint
中使用它。
有谁知道怎么做吗?
编辑:
我意识到,也许是父进程,具有 ErrorDataReceived
和 OutputDataReceived
的监听器,可以正确关闭/刷新 mongod 输出流到 mongod.lock 。 ..可以吗?
最佳答案
在 OnStop
方法中,您可以 invoke the shutdown command 。你可以做类似的事情
var server = MongoServer.Create("mongodb://host:port");
server.Shutdown();
如果您使用官方 1.0 驱动程序,即使关闭服务器已关闭,关闭命令也会挂起。尽管挂起,Azure 仍会回收此角色实例,因为 OnStop 中只有大约 30 秒的时间。该bug已在GitHub最新版驱动中修复https://github.com/mongodb/mongo-csharp-driver 。
此外use mongodb 1.8.1 with journaling enabled 。那时你就不需要修理了。如果由于某种原因 Azure 在关闭完成之前回收角色实例且不干净,则需要执行此操作。有关日记的更多信息可以在 http://www.mongodb.org/display/DOCS/Journaling 找到。
关于c# - 如何在 `RoleEntryPoint.OnStop()` 方法中优雅地关闭 mongod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6312936/