Пример #1
0
        private ConfiguredActivity DeserializeActivity(XElement element)
        {
            var name = element.Attribute("name").Value;
            var desc = element.Attribute("description").Value;

            var activity = new ConfiguredActivity(name, desc);

            var asyncAttr = element.Attribute("async");

            if (asyncAttr != null)
            {
                activity.Async = Convert.ToBoolean(asyncAttr.Value);
            }

            if (activity.Async)
            {
                var delayAttr = element.Attribute("async-delay");
                if (delayAttr != null)
                {
                    activity.AsyncDelay = Convert.ToInt32(delayAttr.Value);
                }
            }

            var configElement = element.Element("config");

            if (configElement != null)
            {
                activity.Config = configElement.Value;
            }

            return(activity);
        }
Пример #2
0
 public ScheduledActivity(IEvent @event, ConfiguredActivity activity)
 {
     EventType                 = @event.GetType().AssemblyQualifiedNameWithoutVersion();
     EventData                 = JsonConvert.SerializeObject(@event);
     ActivityName              = activity.ActivityName;
     ActivityConfig            = activity.Config;
     ScheduledExecutionTimeUtc = DateTime.UtcNow.AddSeconds(activity.AsyncDelay);
 }
        public ConfiguredActivity ToConfiguredActivity()
        {
            var activity = new ConfiguredActivity(ActivityName, Description)
            {
                Config = Config,
                Async  = Async
            };

            if (activity.Async)
            {
                var delay = new TimeSpan(AsyncDelayDays, AsyncDelayHours, AsyncDelayMinutes, AsyncDelaySeconds);
                activity.AsyncDelay = (int)delay.TotalSeconds;
            }

            return(activity);
        }
Пример #4
0
        private XElement SerializeActivity(ConfiguredActivity activity)
        {
            var element = new XElement("activity",
                                       new XAttribute("name", activity.ActivityName),
                                       new XAttribute("description", activity.Description),
                                       new XAttribute("async", activity.Async)
                                       );

            if (activity.Async)
            {
                element.Add("async-delay", activity.AsyncDelay);
            }

            if (activity.Config != null)
            {
                var configElement = new XElement("config");
                configElement.Add(new XCData(activity.Config));
                element.Add(configElement);
            }

            return(element);
        }
        public static ConfiguredActivityModel FromConfiguredActivity(ConfiguredActivity activity)
        {
            var model = new ConfiguredActivityModel
            {
                ActivityName = activity.ActivityName,
                Description  = activity.Description,
                Config       = activity.Config,
                Async        = activity.Async
            };

            if (activity.Async)
            {
                if (activity.AsyncDelay > 0)
                {
                    var delay = TimeSpan.FromSeconds(activity.AsyncDelay);
                    model.AsyncDelayDays    = delay.Days;
                    model.AsyncDelayHours   = delay.Hours;
                    model.AsyncDelayMinutes = delay.Minutes;
                    model.AsyncDelaySeconds = delay.Seconds;
                }
            }

            return(model);
        }
Пример #6
0
        public void Execute(object executionState)
        {
            // Prevent the job execution overlapping
            if (InProgress)
            {
                return;
            }

            InProgress = true;

            var engine           = EngineContext.Current;
            var instanceManager  = engine.Resolve <ICommerceInstanceManager>();
            var activityProvider = engine.Resolve <IActivityProvider>();
            var now = DateTime.UtcNow;

            foreach (var instance in instanceManager.GetInstances())
            {
                // Begin a scope so the activities are able to resolve dependencies from the ioc container
                using (var scope = Scope.Begin(instance))
                {
                    var batchSize = 100;
                    var queue     = instance.Database.Repository <ScheduledActivity>();
                    var query     = queue.Query()
                                    .Where(x => x.Status == ActivityExecutionStatus.Pending && x.ScheduledExecutionTimeUtc <= now)
                                    .OrderBy(x => x.Id);

                    foreach (var queueItem in query.Batched(batchSize))
                    {
                        // Change status
                        queueItem.MarkStarted();
                        instance.Database.SaveChanges();

                        // Execute activity
                        using (var tx = instance.Database.BeginTransaction())
                        {
                            try
                            {
                                var @event   = queueItem.LoadEvent();
                                var activity = activityProvider.FindByName(queueItem.ActivityName);
                                if (activity != null)
                                {
                                    object parameters = null;
                                    if (activity.ConfigType != null)
                                    {
                                        parameters = ConfiguredActivity.LoadConfigModel(queueItem.ActivityConfig, activity.ConfigType);
                                    }

                                    activity.Execute(@event, new ActivityContext(parameters, true));
                                    // TODO: Delete queue item when success, but i think some log is needed
                                    queue.Delete(queueItem);
                                }
                                else
                                {
                                    queueItem.MarkFailed("Cannot find activity with name '" + queueItem.ActivityName + "'. Ensure the activity is installed.");
                                }
                            }
                            catch (Exception ex)
                            {
                                queueItem.MarkFailed(ex);
                            }

                            tx.Commit();
                        }
                    }
                }
            }

            InProgress = false;
        }