public ConsulAdapter(string ApplicationName, int ApplicationPort, string ApplicationIp, int OwnerLimit) { _appname = ApplicationName; _apport = ApplicationPort; _appadress = ApplicationIp; _spath = string.Concat(_appname, "/Semaphore"); // What to do with applications with the same name? _limit = OwnerLimit; try { _client = new Client(); _semaphore = _client.Semaphore(_spath, _limit); } catch (Exception e) { Console.WriteLine("Could not create a client session to consul: {0}", e); Console.ReadLine(); } }
private void GetLock() { Console.WriteLine("Trying to acquire lock"); if (_semaphore != null) try { _semaphore.Destroy(); } catch (SemaphoreInUseException) { } _semaphoreOptions = new SemaphoreOptions(Prefix, Limit) { SessionName = Name + "_Session", SessionTTL = TimeSpan.FromSeconds(10) }; _semaphore = _parent.ConsulClient.Semaphore(_semaphoreOptions); _semaphore.Acquire(_getLockCancellationTokenSource.Token); if (_getLockCancellationTokenSource.IsCancellationRequested) { Console.WriteLine("Cancelling Lock aqcuisition"); } else { Console.WriteLine("Lock acquired, becoming active"); _actionThread = ActionThread(); _actionThread.Start(); } }