/// <summary>
        /// The method that registers Service Bus listeners and assigns them to a receive event.  When an event from the service bus listener is raised, trigger the callbackURL
        /// </summary>
        /// <param name="logicAppId"></param>
        /// <param name="triggerInput"></param>
        /// <returns></returns>
        public async Task RegisterTrigger(string workflowId, SubscriptionInfo subscriptionInfo, bool addToTable = true)
        {
            // Add to the table store
            if (addToTable)
            {
                // according to https://msdn.microsoft.com/en-us/library/azure/dd179338.aspx, PartitionKeys do not allow the "/" character, so when inserting we replaced with "||"
                TriggerRecord newTrigger = new TriggerRecord(workflowId.Replace("/", "||"));
                string        jsonString = JsonConvert.SerializeObject(subscriptionInfo);
                newTrigger.subscriptionInfo = jsonString;
                TableOperation insertOperation = TableOperation.Insert(newTrigger);
                try
                {
                    triggerTable.Execute(insertOperation);
                }
                catch (StorageException ex)
                {
                    throw new Exception("Error occurred while trying to add trigger to Azure Table. Error: " + ex.RequestInformation.ExtendedErrorInformation.ErrorMessage, ex);
                }
            }

            // Create a new listener for this trigger
            ServiceBusListener listener = new ServiceBusListener(subscriptionInfo);

            HostingEnvironment.QueueBackgroundWorkItem(async ct => await listener.StartListening(subscriptionInfo.triggerConfig));
            subscriptionInfo.listener = listener;

            // Register the logicAppId in the store, so on subsequent checks from the logic app we don't spin up a new set of listeners
            _store[workflowId] = subscriptionInfo;
        }
        public void ScheduleNext(TriggerRecord triggerRecord)
        {
            DateTime triggerTime = triggerRecord.TIME;
            double   tickTime    = (triggerTime - DateTime.Now).TotalMilliseconds;

            if (tickTime < 0)
            {
                sourceLogger.Info($"Firing Trigger: {triggerRecord}");
                DistributeMessage(triggerRecord);
                try {
                    if (triggerQueue.Count == 0)
                    {
                        return;
                    }
                    ScheduleNext(triggerQueue.Dequeue());
                } catch (Exception ex) {
                    sourceLogger.Error(ex, "Error dequeuing next event (2) " + ex.Message);
                }
                return;
            }
            timers.Clear();
            Timer timer = new Timer(tickTime)
            {
                AutoReset = false
            };

            timer.Elapsed += (sender, e) => { Timer_Elapsed(triggerRecord); };
            timer.Start();
            timers.Add(timer);
            NextTrigger = triggerRecord.TIME;
            sourceLogger.Trace($"Started Event Timer for: {triggerRecord}");
        }
        internal Tuple <int, int, TriggerRecord> InitTriggers(int duration)
        {
            if (triggerRecords.Count == 0)
            {
                return(new Tuple <int, int, TriggerRecord>(0, 0, null));
            }
            DateTime endOfTest = DateTime.Now.AddSeconds(duration);

            int lateTriggers  = 0;
            int earlyTriggers = 0;

            List <TriggerRecord> sortedTriggers = new List <TriggerRecord>();

            DateTime now = DateTime.Now;

            foreach (TriggerRecord rec in triggerRecords)
            {
                // First, modify the time relative to start if it is a relative trigger.
                if (rec.isRelative)
                {
                    bool hasOffset = double.TryParse(rec.baseTime, out double offset);
                    if (hasOffset)
                    {
                        rec.TIME = now.AddMilliseconds(offset * 1000);
                    }
                }

                if (rec.TIME > endOfTest)
                {
                    lateTriggers++;
                    continue;
                }
                if (rec.TIME < now)
                {
                    earlyTriggers++;
                    continue;
                }

                sortedTriggers.Add(rec);
            }

            sortedTriggers.Sort((x, y) => { return(x.TIME.CompareTo(y.TIME)); });

            //   this.executionController.clientHub.SetSchedTrigger(executionController.executionNodeUuid, sortedTriggers);

            triggerQueue = new Queue <TriggerRecord>(sortedTriggers);

            if (triggerQueue.Count > 0)
            {
                TriggerRecord first = triggerQueue.Peek();
                return(new Tuple <int, int, TriggerRecord>(earlyTriggers, lateTriggers, first));
            }
            else
            {
                return(null);
            }
        }
 public void AddSchedTrigger(string executionNodeUuid, TriggerRecord triggerRecord)
 {
     try {
         Application.Current.Dispatcher.Invoke(delegate {
             CentralMessagingHub.executionUI.SchedTriggers.Add(triggerRecord);
         });
     } catch (Exception ex) {
         Debug.WriteLine("Add Sched Triggers " + ex.Message);
     }
 }
 public void Timer_Elapsed(TriggerRecord rec)
 {
     DistributeMessage(rec);
     try {
         if (triggerQueue.Count == 0)
         {
             return;
         }
         ScheduleNext(triggerQueue.Dequeue());
     } catch (Exception ex) {
         sourceLogger.Error(ex, "Error dequeuing next event (1)");
     }
 }
        internal bool ScheduleEvent(TriggerRecord triggerRecord)
        {
            double tickTime = (triggerRecord.TIME - DateTime.Now).TotalMilliseconds;

            if (tickTime < 0)
            {
                return(false);
            }
            triggerRecords.Add(triggerRecord);

            //      this.executionController.clientHub.AddSchedTrigger(executionController.executionNodeUuid, triggerRecord);

            return(true);
        }
 public void ScheduleFirst()
 {
     try {
         if (triggerQueue.Count > 0)
         {
             TriggerRecord first = triggerQueue.Dequeue();
             if (first != null)
             {
                 ScheduleNext(first);
             }
         }
     } catch (Exception) {
         // NO-OP
     }
 }
        public void DistributeMessage(TriggerRecord rec)
        {
            //Used by event driven sources

            if (rec.refreshFlight && rec.record.Item2 != null)
            {
                try {
                    FlightNode update = rec.record.Item2.RefeshFlight(executionController.amshost, executionController.token, executionController.apt_code).Result;
                    if (update != null)
                    {
                        rec.record.Item2.UpdateFlight(update);
                    }
                } catch (Exception e) {
                    sourceLogger.Warn(e, "Failed to update flight. Using original data");
                }
            }

            if (rec.record.Item2 != null)
            {
                if (rec.expression != null)
                {
                    bool pass = rec.expression.Pass(rec.record.Item2.FightXML);
                    if (!pass)
                    {
                        sourceLogger.Trace("Post Filtering: Flight did not pass filter");
                        return;
                    }
                }
                if (rec.topLevelFilter != null)
                {
                    bool pass = rec.topLevelFilter.Pass(rec.record.Item2.FightXML);
                    if (!pass)
                    {
                        sourceLogger.Trace("Post Filtering: Flight did not pass filter");
                        return;
                    }
                }
            }
            map[rec.ID]?.Fire(new TriggerFiredEventArgs(rec.ID, rec.record));

            // Fire any chained elements
            foreach (IChainedSourceController chain in rec.chain)
            {
                chain.ParentFired(rec.record);
            }

            //       this.executionController.clientHub.DispatcherDistributeMessage(executionController.executionNodeUuid, rec);
        }
        public void DispatcherDistributeMessage(string executionNodeID, TriggerRecord rec)
        {
            Task.Run(() => {
                TriggerRecord remove = null;

                foreach (TriggerRecord r in CentralMessagingHub.executionUI.SchedTriggers)
                {
                    if (r.uuid == rec.uuid)
                    {
                        remove = r;
                        break;
                    }
                }
                if (remove != null)
                {
                    try {
                        Application.Current.Dispatcher.Invoke(delegate {
                            CentralMessagingHub.executionUI.SchedTriggers.Remove(remove);
                            CentralMessagingHub.executionUI.OnPropertyChanged("lvTriggers");
                            CentralMessagingHub.executionUI.FiredTriggers.Add(rec);
                            CentralMessagingHub.executionUI.OnPropertyChanged("lvFiredTriggers");
                        });
                    } catch (Exception ex) {
                        Debug.WriteLine("Dispatcher Distribute Error " + ex.Message);
                    }
                }
                else
                {
                    try {
                        Application.Current.Dispatcher.Invoke(delegate {
                            CentralMessagingHub.executionUI.FiredTriggers.Add(rec);
                            CentralMessagingHub.executionUI.OnPropertyChanged("lvFiredTriggers");
                        });
                    } catch (Exception ex) {
                        Debug.WriteLine("Dispatcher Distribute Error " + ex.Message);
                    }
                }
            });
        }
        public async Task UnregisterTrigger(string workflowId)
        {
            if (_store.ContainsKey(workflowId))
            {
                // Remove from the table
                // according to https://msdn.microsoft.com/en-us/library/azure/dd179338.aspx, PartitionKeys do not allow the "/" character, so when inserting we replaced with "||"
                TriggerRecord oldTrigger = new TriggerRecord(workflowId.Replace("/", "||"));
                oldTrigger.ETag = "*";
                TableOperation deleteOperation = TableOperation.Delete(oldTrigger);
                try
                {
                    triggerTable.Execute(deleteOperation);
                }
                catch (StorageException ex)
                {
                    throw new Exception("Error occurred while trying to remove trigger from Azure Table. Error: " + ex.RequestInformation.ExtendedErrorInformation.ErrorMessage, ex);
                }

                // stop listening
                await _store[workflowId].listener.StopListening();
            }
        }
