Пример #1
0
        private static void ProcessQueue(object discard)
        {
            Log.Info("Processing queue.");
            // check memory usage

            /*var memStatus = new WinBase.MEMORYSTATUSEX();
             * float installedMemory = 10;
             * if (Kernel32.GlobalMemoryStatusEx(memStatus))
             * {
             *  installedMemory = (float)memStatus.ullAvailPhys/memStatus.ullTotalPhys*100;
             * }
             *
             * // check network usage
             * const string TEMPFILE = "tempfile.tmp";
             * var webClient = new System.Net.WebClient();
             *
             * Console.WriteLine("Downloading file....");
             *
             * var sw = Stopwatch.StartNew();
             * webClient.DownloadFile("http://dl.google.com/googletalk/googletalk-setup.exe", TEMPFILE);
             * sw.Stop();
             *
             * var fileInfo = new FileInfo(TEMPFILE);
             * var speed = fileInfo.Length / sw.Elapsed.Seconds;
             *
             * Console.WriteLine("Download duration: {0}", sw.Elapsed);
             * Console.WriteLine("File size: {0}", fileInfo.Length.ToString("N0"));
             * Console.WriteLine("Speed: {0} bps ", speed.ToString("N0"));
             *
             * // check cpu usage
             * if (CpuCounter.NextValue() < 90 &&
             *  installedMemory > 10)
             * {
             */
            // we don't want the queue changing while we are getting the current list
            lock (QueueLock)
            {
                var items = QueuingContext.ActivityQueues
                            .Where(x => x.TimeStarted == null && x.TimeCompleted == null &&
                                   x.TimeAdded >= Start)
                            .ToList();
                foreach (var item in items)
                {
                    try
                    {
                        item.TimeStarted = DateTime.UtcNow;
                        QueuingContext.SubmitChanges();
                        ThreadPool.QueueUserWorkItem(ExecuteActivity, item.Id);
                        Log.Info(string.Format("Queued activity: {0}", item.Id));
                    }
                    catch (Exception ex)
                    {
                        Log.Error(string.Format("Error queuing: {0}", item.Id), ex);
                    }
                }
            }
        }
Пример #2
0
        private static void ExecuteActivity(object state)
        {
            var id = (int)state;

            Log.Info(string.Format("Starting activity: {0}", id));
            Utilities.Activity activity;
            Utilities.Trigger  trigger;
            ActivityQueue      queue;

            // update the time started on the queue
            try
            {
                lock (QueueLock)
                {
                    queue = QueuingContext.ActivityQueues.First(x => x.Id == id);
                }
                trigger = Triggers.FirstOrDefault(x => x.Entity.Id == queue.TriggerId);
                if (trigger == null)
                {
                    throw new Exception("Trigger doesn't exist.");
                }
                activity = Activities.FirstOrDefault(x => x.Entity.Id == queue.ActivityId);
                if (activity == null)
                {
                    throw new Exception("Activity doesn't exist.");
                }
            }
            catch (Exception ex)
            {
                Log.Error("An error occurred while saving the start time of the activity.", ex);
                return;
            }

            Log.Info(string.Format("Executing activity: {0}", id));
            try
            {
                var serializer = new SoapFormatter();

                using (var stream = new MemoryStream(Encoding.Default.GetBytes(queue.State)))
                {
                    AppDomain.CurrentDomain.AssemblyResolve += LoadComponentAssembly;
                    var paramState = serializer.Deserialize(stream);
                    AppDomain.CurrentDomain.AssemblyResolve -= LoadComponentAssembly;
                    if (paramState as StoredQuery != null)
                    {
                        paramState = ((StoredQuery)paramState).Expression;
                    }
                    activity.Execute(paramState, queue, trigger);
                }
            }
            catch (Exception ex)
            {
                Log.Error(
                    string.Format("An exception occured while executing activity: {0} {1}", activity.Entity.Id,
                                  activity.Entity.Type), ex);
            }

            // change the timecompleted on the queue item
            try
            {
                lock (QueueLock)
                {
                    QueuingContext.ActivityQueues
                    .First(x => x.Id == queue.Id)
                    .TimeCompleted = DateTime.UtcNow;
                    QueuingContext.SubmitChanges();
                }
                if (ActivityCompleted != null)
                {
                    ActivityCompleted(activity);
                }
            }
            catch (Exception ex)
            {
                Log.Error(
                    string.Format("There was an error removing the activity from the queue: {0}",
                                  queue.Id), ex);
            }
        }
Пример #3
0
        private static void Triggered(Utilities.Trigger trigger, object state)
        {
            Log.Info(string.Format("Triggered by: {0}", trigger.Entity.Type));

            // queue the trigger for processing
            var activityTriggers = trigger.Entity.ActivityTriggers
                                   .Select(x => Activities
                                           .FirstOrDefault(y => y.Entity.Id == x.ActivityId))
                                   .Where(x => x != null && x.Enabled)
                                   .ToList();

            if (!activityTriggers.Any())
            {
                Log.Info("No enabled activites, exiting.");
                return;
            }

            // try to serialize state information
            try
            {
                using (var mem = new MemoryStream())
                {
                    var serlializer = new SoapFormatter
                    {
                        AssemblyFormat = FormatterAssemblyStyle.Full
                    };

                    // store in an object so we know what to expect
                    if (state as Expression != null)
                    {
                        state = new StoredQuery((Expression)state);
                    }
                    serlializer.Serialize(mem, state);
                    state = Encoding.Default.GetString(mem.ToArray());
                    var insertQueue = activityTriggers.Select(activity =>
                    {
                        Log.Info(string.Format("Queuing activity [{0}] due to trigger [{1}].", activity.Entity.Type,
                                               trigger.Entity.Type));
                        var queue = new ActivityQueue
                        {
                            TriggerId  = trigger.Entity.Id,
                            ActivityId = activity.Entity.Id,
                            State      = state.ToString(),
                            TimeAdded  = DateTime.UtcNow
                        };

                        if (queue.TriggerId == 7 && queue.ActivityId == 8)
                        {
                        }
                        return(queue);
                    });
                    lock (QueueLock)
                    {
                        QueuingContext.ActivityQueues.InsertAllOnSubmit(insertQueue);
                        QueuingContext.SubmitChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("Error queuing triggered activities.", ex);
            }

            ThreadPool.QueueUserWorkItem(ProcessQueue);
        }