Пример #1
0
        /// <summary>
        /// Removes the specified event from the schedule
        /// </summary>
        /// <param name="scheduledEvent">The event to be removed</param>
        public override void Remove(ScheduledEvent scheduledEvent)
        {
            int id;

            ScheduledEvents.TryRemove(scheduledEvent, out id);

            _sortingScheduledEventsRequired = true;
        }
Пример #2
0
        /// <summary>
        /// Adds the specified event to the schedule
        /// </summary>
        /// <param name="scheduledEvent">The event to be scheduled, including the date/times the event fires and the callback</param>
        public override void Add(ScheduledEvent scheduledEvent)
        {
            if (Algorithm != null)
            {
                scheduledEvent.SkipEventsUntil(Algorithm.UtcTime);
            }

            ScheduledEvents.AddOrUpdate(scheduledEvent, GetScheduledEventUniqueId());
        }
Пример #3
0
 public void Unschedule(string identifier)
 {
     ScheduledEvents.ForEachReverse(i => {
         if (i.Identifier == identifier)
         {
             this.Unschedule(i);
         }
     });
 }
Пример #4
0
        public CustomJsonResult AjaxEdit(ScheduledEvents model)
        {
            var key = LRequest.GetFormString("Key");

            #region MyRegion
            ScheduleConfigInfo sci = ScheduleConfigs.GetConfig();
            foreach (EventInfo ev1 in sci.Events)
            {
                if (ev1.Key == model.Key.Trim())
                {
                    ModelState.AddModelError("Key", "消息:计划任务名称已经存在!");
                    //return RedirectToAction("Index", new { currentPageNum = model.CurrentPageNum, pageSize = model.PageSize });
                }
            }
            foreach (EventInfo ev1 in sci.Events)
            {
                if (ev1.Key == key)
                {
                    ev1.Key          = model.Key.Trim();
                    ev1.ScheduleType = model.ScheduleType.Trim();

                    if (model.ExetimeType)
                    {
                        ev1.TimeOfDay = model.hour * 60 + model.minute;
                        ev1.Minutes   = sci.TimerMinutesInterval;
                    }
                    else
                    {
                        if (model.timeserval < sci.TimerMinutesInterval)
                        {
                            ev1.Minutes = sci.TimerMinutesInterval;
                        }
                        else
                        {
                            ev1.Minutes = model.timeserval;
                        }
                        ev1.TimeOfDay = -1;
                    }
                    if (!ev1.IsSystemEvent)
                    {
                        ev1.Enabled = model.Enable;
                    }
                    break;
                }
            }
            ScheduleConfigs.SaveConfig(sci);
            #endregion

            var result = new Result(true);

            var json = new CustomJsonResult();
            json.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            json.Data = result;

            return(json);
        }
Пример #5
0
        private static void DomainReset()
        {
            if (ScheduledEvents != null)
            {
                ScheduledEvents.Clear();
            }

            instance             = null;
            ScheduledEventsCount = 0;
        }
Пример #6
0
        public CustomJsonResult AjaxAdd(ScheduledEvents model, FormCollection collection)
        {
            int Entity_ExetimeType = 0;

            if (collection.GetValues("Entity.ExetimeType") != null)
            {
                Entity_ExetimeType = int.Parse(collection.GetValue("Entity.ExetimeType").AttemptedValue);
            }

            #region MyRegion
            ScheduleConfigInfo sci = ScheduleConfigs.GetConfig();
            foreach (EventInfo ev1 in sci.Events)
            {
                if (ev1.Key == model.Key.Trim())
                {
                    ModelState.AddModelError("Key", "消息:计划任务名称已经存在!");
                    //return RedirectToAction("Index", new { currentPageNum = model.CurrentPageNum, pageSize = model.PageSize });
                }
            }

            EventInfo ev = new EventInfo();
            ev.Key            = model.Key;
            ev.Enabled        = true;
            ev.IsSystemEvent  = false;
            ev.ScheduleType   = model.ScheduleType.ToString();
            model.ExetimeType = Entity_ExetimeType == 0 ? false : true;

            if (model.ExetimeType)
            {
                ev.TimeOfDay = model.hour * 60 + model.minute;
                ev.Minutes   = sci.TimerMinutesInterval;
            }
            else
            {
                ev.Minutes   = model.timeserval;
                ev.TimeOfDay = -1;
            }
            EventInfo[] es = new EventInfo[sci.Events.Length + 1];
            for (int i = 0; i < sci.Events.Length; i++)
            {
                es[i] = sci.Events[i];
            }
            es[es.Length - 1] = ev;
            sci.Events        = es;
            ScheduleConfigs.SaveConfig(sci);
            #endregion

            var result = new Result(true);

            var json = new CustomJsonResult();
            json.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            json.Data = result;

            return(json);
        }
