public async Task StoreTrigger(IOperableTrigger newTrigger, bool replaceExisting, CancellationToken cancellationToken = default) { using var session = Store.OpenAsyncSession(); if (await session.Advanced.ExistsAsync(newTrigger.Key.GetDatabaseId(), cancellationToken)) { if (!replaceExisting) { throw new ObjectAlreadyExistsException(newTrigger); } } if (!await session.Advanced.ExistsAsync(newTrigger.JobKey.GetDatabaseId(), cancellationToken)) { throw new JobPersistenceException("The job (" + newTrigger.JobKey + ") referenced by the trigger does not exist."); } var trigger = new Trigger(newTrigger, InstanceName); var isTriggerGroupPaused = await session .Query <Trigger>() .Include(t => t.Scheduler) .Where(t => Equals(t.Group, newTrigger.Key.Group) && (t.State == InternalTriggerState.Paused || t.State == InternalTriggerState.PausedAndBlocked)) .AnyAsync(cancellationToken); var scheduler = await session.LoadAsync <Scheduler>(InstanceName, cancellationToken); if (scheduler != null) { var isJobGroupPaused = scheduler.PausedJobGroups.Contains(newTrigger.JobKey.Group); // make sure trigger group is not paused and that job is not blocked if (isTriggerGroupPaused || isJobGroupPaused) { trigger.State = InternalTriggerState.Paused; if (scheduler.BlockedJobs.Contains(newTrigger.GetJobDatabaseId())) { trigger.State = InternalTriggerState.PausedAndBlocked; } } else if (scheduler.BlockedJobs.Contains(newTrigger.GetJobDatabaseId())) { trigger.State = InternalTriggerState.Blocked; } } // Overwrite if exists await session.StoreAsync(trigger, trigger.Key, cancellationToken); await session.SaveChangesAsync(cancellationToken); }