public void Execute(IJobHandlerConfiguration configuration, ExecutionEntity execution, CommandContext commandContext, string tenantId)
        {
            var conf = (HistoryCleanupJobHandlerConfiguration)configuration;
            //find JobEntity
            JobEntity jobEntity = commandContext.JobManager.FindJobByHandlerType(Type);

            bool rescheduled = false;

            if (conf.ImmediatelyDue || (HistoryCleanupHelper.IsBatchWindowConfigured(commandContext) && HistoryCleanupHelper.IsWithinBatchWindow(ClockUtil.CurrentTime, commandContext)))
            {
                //find data to delete
                HistoryCleanupBatch nextBatch = HistoryCleanupHelper.GetNextBatch(commandContext);
                if (nextBatch.Size() >= GetBatchSizeThreshold(commandContext))
                {
                    //delete bunch of data
                    nextBatch.PerformCleanup();

                    //ReSchedule now
                    commandContext.JobManager.ReSchedule(jobEntity, ClockUtil.CurrentTime);
                    rescheduled = true;
                    CancelCountEmptyRuns(conf, jobEntity);
                }
                else
                {
                    //still have something to delete
                    if (nextBatch.Size() > 0)
                    {
                        nextBatch.PerformCleanup();
                    }
                    //not enough data for cleanup was found
                    if (HistoryCleanupHelper.IsWithinBatchWindow(ClockUtil.CurrentTime, commandContext))
                    {
                        //ReSchedule after some delay
                        DateTime nextRunDate = conf.GetNextRunWithDelay(ClockUtil.CurrentTime);
                        if (HistoryCleanupHelper.IsWithinBatchWindow(nextRunDate, commandContext))
                        {
                            commandContext.JobManager.ReSchedule(jobEntity, nextRunDate);
                            rescheduled = true;
                            IncrementCountEmptyRuns(conf, jobEntity);
                        }
                    }
                }
            }
            if (!rescheduled)
            {
                if (HistoryCleanupHelper.IsBatchWindowConfigured(commandContext))
                {
                    ReScheduleRegularCall(commandContext, jobEntity);
                }
                else
                {
                    //nothing more to do, suspend the job
                    SuspendJob(jobEntity);
                }
                CancelCountEmptyRuns(conf, jobEntity);
            }
        }
        /// <summary>
        ///     Creates next batch object for history cleanup. First searches for historic process instances ready for cleanup. If
        ///     there is still some place left in batch
        ///     (configured batch size was not reached), searches for historic decision instances and also adds them to the batch.
        /// </summary>
        /// <param name="commandContext">
        ///     @return
        /// </param>
        public static HistoryCleanupBatch GetNextBatch(CommandContext commandContext)
        {
            var batchSize           = GetHistoryCleanupBatchSize(commandContext);
            var historyCleanupBatch = new HistoryCleanupBatch();

            //add process instance ids
            IList <string> historicProcessInstanceIds =
                commandContext.HistoricProcessInstanceManager.FindHistoricProcessInstanceIdsForCleanup(batchSize.Value);

            if (historicProcessInstanceIds.Count > 0)
            {
                historyCleanupBatch.HistoricProcessInstanceIds = historicProcessInstanceIds;
            }

            //if batch is not full, add decision instance ids
            if (historyCleanupBatch.Size() < batchSize && commandContext.ProcessEngineConfiguration.DmnEnabled)
            {
                //IList<string> historicDecisionInstanceIds = commandContext.HistoricDecisionInstanceManager.findHistoricDecisionInstanceIdsForCleanup(batchSize - historyCleanupBatch.size());
                // if (historicDecisionInstanceIds.Count > 0)
                // {
                //historyCleanupBatch.HistoricDecisionInstanceIds = historicDecisionInstanceIds;
                // }
            }

            //if batch is not full, add case instance ids
            if (historyCleanupBatch.Size() < batchSize && commandContext.ProcessEngineConfiguration.CmmnEnabled)
            {
                //IList<string> historicCaseInstanceIds = commandContext.HistoricCaseInstanceManager.findHistoricCaseInstanceIdsForCleanup(batchSize - historyCleanupBatch.size());
                // if (historicCaseInstanceIds.Count > 0)
                // {
                //historyCleanupBatch.HistoricCaseInstanceIds = historicCaseInstanceIds;
                // }
            }

            return(historyCleanupBatch);
        }