Пример #7
0
 //--------------------------------------------------------------------------------------------------------------------
 // - Schedule Event
 //--------------------------------------------------------------------------------------------------------------------
 public static void Schedule(Action action, float delay, Func <bool> trigger = null, object sender = null)
 {
     ScheduledEvents.AddLast(new Event()
     {
         Action = action,
         // Convert the delay in seconds to frames
         Delay   = (int)Math.Round(delay * EventCycle.Framerate),
         Trigger = trigger,
         Sender  = sender,
     });
 }
Пример #8
0
        private List <ScheduledEvent> GetScheduledEventsSortedByTime()
        {
            if (_sortingScheduledEventsRequired)
            {
                _sortingScheduledEventsRequired = false;
                _scheduledEventsSortedByTime    = ScheduledEvents
                                                  // we order by next event time
                                                  .OrderBy(x => x.Key.NextEventUtcTime)
                                                  // then by unique id so that for scheduled events in the same time
                                                  // respect their creation order, so its deterministic
                                                  .ThenBy(x => x.Value)
                                                  .Select(x => x.Key).ToList();
            }

            return(_scheduledEventsSortedByTime);
        }
Пример #9
0
        /// <summary>
        /// Reset the system. Removes all scheduled events and destroys instance.
        /// </summary>
        public static void Clear()
        {
            if (ScheduledEvents != null)
            {
                ScheduledEvents.Clear();
            }

            if (instance != null)
            {
                Destroy(instance.gameObject);
            }

            instance = null;

            ScheduledEventsCount = 0;
        }
Пример #10
0
        /// <summary>
        /// Adds the specified event to the schedule
        /// </summary>
        /// <param name="scheduledEvent">The event to be scheduled, including the date/times the event fires and the callback</param>
        public override void Add(ScheduledEvent scheduledEvent)
        {
            if (Algorithm != null)
            {
                scheduledEvent.SkipEventsUntil(Algorithm.UtcTime);
            }

            ScheduledEvents.AddOrUpdate(scheduledEvent, GetScheduledEventUniqueId());

            if (Log.DebuggingEnabled)
            {
                scheduledEvent.IsLoggingEnabled = true;
            }

            _sortingScheduledEventsRequired = true;
        }
Пример #11
0
        public void NotifyRemoval(BaseNotifier notifier)
        {
            Event eventToBeNotifiedAbout = ((EventNotifier)notifier).EvetToBeNotifiedAbout;

            if (!eventToBeNotifiedAbout.Participants.Contains(this, new PersonComparer()))
            {
                return;
            }

            if (!ScheduledEvents.Contains(eventToBeNotifiedAbout, new EventComparer()))
            {
                return;
            }

            CancleParticipationInEvent(eventToBeNotifiedAbout);
            DbManager.Instance.ModifyPerson(this);
        }
Пример #12
0
        //--------------------------------------------------------------------------------------------------------------------
        // - Event Scheduler Main Routine
        //--------------------------------------------------------------------------------------------------------------------
        private static void Step()
        {
            LinkedListNode <Event> next;

            for (LinkedListNode <Event> scheduled = ScheduledEvents.First; scheduled != null;)
            {
                next = scheduled.Next;

                // For any event whose delay has expired or their triggering event has fired TRUE, execute and remove it
                if (scheduled.Value.Countdown() || (scheduled.Value.Trigger?.Invoke() ?? false))
                {
                    scheduled.Value.Action();
                    ScheduledEvents.Remove(scheduled);
                }
                scheduled = next;
            }
        }
