public TestForm() { InitializeComponent(); _beanstalkdClient = ManagedBeanstalkdClientFactory.Create("192.168.1.254"); _beanstalkdClient.Watch(TestTube); _beanstalkdClient.Ignore("default"); Task.Factory.StartNew(PullJobProcedure); }
private IMessage Invoke(IMessage msg, bool retry) { try { if (_client == null || _client.Disposed) { if (_client != null) { Log.Info("Client disconnected, trying to reconnect and restore current state."); } _client = new BeanstalkdClient(_host, _port); _watchList.ForEach(tube => _client.Watch(tube)); if (!_watchList.Contains("default")) { _client.Ignore("default"); } if (_currentTube != "default") { _client.Use(_currentTube); } } var methodCall = (IMethodCallMessage)msg; var method = (MethodInfo)methodCall.MethodBase; var args = methodCall.Args; var result = typeof(BeanstalkdClient).InvokeMember(methodCall.MethodName, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, _client, args); if (method.Name == "Use" && null != result as string) { _currentTube = result as string; } if (method.Name == "Watch" && methodCall.ArgCount == 1 && result is uint && !_watchList.Contains((string)methodCall.InArgs[0])) { _watchList.Add((string)methodCall.InArgs[0]); } if (method.Name == "Ignore" && methodCall.ArgCount == 1 && result is bool && _watchList.Contains((string)methodCall.InArgs[0])) { _watchList.Remove((string)methodCall.InArgs[0]); } return(new ReturnMessage(result, args, args.Length, methodCall.LogicalCallContext, methodCall)); } catch (Exception e) { if (!(e is TargetInvocationException) || e.InnerException == null) { return(new ReturnMessage(e, msg as IMethodCallMessage)); } var ex = e.InnerException as BeanstalkdException; if (retry && ex != null && ex.Code == BeanstalkdExceptionCode.ConnectionError) { if (_client != null && !_client.Disposed) { _client.Dispose(); } return(Invoke(msg, false)); } return(new ReturnMessage(e.InnerException, msg as IMethodCallMessage)); } }