我有两个运行相同 Windows 服务的实例。他们检查彼此的健康状况并报告是否发现任何问题。我有一项需要执行的关键工作,所以我使用故障转移方法运行它,它在主服务器中运行,如果主服务器没有响应,它在从服务器中运行。这项工作需要通过特定的串行端口进行通信,我正在尝试使用 Mutex 来检查竞争条件。我无法访问生产,所以在部署之前我想确保我的方法是好的。所以请建议我使用 Mutex 是否适合给定的情况。
if (iAmRunningInSlave)
{
HealthClient hc = new HealthClient();
if (!hc.CheckHealthOfMaster())
return this.runJobWrapper(withMutex, iAmRunningInSlave);
else
return true; //master is ok, we dont need to run the job in slave
}
return this.runJobWrapper(withMutex, iAmRunningInSlave);
然后在 runJobWrapper
bool runJobWrapper(bool withMutex, bool iAmRunningInSlave)
{
if (!withMutex)
return this.runJob(iAmRunningInSlave); //the job might be interested to know
Mutex mutex = null;
string mutexName = this.jobCategory + "-" + this.jobTitle; //this will be unique for given job
try
{
mutex = Mutex.OpenExisting(mutexName);
return false; //mutex is with peer, return false which will re-trigger slave
}
catch
{
try
{ //mean time mutex might have created, so wrapping in try/catch
mutex = new Mutex(true /*initiallyOwned*/, mutexName);
return this.runJob(iAmRunningInSlave); //the job might be interested to know where I am running
}
finally
{
if (null!=mutex) mutex.ReleaseMutex();
}
return false;
}
}