Пример #13
0
        public void AddScheduledEventItem(int serviceId, EventItem eventItem)
        {
            if (!ScheduledEvents.ContainsKey(serviceId))
            {
                ScheduledEvents[serviceId] = new List <EventItem>();
            }

            foreach (var item in ScheduledEvents[serviceId])
            {
                if (item.EventId == eventItem.EventId)
                {
                    return;
                }
            }

            ScheduledEvents[serviceId].Add(eventItem);
        }
Пример #14
0
        public void NotifyChange(BaseNotifier notifier)
        {
            Event eventToBeNotifiedAbout = ((EventNotifier)notifier).EvetToBeNotifiedAbout;

            if (eventToBeNotifiedAbout.Participants.Contains(this, new PersonComparer()))
            {
                if (ScheduledEvents.Contains(eventToBeNotifiedAbout, new EventComparer()))
                {
                    if (IsAvailableForEventExcludingOneSpecificEvent(eventToBeNotifiedAbout.ScheduledDateTimeBeging, eventToBeNotifiedAbout.ScheduledDateTimeEnd, eventToBeNotifiedAbout))
                    {
                        if (CancleParticipationInEvent(eventToBeNotifiedAbout) && ScheduleParticipationInEvent(eventToBeNotifiedAbout))
                        {
                            DbManager.Instance.ModifyPerson(this);
                        }
                    }
                    else
                    {
                        if (CancleParticipationInEvent(eventToBeNotifiedAbout))
                        {
                            DbManager.Instance.ModifyPerson(this);
                        }
                    }
                }
                else
                {
                    if (IsAvailableForEvent(eventToBeNotifiedAbout.ScheduledDateTimeBeging, eventToBeNotifiedAbout.ScheduledDateTimeEnd))
                    {
                        if (ScheduleParticipationInEvent(eventToBeNotifiedAbout))
                        {
                            DbManager.Instance.ModifyPerson(this);
                        }
                    }
                }
            }
            else
            {
                if (ScheduledEvents.Contains(eventToBeNotifiedAbout, new EventComparer()))
                {
                    if (CancleParticipationInEvent(eventToBeNotifiedAbout))
                    {
                        DbManager.Instance.ModifyPerson(this);
                    }
                }
            }
        }
Пример #15
0
        /// <summary>
        /// Execute the live realtime event thread montioring.
        /// It scans every second monitoring for an event trigger.
        /// </summary>
        public void Run()
        {
            IsActive = true;

            // continue thread until cancellation is requested
            while (!_cancellationTokenSource.IsCancellationRequested)
            {
                var time = DateTime.UtcNow;

                // pause until the next second
                var nextSecond = time.RoundUp(TimeSpan.FromSeconds(1));
                var delay      = Convert.ToInt32((nextSecond - time).TotalMilliseconds);
                Thread.Sleep(delay < 0 ? 1 : delay);

                // poke each event to see if it should fire, we order by unique id to be deterministic
                foreach (var kvp in ScheduledEvents.OrderBy(pair => pair.Value))
                {
                    var scheduledEvent = kvp.Key;
                    try
                    {
                        _isolatorLimitProvider.Consume(scheduledEvent, time);
                    }
                    catch (ScheduledEventException scheduledEventException)
                    {
                        var errorMessage = "LiveTradingRealTimeHandler.Run(): There was an error in a scheduled " +
                                           $"event {scheduledEvent.Name}. The error was {scheduledEventException.Message}";

                        Log.Error(scheduledEventException, errorMessage);

                        ResultHandler.RuntimeError(errorMessage);

                        // Errors in scheduled event should be treated as runtime error
                        // Runtime errors should end Lean execution
                        Algorithm.RunTimeError = new Exception(errorMessage);
                    }
                }
            }

            IsActive = false;
            Log.Trace("LiveTradingRealTimeHandler.Run(): Exiting thread... Exit triggered: " + _cancellationTokenSource.IsCancellationRequested);
        }
