/// <summary>
        /// Sets the next poll time for the poll schedule.
        /// </summary>
        /// <param name="schedule">The schedule.</param>
        /// <param name="state">The current state</param>
        /// <param name="pollAttempts">The current poll attempts.</param>
        public override void SetNextPollTime(Schedule schedule, MasterJobState state, int pollAttempts)
        {
            switch (state)
            {
            case MasterJobState.Active:
                schedule.Interval = TimeSpan.FromMilliseconds(5 + Generator.Next(10));
                break;

            case MasterJobState.Inactive:
            case MasterJobState.VerifyingComms:
            case MasterJobState.Starting:
                schedule.Interval    = TimeSpan.FromMilliseconds(300);
                schedule.UTCPollTime = DateTime.UtcNow.AddMilliseconds(300);
                break;

            default:
                if (this.PollAttemptsExceeded(state, pollAttempts))
                {
                    schedule.Interval = TimeSpan.FromMilliseconds(5 + Generator.Next(25));
                }
                else
                {
                    schedule.Interval = TimeSpan.FromMilliseconds(25 + Generator.Next(50));
                }
                break;
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="MasterJobCommunicationEventArgs"/> class.
 /// </summary>
 /// <param name="direction">The direction.</param>
 /// <param name="state">The state.</param>
 /// <param name="action">The action.</param>
 /// <param name="iteration">The current iteration.</param>
 /// <param name="originatorId">The originator identifier.</param>
 public MasterJobCommunicationEventArgs(MasterJobCommunicationDirection direction
                                        , MasterJobState state, string action, long iteration, string originatorId = null)
     : base(iteration)
 {
     State        = state;
     Action       = action;
     Direction    = direction;
     OriginatorId = originatorId;
 }
 /// <summary>
 /// This method sends a notification message if the service is active, otherwise it sets the service to inactive.
 /// </summary>
 /// <param name="state">The state.</param>
 protected async Task InformOrResetState(MasterJobState state)
 {
     if (mMasterJobContext.State == MasterJobState.Active)
     {
         await MasterJobSyncIAmMaster();
     }
     else if (mMasterJobContext.State <= state)
     {
         mMasterJobContext.State = MasterJobState.Inactive;
     }
 }
        /// <summary>
        /// Returns true if the maximum poll attempts have been exceeded. The default is 3.
        /// </summary>
        /// <param name="state">The current state.</param>
        /// <param name="currentMasterPollAttempts">The current master poll attempts.</param>
        /// <returns>True if exceeded.</returns>
        public virtual bool PollAttemptsExceeded(MasterJobState state, int currentMasterPollAttempts)
        {
            switch (state)
            {
            case MasterJobState.Active:
            case MasterJobState.TakingControl:
            case MasterJobState.Requesting2:
                return(currentMasterPollAttempts >= 1);

            case MasterJobState.Requesting1:
                return(currentMasterPollAttempts >= 2);

            default:
                return(currentMasterPollAttempts >= 3);
            }
        }
Example #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MasterJobStatusChangeEventArgs"/> class.
 /// </summary>
 /// <param name="state">The current state.</param>
 /// <param name="iteration">The current iteration.</param>
 public MasterJobStatusChangeEventArgs(MasterJobState state, long iteration) : base(iteration)
 {
     State = state;
 }
Example #6
0
        /// <summary>
        /// This is the master job negotiation logic.
        /// </summary>
        /// <param name="schedule">The current schedule object.</param>
        /// <param name="token">The cancellation token</param>
        protected virtual async Task MasterJobStateNotificationOutgoing(Schedule schedule, CancellationToken token)
        {
            mMasterJobLastPollTime = DateTime.UtcNow;
            //We set a random poll time to reduce the potential for deadlocking
            //and to make the negotiation messaging more efficient.
            schedule.Frequency = TimeSpan.FromSeconds(5 + mRandom.Next(10));

            switch (State)
            {
            case MasterJobState.VerifyingComms:
                await NegotiationTransmit(MasterJobStates.WhoIsMaster);

                break;

            case MasterJobState.Starting:
                await NegotiationTransmit(MasterJobStates.WhoIsMaster);

                State = MasterJobState.Requesting1;
                mCurrentMasterPollAttempts++;
                break;

            case MasterJobState.Inactive:
                await NegotiationTransmit(MasterJobStates.WhoIsMaster);

                if (mCurrentMasterPollAttempts == 0)
                {
                    schedule.Frequency = TimeSpan.FromSeconds(10 + mRandom.Next(60));
                }
                if (mCurrentMasterPollAttempts >= 3)
                {
                    State = MasterJobState.Starting;
                }
                mCurrentMasterPollAttempts++;
                break;

            case MasterJobState.Requesting1:
                await NegotiationTransmit(MasterJobStates.RequestingControl1);

                State = MasterJobState.Requesting2;
                break;

            case MasterJobState.Requesting2:
                await NegotiationTransmit(MasterJobStates.RequestingControl2);

                State = MasterJobState.TakingControl;
                break;

            case MasterJobState.TakingControl:
                await NegotiationTransmit(MasterJobStates.TakingControl);

                State = MasterJobState.Active;
                MasterJobStart();
                break;

            case MasterJobState.Active:
                await NegotiationTransmit(MasterJobStates.IAmMaster);

                schedule.Frequency      = TimeSpan.FromSeconds(5 + mRandom.Next(25));
                mCurrentMasterServiceId = null;
                break;
            }

            mMasterJobLastPollTime = DateTime.UtcNow;
        }
 /// <summary>
 /// Sets the next poll time for the poll schedule.
 /// </summary>
 /// <param name="schedule">The schedule.</param>
 /// <param name="state">The current state</param>
 /// <param name="pollAttempts">The current poll attempts.</param>
 public abstract void SetNextPollTime(Schedule schedule, MasterJobState state, int pollAttempts);
Example #8
0
 public MasterJobStateChange(MasterJobState oldState, MasterJobState newState)
 {
     StateOld = oldState;
     StateNew = newState;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="MasterJobStateChangeEventArgs"/> class.
 /// </summary>
 /// <param name="oldState">The old state.</param>
 /// <param name="newState">The new state.</param>
 /// <param name="iteration">The current iteration.</param>
 public MasterJobStateChangeEventArgs(MasterJobState oldState, MasterJobState newState, long?iteration = null) : base(iteration)
 {
     StateOld = oldState;
     StateNew = newState;
 }