public void CalculateFutureSchedules(IEnumerable<ScheduleTask> tasks, bool isAppStart = false)
        {
            Guard.ArgumentNotNull(() => tasks);

            using (var scope = new DbContextScope(autoCommit: false))
            {
                var now = DateTime.UtcNow;
                foreach (var task in tasks)
                {
                    task.NextRunUtc = GetNextSchedule(task);
                    if (isAppStart)
                    {
                        task.ProgressPercent = null;
                        task.ProgressMessage = null;
                        if (task.LastEndUtc.GetValueOrDefault() < task.LastStartUtc)
                        {
                            task.LastEndUtc = task.LastStartUtc;
                            task.LastError = T("Admin.System.ScheduleTasks.AbnormalAbort");
                        }
                    }
                    this.UpdateTask(task);
                }

                scope.Commit();
            }
        }
Example #2
0
        /// <summary>
        /// Install permissions
        /// </summary>
        /// <param name="permissionProvider">Permission provider</param>
        public virtual void InstallPermissions(IPermissionProvider permissionProvider)
        {
            using (var scope = new DbContextScope(_permissionRecordRepository.Context, autoDetectChanges: false, autoCommit: false))
            {
                //install new permissions
                var permissions = permissionProvider.GetPermissions();
                foreach (var permission in permissions)
                {
                    var permission1 = GetPermissionRecordBySystemName(permission.SystemName);
                    if (permission1 == null)
                    {
                        //new permission (install it)
                        permission1 = new PermissionRecord()
                        {
                            Name = permission.Name,
                            SystemName = permission.SystemName,
                            Category = permission.Category,
                        };

                        // default customer role mappings
                        var defaultPermissions = permissionProvider.GetDefaultPermissions();
                        foreach (var defaultPermission in defaultPermissions)
                        {
                            var customerRole = _customerService.GetCustomerRoleBySystemName(defaultPermission.CustomerRoleSystemName);
                            if (customerRole == null)
                            {
                                //new role (save it)
                                customerRole = new CustomerRole()
                                {
                                    Name = defaultPermission.CustomerRoleSystemName,
                                    Active = true,
                                    SystemName = defaultPermission.CustomerRoleSystemName
                                };
                                _customerService.InsertCustomerRole(customerRole);
                            }

                            var defaultMappingProvided = (from p in defaultPermission.PermissionRecords
                                                            where p.SystemName == permission1.SystemName
                                                            select p).Any();
                            var mappingExists = (from p in customerRole.PermissionRecords
                                                    where p.SystemName == permission1.SystemName
                                                    select p).Any();
                            if (defaultMappingProvided && !mappingExists)
                            {
                                permission1.CustomerRoles.Add(customerRole);
                            }
                        }

                        //save new permission
                        InsertPermissionRecord(permission1);
                    }
                }

                scope.Commit();
            }
        }