public void Execute(Guid stackId)
        {
            var stack = _stackRepository.Find(stackId);

            if (stack == null)
            {
                // nothing to do if the stack doesn't exist anymore
                return;
            }

            var instancesToStop = new List <string>();

            // ReSharper disable once LoopCanBeConvertedToQuery
            foreach (var id in stack.InstanceIds)
            {
                var instance = _instanceRepository.Find(id);
                if (instance != null)
                {
                    instancesToStop.Add(instance.ResourceId);
                }
            }

            // Only toggle the stack instances if the stack schedule is enabled
            // We do not end the chain of power on / power off cycles because
            // the enabled flag could be flipped between one run and the next
            if (stack.ScheduleEnabled)
            {
                _stopInstances.Execute(stack.OwnerProfileId, instancesToStop);
            }

            var nextStartTimespan = _scheduleCalculator.GetNextStart(stack);

            _backgroundJobClient.Schedule <ScheduledStartStack>(x => x.Execute(stackId), nextStartTimespan);
        }
        public void KickstartSchedule(Stack stack)
        {
            var timeUntilNextStart = _scheduleCalculator.GetNextStart(stack);
            var timeUntilNextStop  = _scheduleCalculator.GetNextStop(stack);

            var nextScheduleTypeResult = timeUntilNextStart.CompareTo(timeUntilNextStop);

            if (nextScheduleTypeResult < 0)
            {
                // start time is sooner
                _backgroundJobClient.Schedule <ScheduledStartStack>(x => x.Execute(stack.Id), timeUntilNextStart);
            }
            else if (nextScheduleTypeResult > 0)
            {
                // stop time is sooner
                _backgroundJobClient.Schedule <ScheduledStopStack>(x => x.Execute(stack.Id), timeUntilNextStop);
            }
            else
            {
                throw new InvalidOperationException("Start time and end time are the same.");
            }
        }