// To be called fro within Activation context. // Cannot be awaitable, since after DestroyActivation is done the activation is in Invalid state and cannot await any Task. internal void DeactivateActivationOnIdle(ActivationData data) { bool promptly = false; bool alreadBeingDestroyed = false; lock (data) { if (data.State == ActivationState.Valid) { // Change the ActivationData state here, since we're about to give up the lock. data.PrepareForDeactivation(); // Don't accept any new messages if (!data.IsCurrentlyExecuting) { promptly = true; } else // busy, so destroy later. { data.AddOnInactive(() => DestroyActivationVoid(data)); } } else { alreadBeingDestroyed = true; } } logger.Info(ErrorCode.Catalog_ShutdownActivations_2, "DeactivateActivationOnIdle: {0} {1}.", data.ToString(), promptly ? "promptly" : (alreadBeingDestroyed ? "already being destroyed or invalid" : "later when become idle")); CounterStatistic.FindOrCreate(StatisticNames.CATALOG_ACTIVATION_SHUTDOWN_VIA_DEACTIVATE_ON_IDLE).Increment(); if (promptly) { DestroyActivationVoid(data); // Don't await or Ignore, since we are in this activation context and it may have alraedy been destroyed! } }
private void DeactivateActivationImpl(ActivationData data, StatisticName statisticName) { bool promptly = false; bool alreadBeingDestroyed = false; lock (data) { if (data.State == ActivationState.Valid) { // Change the ActivationData state here, since we're about to give up the lock. data.PrepareForDeactivation(); // Don't accept any new messages ActivationCollector.TryCancelCollection(data); if (!data.IsCurrentlyExecuting) { promptly = true; } else // busy, so destroy later. { data.AddOnInactive(() => DestroyActivationVoid(data)); } } else if (data.State == ActivationState.Create) { throw new InvalidOperationException(String.Format( "Activation {0} has called DeactivateOnIdle from within a constructor, which is not allowed.", data.ToString())); } else if (data.State == ActivationState.Activating) { throw new InvalidOperationException(String.Format( "Activation {0} has called DeactivateOnIdle from within OnActivateAsync, which is not allowed.", data.ToString())); } else { alreadBeingDestroyed = true; } } logger.Info(ErrorCode.Catalog_ShutdownActivations_2, "DeactivateActivationOnIdle: {0} {1}.", data.ToString(), promptly ? "promptly" : (alreadBeingDestroyed ? "already being destroyed or invalid" : "later when become idle")); CounterStatistic.FindOrCreate(statisticName).Increment(); if (promptly) { DestroyActivationVoid(data); // Don't await or Ignore, since we are in this activation context and it may have alraedy been destroyed! } }