Esempio n. 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>

        public async Task Execute(IJobExecutionContext context)
        {
            try
            {
                // init time
                var time = DateTime.Now.AddMinutes(1);

                // init to time
                var toTime = new DateTime(time.Year, time.Month, time.Day, time.Hour, time.Minute, 0);

                // get ready scheduler
                var scheduler = SchedulerServices.GetAll(null, null, null, SchedulerStatus.Ready, true, SchedulerScope.Internal, null, toTime, null, 1).FirstOrDefault();
                if (scheduler != null)
                {
                    // run scheduler
                    await RunScheduler(scheduler);
                }
            }
            catch (Exception ex)
            {
                // create log
                var systemLog = new SystemLogModel
                {
                    Username         = "******",
                    Thread           = "JobScheduler",
                    Action           = SystemAction.TrackError,
                    ShortDescription = ex.Message,
                    LongDescription  = ex.ToString(),
                    IsException      = true,
                    CreatedDate      = DateTime.Now
                };

                // log exception
                SystemLogController.Create(systemLog);
            }
        }
Esempio n. 2
0
        /// <inheritdoc />
        /// <summary>
        /// Page load
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            // check session current user
            if (Session["CurrentUser"] == null)
            {
                Response.Redirect(Resource.Get("LoginUrl"), true);
            }

            // init current user
            CurrentUser = (UserModel)Session["CurrentUser"];

            // init menu id
            if (int.TryParse(Request.QueryString["mId"], out var parseMenuId) && parseMenuId > 0)
            {
                MenuId = parseMenuId;
            }
            else
            {
                var menuModel = MenuController.GetByUrl(Request.ApplicationPath);
                MenuId = menuModel?.Id ?? 0;
            }

            // check menu id zero or use is admin
            if (CurrentUser != null && CurrentUser.User.IsSuperUser)
            {
                // init full control permission
                CurrentPermission = new PermissionModel(true, true, true, true);
            }
            else
            {
                if (Request.ApplicationPath == "/" || string.Compare(Request.ApplicationPath, "/default.aspx", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    // init full control permission for default page
                    CurrentPermission = new PermissionModel(true, true, true, true);
                }
                else
                {
                    if (MenuId == 0)
                    {
                        CurrentPermission = new PermissionModel(false, false, false, false);
                    }
                    else
                    {
                        // get permission role
                        var menuRoles = MenuRoleServices.GetAll(null, MenuId, null, null).Where(mr => CurrentUser.Roles.Select(r => r.Id).Contains(mr.RoleId));
                        // int permission
                        var canRead     = false;
                        var canWrite    = false;
                        var canDelete   = false;
                        var fullControl = false;
                        foreach (var menuRole in menuRoles)
                        {
                            // permission in format RWDF - example 1100 => Read: true, Write: true, Delete: false, FullControl: false
                            fullControl = fullControl || menuRole.Permission[3] == '1';
                            canDelete   = fullControl || canDelete || menuRole.Permission[2] == '1';
                            canWrite    = canDelete || canWrite || menuRole.Permission[1] == '1';
                            canRead     = canWrite || canRead || menuRole.Permission[0] == '1';
                        }
                        // update permission by level
                        canDelete = fullControl || canDelete;
                        canWrite  = canDelete || canWrite;
                        canRead   = canWrite || canRead;
                        // init current page permission
                        CurrentPermission = new PermissionModel(canRead, canWrite, canDelete, fullControl);
                    }
                }
            }
            // check current permission
            if (CurrentPermission.CanRead)
            {
                try
                {
                    // check permission
                    var toolbarIds = new[] { "toolbarFn" };
                    foreach (var toolbarId in toolbarIds)
                    {
                        // set toolbar permission
                        var findToolbar = FindControl(toolbarId);
                        if (findToolbar != null)
                        {
                            // cast to Ext.Net.Toolbar
                            var toolbarFn = (Toolbar)findToolbar;
                            // check all button in toolbar
                            foreach (var item in toolbarFn.Items)
                            {
                                if (item.InstanceOf == "Ext.Button")
                                {
                                    // cast obj to button
                                    var btn = (Button)item;
                                    // set write (add & edit) permission
                                    if (btn.ID.Contains("Add") || btn.ID.Contains("Edit"))
                                    {
                                        btn.Visible = CurrentPermission.CanWrite;
                                    }
                                    // set delete permission
                                    if (btn.ID.Contains("Delete"))
                                    {
                                        btn.Visible = CurrentPermission.CanDelete;
                                    }
                                }
                            }
                        }
                    }
                    // base load
                    base.OnLoad(e);
                }
                catch (Exception ex)
                {
                    SystemLogController.Create(new SystemLogModel("System", "BasePage", ex));
                }
            }
            else
            {
                Dialog.Alert("Cảnh báo truy cập", "Bạn không có quyền truy cập chức năng này");
            }
        }