Esempio n. 11
0
        private void buttonEdit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //update category trigger settings
                bool settingsUpdated = false;
                string centralPath = reportingInfo.CentralPath;
                if (AppCommand.Instance.ConfigDictionary.ContainsKey(centralPath))
                {
                    Configuration config = AppCommand.Instance.ConfigDictionary[centralPath];
                    var updaterFound = from updater in config.updaters where updater.updaterId.ToLower() == reportingInfo.UpdaterId.ToLower() select updater;
                    if (updaterFound.Count() > 0)
                    {
                        ProjectUpdater pUpdater = updaterFound.First();
                        int updaterIndex = config.updaters.IndexOf(pUpdater);

                        var triggerFound = from trigger in pUpdater.CategoryTriggers where trigger.categoryName == reportingInfo.CategoryName select trigger;
                        if (triggerFound.Count() > 0)
                        {
                            CategoryTrigger cTrigger = triggerFound.First();
                            int triggerIndex = pUpdater.CategoryTriggers.IndexOf(cTrigger);

                            config.updaters[updaterIndex].CategoryTriggers[triggerIndex].isEnabled = false;

                            AppCommand.Instance.ConfigDictionary.Remove(centralPath);
                            AppCommand.Instance.ConfigDictionary.Add(centralPath, config);
                            
                            //refresh category trigger
                            AppCommand.Instance.DTMUpdaterInstance.Unregister(currentDoc);
                            AppCommand.Instance.DTMUpdaterInstance.Register(currentDoc, config.updaters[updaterIndex]);

                            settingsUpdated = true;
                        }
                    }
                }

                if (settingsUpdated)
                {
                    //database updated
                    TriggerRecord record = new TriggerRecord()
                    {
                        configId = reportingInfo.ConfigId,
                        centralPath = reportingInfo.CentralPath,
                        updaterId = reportingInfo.UpdaterId,
                        categoryName = reportingInfo.CategoryName,
                        elementUniqueId = reportingInfo.ReportingUniqueId,
                        edited = DateTime.Now,
                        editedBy = Environment.UserName
                    };

                    string content;
                    string errMessage;
                    
                    HttpStatusCode status = ServerUtil.PostTriggerRecords(out content, out errMessage, record);

                    this.DialogResult = false;
                }
                this.Close();
                
            }
            catch (Exception ex)
            {
                LogUtil.AppendLog("DTMWindow-buttonEdit_Click:" + ex.Message);
            }
        }
Esempio n. 12
0
        public static HttpStatusCode PostTriggerRecords(out string content, out string errorMessage, TriggerRecord record)
        {
            HttpStatusCode status = HttpStatusCode.Unused;

            content      = "";
            errorMessage = "";
            try
            {
                var client  = new RestClient(RestApiBaseUrl);
                var request = new RestRequest(apiVersion + "/triggerrecords", Method.POST);
                request.RequestFormat = RestSharp.DataFormat.Json;
                request.AddBody(record);

                IRestResponse response = client.Execute <TriggerRecord>(request);
                content      = response.Content;
                errorMessage = response.ErrorMessage;
                status       = response.StatusCode;
            }
            catch (Exception ex)
            {
                string message = ex.Message;
                LogUtil.AppendLog("ServerUtil-PostTriggerRecords:" + ex.Message);
            }
            return(status);
        }