Example #1
0
 public static void ExecuteAsync(ITaskEntity task, ScheduledTaskEntity scheduledTask, IUserEntity user)
 {
     using (ExecutionContext.SuppressFlow())
         Task.Run(() =>
         {
             try
             {
                 ExecuteSync(task, scheduledTask, user);
             }
             catch (Exception e)
             {
                 e.LogException(ex =>
                 {
                     ex.ControllerName = "SchedulerLogic";
                     ex.ActionName = "ExecuteAsync";
                 });
             }
         });
 }
Example #2
0
        public static Lite<IEntity> ExecuteSync(ITaskEntity task, ScheduledTaskEntity scheduledTask, IUserEntity user)
        {
            IUserEntity entityIUser = user ?? (IUserEntity)scheduledTask.User.Retrieve();

            using (IsolationEntity.Override(entityIUser.TryIsolation()))
            {
                ScheduledTaskLogEntity stl = new ScheduledTaskLogEntity
                {
                    Task = task,
                    ScheduledTask = scheduledTask,
                    StartTime = TimeZoneManager.Now,
                    MachineName = Environment.MachineName,
                    ApplicationName = Schema.Current.ApplicationName,
                    User = entityIUser.ToLite(),
                };

                using (AuthLogic.Disable())
                {
                    using (Transaction tr = Transaction.ForceNew())
                    {
                        stl.Save();

                        tr.Commit();
                    }
                }

                try
                {
                    using (UserHolder.UserSession(entityIUser))
                    {
                        using (Transaction tr = Transaction.ForceNew())
                        {
                            stl.ProductEntity = ExecuteTask.Invoke(task);

                            using (AuthLogic.Disable())
                            {
                                stl.EndTime = TimeZoneManager.Now;
                                stl.Save();
                            }

                            tr.Commit();
                        }
                    }
                }
                catch (Exception ex)
                {
                    using (AuthLogic.Disable())
                    {
                        if (Transaction.InTestTransaction)
                            throw;

                        var exLog = ex.LogException().ToLite();

                        using (Transaction tr = Transaction.ForceNew())
                        {
                            stl.Exception = exLog;
                            stl.EndTime = TimeZoneManager.Now;
                            stl.Save();

                            tr.Commit();
                        }
                    }
                    throw;

                }

                return stl.ProductEntity;
            }
        }
 public static void ExecuteAsync(ITaskEntity task, ScheduledTaskEntity scheduledTask, IUserEntity user)
 {
     Task.Factory.StartNew(() =>
     {
         try
         {
             ExecuteSync(task, scheduledTask, user);
         }
         catch (Exception e)
         {
             e.LogException(ex =>
             {
                 ex.ControllerName = "SchedulerLogic";
                 ex.ActionName = "ExecuteAsync";
             });
         }
     });
 }