Esempio n. 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="scheduler"></param>
        private Task RunScheduler(Scheduler scheduler)
        {
            try
            {
                // next run time
                DateTime?nextRunTime = null;
                if (scheduler.NextRunTime == null)
                {
                    return(Task.FromCanceled(new CancellationToken(true)));
                }
                // switch repeat type
                switch (scheduler.RepeatType)
                {
                case SchedulerRepeatType.OneTime:
                    break;

                case SchedulerRepeatType.Monthly:
                    nextRunTime = scheduler.NextRunTime.Value.AddMonths(1);
                    break;

                case SchedulerRepeatType.Weekly:
                    nextRunTime = scheduler.NextRunTime.Value.AddDays(7);
                    break;

                case SchedulerRepeatType.Daily:
                    nextRunTime = scheduler.NextRunTime.Value.AddDays(1);
                    break;

                case SchedulerRepeatType.Hourly:
                    nextRunTime = scheduler.NextRunTime.Value.AddHours(1);
                    break;

                case SchedulerRepeatType.IntervalTime:
                    if (scheduler.IntervalTime > 0)
                    {
                        nextRunTime = scheduler.NextRunTime.Value.AddMinutes(scheduler.IntervalTime);
                    }
                    break;

                default:
                    return(Task.FromCanceled(new CancellationToken(true)));
                }

                // expired time
                DateTime?expiredTime = null;
                if (scheduler.ExpiredAfter > 0 && scheduler.RepeatType != SchedulerRepeatType.OneTime)
                {
                    expiredTime = scheduler.NextRunTime.Value.AddMinutes(scheduler.ExpiredAfter);
                }
                // update current scheduler status to running
                SchedulerServices.Update(scheduler.Id, SchedulerStatus.Running, DateTime.Now, nextRunTime, expiredTime);
                // init scheduler type
                var schedulerType = cat_BaseServices.GetById("cat_SchedulerType", scheduler.SchedulerTypeId);
                // init job type
                var jobType = Type.GetType("Web.Core.Framework.{0},Web.Core.Framework".FormatWith(schedulerType.Name));
                if (jobType == null)
                {
                    return(Task.FromCanceled(new CancellationToken(true)));
                }
                // init job
                var job = Activator.CreateInstance(jobType);
                // int job property
                ((BaseTask)job).Id        = scheduler.Id;
                ((BaseTask)job).Name      = scheduler.Name;
                ((BaseTask)job).Arguments = scheduler.Arguments;
                // execute job
                ((BaseTask)job).Excute(scheduler.Arguments);

                // update scheduler status when complete
                SchedulerServices.Update(scheduler.Id, scheduler.RepeatType == SchedulerRepeatType.OneTime
                    ? SchedulerStatus.Completed
                    : SchedulerStatus.Ready);
                return(Task.CompletedTask);
            }
            catch (Exception ex)
            {
                // log error
                SystemLogController.Create(new SystemLogModel("system", "RunScheduler", SystemAction.TrackError,
                                                              SystemLogType.ScheduleError, ex.Message, ex.ToString(), true));
                return(Task.FromException(ex));
            }
        }