Пример #16
0
        //public List<CustomDomainValidation> CustomValidators { get; private set; } = new List<CustomDomainValidation>();

        public void Schedule(IDomainEvent evt, DateTime when, string identifier)
        {
            var due   = when.ToUniversalTime() - DateTime.UtcNow;
            var sched = ScheduledEvents.FirstOrDefault(s => s.Identifier == identifier) ??
                        new ScheduledDomainEvent()
            {
                Identifier = identifier ?? new RID().AsBase36,
            };

            sched.Event         = evt;
            sched.ScheduledTime = when;
            if (sched.timer != null)
            {
                sched.timer.Dispose();
                sched.timer = null;
            }

            sched.timer = new Timer((s) => {
                this.Raise((s as ScheduledDomainEvent).Event);
                this.Unschedule(sched);
            }, sched, (long)due.TotalMilliseconds, Timeout.Infinite);

            ScheduledEvents.Add(sched);
        }
Пример #17
0
        /// <summary>
        /// Schedule a new event to occur after the specified delay.
        /// </summary>
        /// <param name="delay">Delay in seconds.</param>
        /// <param name="action">Action that occurs after the delay.</param>
        /// <returns>Event that scheduled.</returns>
        public static ScheduledEvent Schedule(float delay, Action action)
        {
            if (Instance == null)
            {
                return(null);
            }

            if (action == null)
            {
                return(null);
            }

            if (delay == 0)
            {
                action?.Invoke();
                return(null);
            }

            var scheduledEvent = new ScheduledEvent(delay, action);

            ScheduledEvents.Add(scheduledEvent);
            ScheduledEventsCount++;
            return(scheduledEvent);
        }
Пример #18
0
        /// <summary>
        /// Removes the specified event from the schedule
        /// </summary>
        /// <param name="scheduledEvent">The event to be removed</param>
        public override void Remove(ScheduledEvent scheduledEvent)
        {
            int id;

            ScheduledEvents.TryRemove(scheduledEvent, out id);
        }
Пример #19
0
 public void Unschedule(ScheduledDomainEvent sched)
 {
     sched.timer.Dispose();
     ScheduledEvents.Remove(sched);
 }
Пример #20
0
        public override string ToString()
        {
            var sb = new StringBuilder(string.Format("; {0}\n\n(import (rnrs) (emodl cmslib))\n\n(start-model \"{0}\")\n\n", Name));

            if (Constraints.Any())
            {
                foreach (var constraint in Constraints)
                {
                    sb.AppendLine(constraint.ToString());
                }

                sb.AppendLine();
            }

            if (ScheduledEvents.Any())
            {
                foreach (var evt in ScheduledEvents)
                {
                    sb.AppendLine(evt.ToString());
                }

                sb.AppendLine();
            }

            if (TriggeredEvents.Any())
            {
                foreach (var evt in TriggeredEvents)
                {
                    sb.AppendLine(evt.ToString());
                }

                sb.AppendLine();
            }

            var expressions = Expressions.Where(e => (e.Name != null && e.Name != "rand"));

            if (expressions.Any())
            {
                foreach (var expression in expressions)
                {
                    sb.AppendLine(expression.ToString());
                }

                sb.AppendLine();
            }

            if (Observables.Any())
            {
                foreach (var observable in Observables)
                {
                    sb.AppendLine(observable.ToString());
                }

                sb.AppendLine();
            }

            var parameters = Parameters.Where(p => (p.Name != "time" && p.Name != "pi"));

            if (parameters.Any())
            {
                foreach (var parameter in parameters)
                {
                    sb.AppendLine(parameter.ToString());
                }

                sb.AppendLine();
            }

            var predicates = Predicates.Where(p => p.Name != null);

            if (predicates.Any())
            {
                foreach (var predicate in predicates)
                {
                    sb.AppendLine(predicate.ToString());
                }

                sb.AppendLine();
            }

            foreach (var locale in Locales)
            {
                if (locale.Name != "global")
                {
                    sb.AppendLine(locale.ToString());
                    sb.AppendLine(string.Format("(set-locale {0})\n", locale.Name));
                }
                var currentLocale = locale;
                foreach (var species in Species.Where(s => s.Locale == currentLocale))
                {
                    sb.AppendLine(species.ToString());
                }
                sb.AppendLine();
            }

            if (Reactions.Any())
            {
                foreach (var reaction in Reactions)
                {
                    sb.AppendLine(reaction.ToString());
                }

                sb.AppendLine();
            }

            sb.Append("(end-model)");

            return(sb.ToString());
        }