public void Dispose() { if (Environment.HasShutdownStarted) { return; } m_disposed = true; foreach (var executionContext in m_activeProcessors.Keys) { try { new Task(state => ((RequestExecutionContext)state).Cancel(null), executionContext).Start(); } catch (Exception e) { if (Environment.HasShutdownStarted) { return; } m_tracer.Exception(e); } } m_parsedRequestCache.Dispose(); if (m_storageDriver != null) { m_storageDriver.Dispose(); } }
public void Dispose() { if (Environment.HasShutdownStarted) { return; } // let WCF start shutdown of network interface first, // so that we don't get any new requests coming in IAsyncResult hostClosing = null; var host = Interlocked.CompareExchange(ref m_serviceHost, null, m_serviceHost); if (host != null) { try { var state = host.State; if (state == CommunicationState.Opened || state == CommunicationState.Opening) { hostClosing = host.BeginClose(TimeSpan.FromSeconds(1), null, null); } } catch (Exception e) { m_tracer.Exception(e); } } // while network shutdown is in progress, // trigger cancellation of already running operations on our singleton service instance var service = Interlocked.CompareExchange(ref m_singletonServiceInstance, null, m_singletonServiceInstance); if (service != null) { service.Dispose(); } // now wait for completion of network shutdown to complete if (hostClosing != null) { try { host.EndClose(hostClosing); } catch (Exception e) { m_tracer.Exception(e); if (e is CommunicationException || e is TimeoutException) { try { host.Abort(); } catch {} } } } }
protected override void OnStart(string[] args) { if (m_hostedInstances != null) { throw new InvalidOperationException("Cannot start more than once"); } m_tracer.Info("Resolving process implementations..."); m_hostedInstances = ObjectFactory.GetAllInstances <IHostedProcess>().ToArray(); if (m_hostedInstances == null || m_hostedInstances.Length == 0) { throw new Exception("Could not resolve any implementations of " + typeof(IHostedProcess).FullName); } m_tracer.Info("Starting process implementations..."); try { foreach (var process in m_hostedInstances) { m_tracer.Info("Starting: " + process.GetType().AssemblyQualifiedName); process.Start(this); } } catch (Exception e) { m_tracer.Exception("Failed to initialize process implementations, will clean up and throw.", e); HandleProcessFailure("Failed during startup"); } m_tracer.Info("Started"); }
public void Start(IHostingService host) { if (host == null) { throw new ArgumentNullException("host"); } m_tracer = host.GetTracer(GetType()); m_host = host; m_instanceName = GetInstanceName(); var tcpBaseAddress = GetTcpBaseAddress(); var httpBaseAddress = GetHealthServiceBaseAddress(); try { PerfCounters.Remove(); PerfCounters.Install(); } catch (Exception e) { m_tracer.Exception(e); throw; } var maxConcurrency = Environment.ProcessorCount * 16; var maxPending = Environment.ProcessorCount * 16; m_singletonServiceInstance = new DataService( m_host.GetTracer(typeof(DataService)), this, tcpBaseAddress.Authority + ", " + m_instanceName, maxConcurrency, null); var serviceHost = CreateServiceHost(m_singletonServiceInstance, new[] { tcpBaseAddress, httpBaseAddress }, maxConcurrency, maxPending); serviceHost.Open(); m_serviceHost = serviceHost; if (m_tracer.IsInfoEnabled) { var builder = new StringBuilder(200); foreach (var ep in serviceHost.ChannelDispatchers) { if (ep.Listener != null) { builder.AppendLine(ep.Listener.Uri.ToString()); } } m_tracer.InfoFormat("Service host [{0}] started. Listening on following base URIs:{1}{2}", m_instanceName, Environment.NewLine, builder); } }
public Message Process(Message message) { if (message == null) { throw new ArgumentNullException("message"); } var pqlMessage = message as PqlMessage; if (pqlMessage == null) { throw new Exception(string.Format("Invalid message type, expected {0}, got {1}", typeof(PqlMessage).AssemblyQualifiedName, message.GetType().AssemblyQualifiedName)); } try { // re-establish authentication context as it is on the client side var authContext = AuthContextSerializer.GetObject(pqlMessage.AuthTicket); PqlEngineSecurityContext.Set(authContext); // get a processing manager from pool, start production var holder = m_requestManagersPool.Take(m_cancellationTokenSource.Token); try { var engine = m_enginesCache.GetEngine(authContext.TenantId, pqlMessage.ScopeId); holder.Item.Attach((PqlMessage)message, engine, authContext); engine.BeginExecution(holder.Item.ExecutionContext); } catch { holder.Item.ExecutionContext.Cancel(null); holder.Dispose(); throw; } // return the message to WCF infrastructure try { holder.Item.ExecutionContext.AttachRequestCompletion(); return(new PqlMessage( holder.Item, new IDisposable[] { holder.Item.ExecutionContext.Completion, holder }, pqlMessage.AuthTicket, pqlMessage.ScopeId, m_protocolVersion)); } catch (Exception e) { holder.Item.ExecutionContext.Cancel(e); holder.Dispose(); throw; } } catch (Exception e) { m_tracer.Exception(e); return(new PqlMessage(new PqlErrorDataWriter(1, e, true), null, pqlMessage.AuthTicket, pqlMessage.ScopeId, m_protocolVersion)); } }
public bool HandleError(Exception error) { m_tracer.Exception(error); return(true); }