[12691] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15584] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[12691] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
[12584] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[12691] | 25 | using System.Security;
|
---|
| 26 | using System.ServiceModel;
|
---|
[12584] | 27 | using HeuristicLab.Services.Access;
|
---|
| 28 | using HeuristicLab.Services.Hive.DataAccess.Interfaces;
|
---|
| 29 | using HeuristicLab.Services.Hive.DataTransfer;
|
---|
[12691] | 30 | using HeuristicLab.Services.Hive.Manager;
|
---|
[12584] | 31 | using HeuristicLab.Services.Hive.ServiceContracts;
|
---|
| 32 | using DA = HeuristicLab.Services.Hive.DataAccess;
|
---|
| 33 | using DT = HeuristicLab.Services.Hive.DataTransfer;
|
---|
| 34 |
|
---|
| 35 | namespace HeuristicLab.Services.Hive {
|
---|
[12691] | 36 | /// <summary>
|
---|
| 37 | /// Implementation of the Hive service (interface <see cref="IHiveService"/>).
|
---|
| 38 | /// We need 'IgnoreExtensionDataObject' Attribute for the slave to work.
|
---|
| 39 | /// </summary>
|
---|
| 40 | [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, IgnoreExtensionDataObject = true)]
|
---|
| 41 | [HiveOperationContextBehavior]
|
---|
[12861] | 42 | public class HiveService : IHiveService {
|
---|
[12584] | 43 | private static readonly DA.TaskState[] CompletedStates = { DA.TaskState.Finished, DA.TaskState.Aborted, DA.TaskState.Failed };
|
---|
| 44 |
|
---|
| 45 | private IPersistenceManager PersistenceManager {
|
---|
| 46 | get { return ServiceLocator.Instance.PersistenceManager; }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | private IUserManager UserManager {
|
---|
| 50 | get { return ServiceLocator.Instance.UserManager; }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | private IRoleVerifier RoleVerifier {
|
---|
| 54 | get { return ServiceLocator.Instance.RoleVerifier; }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | private IAuthorizationManager AuthorizationManager {
|
---|
| 58 | get { return ServiceLocator.Instance.AuthorizationManager; }
|
---|
| 59 | }
|
---|
[12691] | 60 | private IEventManager EventManager {
|
---|
| 61 | get { return ServiceLocator.Instance.EventManager; }
|
---|
| 62 | }
|
---|
[12861] | 63 | private HeartbeatManager HeartbeatManager {
|
---|
| 64 | get { return ServiceLocator.Instance.HeartbeatManager; }
|
---|
[12691] | 65 | }
|
---|
[12584] | 66 |
|
---|
[12691] | 67 | #region Task Methods
|
---|
[12584] | 68 | public Guid AddTask(DT.Task task, DT.TaskData taskData, IEnumerable<Guid> resourceIds) {
|
---|
| 69 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12857] | 70 | var pm = PersistenceManager;
|
---|
[12691] | 71 | using (new PerformanceLogger("AddTask")) {
|
---|
[12584] | 72 | var taskDao = pm.TaskDao;
|
---|
| 73 | var stateLogDao = pm.StateLogDao;
|
---|
| 74 | var newTask = task.ToEntity();
|
---|
| 75 | newTask.JobData = taskData.ToEntity();
|
---|
| 76 | newTask.JobData.LastUpdate = DateTime.Now;
|
---|
| 77 | newTask.AssignedResources.AddRange(resourceIds.Select(
|
---|
| 78 | x => new DA.AssignedResource {
|
---|
| 79 | ResourceId = x
|
---|
| 80 | }));
|
---|
| 81 | newTask.State = DA.TaskState.Waiting;
|
---|
| 82 | return pm.UseTransaction(() => {
|
---|
| 83 | taskDao.Save(newTask);
|
---|
| 84 | pm.SubmitChanges();
|
---|
| 85 | stateLogDao.Save(new DA.StateLog {
|
---|
| 86 | State = DA.TaskState.Waiting,
|
---|
| 87 | DateTime = DateTime.Now,
|
---|
| 88 | TaskId = newTask.TaskId,
|
---|
| 89 | UserId = UserManager.CurrentUserId,
|
---|
| 90 | SlaveId = null,
|
---|
| 91 | Exception = null
|
---|
| 92 | });
|
---|
| 93 | pm.SubmitChanges();
|
---|
| 94 | return newTask.TaskId;
|
---|
| 95 | }, false, true);
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | public Guid AddChildTask(Guid parentTaskId, DT.Task task, DT.TaskData taskData) {
|
---|
| 100 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 101 | IEnumerable<Guid> resourceIds;
|
---|
[12857] | 102 | var pm = PersistenceManager;
|
---|
[12691] | 103 | using (new PerformanceLogger("AddChildTask")) {
|
---|
[12584] | 104 | var assignedResourceDao = pm.AssignedResourceDao;
|
---|
| 105 | resourceIds = pm.UseTransaction(() => {
|
---|
| 106 | return assignedResourceDao.GetByTaskId(parentTaskId)
|
---|
| 107 | .Select(x => x.ResourceId)
|
---|
| 108 | .ToList();
|
---|
| 109 | });
|
---|
| 110 | }
|
---|
| 111 | task.ParentTaskId = parentTaskId;
|
---|
| 112 | return AddTask(task, taskData, resourceIds);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | public DT.Task GetTask(Guid taskId) {
|
---|
| 116 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
| 117 | AuthorizationManager.AuthorizeForTask(taskId, Permission.Read);
|
---|
[12857] | 118 | var pm = PersistenceManager;
|
---|
[12691] | 119 | using (new PerformanceLogger("GetTask")) {
|
---|
[12584] | 120 | var taskDao = pm.TaskDao;
|
---|
| 121 | return pm.UseTransaction(() => {
|
---|
| 122 | var task = taskDao.GetById(taskId);
|
---|
| 123 | return task.ToDto();
|
---|
| 124 | });
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | public IEnumerable<DT.LightweightTask> GetLightweightJobTasks(Guid jobId) {
|
---|
| 129 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 130 | AuthorizationManager.AuthorizeForJob(jobId, Permission.Read);
|
---|
[12857] | 131 | var pm = PersistenceManager;
|
---|
[12691] | 132 | using (new PerformanceLogger("GetLightweightJobTasks")) {
|
---|
[12584] | 133 | var taskDao = pm.TaskDao;
|
---|
| 134 | return pm.UseTransaction(() => {
|
---|
| 135 | return taskDao.GetByJobId(jobId)
|
---|
[12691] | 136 | .ToList()
|
---|
[12584] | 137 | .Select(x => new DT.LightweightTask {
|
---|
| 138 | Id = x.TaskId,
|
---|
| 139 | ExecutionTime = TimeSpan.FromMilliseconds(x.ExecutionTimeMs),
|
---|
| 140 | ParentTaskId = x.ParentTaskId,
|
---|
| 141 | StateLog = x.StateLogs.OrderBy(y => y.DateTime)
|
---|
| 142 | .Select(z => z.ToDto())
|
---|
| 143 | .ToList(),
|
---|
| 144 | State = x.State.ToDto(),
|
---|
| 145 | Command = x.Command.ToDto(),
|
---|
| 146 | LastTaskDataUpdate = x.JobData.LastUpdate
|
---|
[12691] | 147 | })
|
---|
| 148 | .ToList();
|
---|
[12584] | 149 | }, false, true);
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | public IEnumerable<DT.LightweightTask> GetLightweightJobTasksWithoutStateLog(Guid jobId) {
|
---|
| 154 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 155 | AuthorizationManager.AuthorizeForJob(jobId, Permission.Read);
|
---|
[12857] | 156 | var pm = PersistenceManager;
|
---|
[12691] | 157 | using (new PerformanceLogger("GetLightweightJobTasksWithoutStateLog")) {
|
---|
[12584] | 158 | var taskDao = pm.TaskDao;
|
---|
| 159 | return pm.UseTransaction(() => {
|
---|
| 160 | return taskDao.GetByJobId(jobId)
|
---|
[12691] | 161 | .ToList()
|
---|
[12584] | 162 | .Select(x => new DT.LightweightTask {
|
---|
| 163 | Id = x.TaskId,
|
---|
| 164 | ExecutionTime = TimeSpan.FromMilliseconds(x.ExecutionTimeMs),
|
---|
| 165 | ParentTaskId = x.ParentTaskId,
|
---|
| 166 | StateLog = new List<DT.StateLog>(),
|
---|
| 167 | State = x.State.ToDto(),
|
---|
| 168 | Command = x.Command.ToDto(),
|
---|
| 169 | LastTaskDataUpdate = x.JobData.LastUpdate
|
---|
[12691] | 170 | })
|
---|
| 171 | .ToList();
|
---|
[12584] | 172 | }, false, true);
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | public DT.TaskData GetTaskData(Guid taskId) {
|
---|
| 177 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
| 178 | AuthorizationManager.AuthorizeForTask(taskId, Permission.Read);
|
---|
[12857] | 179 | var pm = PersistenceManager;
|
---|
[12691] | 180 | using (new PerformanceLogger("GetTaskData")) {
|
---|
[12584] | 181 | var taskDataDao = pm.TaskDataDao;
|
---|
| 182 | return pm.UseTransaction(() => taskDataDao.GetById(taskId).ToDto());
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | public void UpdateTask(DT.Task taskDto) {
|
---|
| 187 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
| 188 | AuthorizationManager.AuthorizeForTask(taskDto.Id, Permission.Full);
|
---|
[12857] | 189 | var pm = PersistenceManager;
|
---|
[12691] | 190 | using (new PerformanceLogger("UpdateTask")) {
|
---|
[12584] | 191 | var taskDao = pm.TaskDao;
|
---|
| 192 | pm.UseTransaction(() => {
|
---|
| 193 | var task = taskDao.GetById(taskDto.Id);
|
---|
| 194 | taskDto.CopyToEntity(task);
|
---|
| 195 | pm.SubmitChanges();
|
---|
| 196 | });
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | public void UpdateTaskData(DT.Task taskDto, DT.TaskData taskDataDto) {
|
---|
| 201 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
| 202 | AuthorizationManager.AuthorizeForTask(taskDto.Id, Permission.Full);
|
---|
[12857] | 203 | var pm = PersistenceManager;
|
---|
[12691] | 204 | using (new PerformanceLogger("UpdateTaskData")) {
|
---|
[12584] | 205 | var taskDao = pm.TaskDao;
|
---|
| 206 | var taskDataDao = pm.TaskDataDao;
|
---|
| 207 | pm.UseTransaction(() => {
|
---|
| 208 | var task = taskDao.GetById(taskDto.Id);
|
---|
| 209 | var taskData = taskDataDao.GetById(taskDataDto.TaskId);
|
---|
| 210 | taskDto.CopyToEntity(task);
|
---|
| 211 | taskDataDto.CopyToEntity(taskData);
|
---|
| 212 | taskData.LastUpdate = DateTime.Now;
|
---|
| 213 | pm.SubmitChanges();
|
---|
| 214 | });
|
---|
| 215 | }
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | public DT.Task UpdateTaskState(Guid taskId, DT.TaskState taskState, Guid? slaveId, Guid? userId, string exception) {
|
---|
| 219 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
| 220 | AuthorizationManager.AuthorizeForTask(taskId, Permission.Full);
|
---|
[12857] | 221 | var pm = PersistenceManager;
|
---|
[12691] | 222 | using (new PerformanceLogger("UpdateTaskState")) {
|
---|
[12584] | 223 | var taskDao = pm.TaskDao;
|
---|
| 224 | return pm.UseTransaction(() => {
|
---|
| 225 | var task = taskDao.GetById(taskId);
|
---|
| 226 | UpdateTaskState(pm, task, taskState, slaveId, userId, exception);
|
---|
| 227 | pm.SubmitChanges();
|
---|
| 228 | return task.ToDto();
|
---|
| 229 | });
|
---|
| 230 | }
|
---|
| 231 | }
|
---|
[12691] | 232 | #endregion
|
---|
[12584] | 233 |
|
---|
[12691] | 234 | #region Task Control Methods
|
---|
[12584] | 235 | public void StopTask(Guid taskId) {
|
---|
| 236 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
| 237 | AuthorizationManager.AuthorizeForTask(taskId, Permission.Full);
|
---|
[12857] | 238 | var pm = PersistenceManager;
|
---|
[12691] | 239 | using (new PerformanceLogger("StopTask")) {
|
---|
[12584] | 240 | var taskDao = pm.TaskDao;
|
---|
| 241 | pm.UseTransaction(() => {
|
---|
| 242 | var task = taskDao.GetById(taskId);
|
---|
| 243 | if (task.State == DA.TaskState.Calculating || task.State == DA.TaskState.Transferring) {
|
---|
| 244 | task.Command = DA.Command.Stop;
|
---|
| 245 | } else if (task.State != DA.TaskState.Aborted
|
---|
| 246 | && task.State != DA.TaskState.Finished
|
---|
| 247 | && task.State != DA.TaskState.Failed) {
|
---|
| 248 | UpdateTaskState(pm, task, DT.TaskState.Aborted, null, null, string.Empty);
|
---|
| 249 | }
|
---|
| 250 | pm.SubmitChanges();
|
---|
| 251 | });
|
---|
| 252 | }
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | public void PauseTask(Guid taskId) {
|
---|
| 256 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
| 257 | AuthorizationManager.AuthorizeForTask(taskId, Permission.Full);
|
---|
[12857] | 258 | var pm = PersistenceManager;
|
---|
[12691] | 259 | using (new PerformanceLogger("PauseTask")) {
|
---|
[12584] | 260 | var taskDao = pm.TaskDao;
|
---|
| 261 | pm.UseTransaction(() => {
|
---|
| 262 | var task = taskDao.GetById(taskId);
|
---|
| 263 | if (task.State == DA.TaskState.Calculating || task.State == DA.TaskState.Transferring) {
|
---|
| 264 | task.Command = DA.Command.Pause;
|
---|
[15262] | 265 | } else if (task.State != DA.TaskState.Paused
|
---|
| 266 | && task.State != DA.TaskState.Aborted
|
---|
| 267 | && task.State != DA.TaskState.Finished
|
---|
| 268 | && task.State != DA.TaskState.Failed) {
|
---|
| 269 | UpdateTaskState(pm, task, DT.TaskState.Paused, null, null, string.Empty);
|
---|
[12584] | 270 | }
|
---|
| 271 | pm.SubmitChanges();
|
---|
| 272 | });
|
---|
| 273 | }
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | public void RestartTask(Guid taskId) {
|
---|
| 277 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
| 278 | AuthorizationManager.AuthorizeForTask(taskId, Permission.Full);
|
---|
[12857] | 279 | var pm = PersistenceManager;
|
---|
[12691] | 280 | using (new PerformanceLogger("RestartTask")) {
|
---|
[12584] | 281 | var taskDao = pm.TaskDao;
|
---|
| 282 | pm.UseTransaction(() => {
|
---|
| 283 | var task = taskDao.GetById(taskId);
|
---|
| 284 | task.Command = null;
|
---|
| 285 | UpdateTaskState(pm, task, DT.TaskState.Waiting, null, UserManager.CurrentUserId, string.Empty);
|
---|
| 286 | pm.SubmitChanges();
|
---|
| 287 | });
|
---|
| 288 | }
|
---|
| 289 | }
|
---|
[12691] | 290 | #endregion
|
---|
[12584] | 291 |
|
---|
[12691] | 292 | #region Job Methods
|
---|
| 293 | public DT.Job GetJob(Guid id) {
|
---|
[12584] | 294 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12691] | 295 | AuthorizationManager.AuthorizeForJob(id, DT.Permission.Read);
|
---|
[12857] | 296 | var pm = PersistenceManager;
|
---|
[12691] | 297 | using (new PerformanceLogger("GetJob")) {
|
---|
[12584] | 298 | var jobDao = pm.JobDao;
|
---|
| 299 | var jobPermissionDao = pm.JobPermissionDao;
|
---|
| 300 | var taskDao = pm.TaskDao;
|
---|
[12691] | 301 | var currentUserId = UserManager.CurrentUserId;
|
---|
[12584] | 302 | return pm.UseTransaction(() => {
|
---|
| 303 | var job = jobDao.GetById(id).ToDto();
|
---|
| 304 | if (job != null) {
|
---|
| 305 | var statistics = taskDao.GetByJobId(job.Id)
|
---|
| 306 | .GroupBy(x => x.JobId)
|
---|
| 307 | .Select(x => new {
|
---|
| 308 | TotalCount = x.Count(),
|
---|
| 309 | CalculatingCount = x.Count(y => y.State == DA.TaskState.Calculating),
|
---|
| 310 | FinishedCount = x.Count(y => CompletedStates.Contains(y.State))
|
---|
| 311 | }).FirstOrDefault();
|
---|
| 312 | if (statistics != null) {
|
---|
| 313 | job.JobCount = statistics.TotalCount;
|
---|
| 314 | job.CalculatingCount = statistics.CalculatingCount;
|
---|
| 315 | job.FinishedCount = statistics.FinishedCount;
|
---|
| 316 | }
|
---|
[12962] | 317 | job.OwnerUsername = UserManager.GetUserNameById(job.OwnerUserId);
|
---|
[12691] | 318 | if (currentUserId == job.OwnerUserId) {
|
---|
| 319 | job.Permission = Permission.Full;
|
---|
| 320 | } else {
|
---|
| 321 | var jobPermission = jobPermissionDao.GetByJobAndUserId(job.Id, currentUserId);
|
---|
| 322 | job.Permission = jobPermission == null ? Permission.NotAllowed : jobPermission.Permission.ToDto();
|
---|
| 323 | }
|
---|
[12584] | 324 | }
|
---|
| 325 | return job;
|
---|
| 326 | });
|
---|
| 327 | }
|
---|
| 328 | }
|
---|
| 329 |
|
---|
[12691] | 330 | public IEnumerable<DT.Job> GetJobs() {
|
---|
| 331 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12857] | 332 | var pm = PersistenceManager;
|
---|
[12691] | 333 | using (new PerformanceLogger("GetJobs")) {
|
---|
| 334 | var jobDao = pm.JobDao;
|
---|
| 335 | var jobPermissionDao = pm.JobPermissionDao;
|
---|
| 336 | var taskDao = pm.TaskDao;
|
---|
| 337 | var currentUserId = UserManager.CurrentUserId;
|
---|
| 338 | return pm.UseTransaction(() => {
|
---|
| 339 | var jobs = jobDao.GetAll()
|
---|
| 340 | .Where(x => x.OwnerUserId == currentUserId
|
---|
| 341 | || x.JobPermissions.Count(y => y.Permission != DA.Permission.NotAllowed
|
---|
| 342 | && y.GrantedUserId == currentUserId) > 0)
|
---|
| 343 | .Select(x => x.ToDto())
|
---|
| 344 | .ToList();
|
---|
| 345 | var statistics = taskDao.GetAll()
|
---|
| 346 | .GroupBy(x => x.JobId)
|
---|
| 347 | .Select(x => new {
|
---|
| 348 | x.Key,
|
---|
| 349 | TotalCount = x.Count(),
|
---|
| 350 | CalculatingCount = x.Count(y => y.State == DA.TaskState.Calculating),
|
---|
| 351 | FinishedCount = x.Count(y => CompletedStates.Contains(y.State))
|
---|
| 352 | })
|
---|
| 353 | .ToList();
|
---|
| 354 | foreach (var job in jobs) {
|
---|
| 355 | var statistic = statistics.FirstOrDefault(x => x.Key == job.Id);
|
---|
| 356 | if (statistic != null) {
|
---|
| 357 | job.JobCount = statistic.TotalCount;
|
---|
| 358 | job.CalculatingCount = statistic.CalculatingCount;
|
---|
| 359 | job.FinishedCount = statistic.FinishedCount;
|
---|
| 360 | }
|
---|
[12962] | 361 | job.OwnerUsername = UserManager.GetUserNameById(job.OwnerUserId);
|
---|
[12691] | 362 | if (currentUserId == job.OwnerUserId) {
|
---|
| 363 | job.Permission = Permission.Full;
|
---|
| 364 | } else {
|
---|
| 365 | var jobPermission = jobPermissionDao.GetByJobAndUserId(job.Id, currentUserId);
|
---|
| 366 | job.Permission = jobPermission == null ? Permission.NotAllowed : jobPermission.Permission.ToDto();
|
---|
| 367 | }
|
---|
| 368 | }
|
---|
| 369 | return jobs;
|
---|
| 370 | });
|
---|
| 371 | }
|
---|
[12584] | 372 | }
|
---|
| 373 |
|
---|
[12776] | 374 | public Guid AddJob(DT.Job jobDto) {
|
---|
[12691] | 375 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12857] | 376 | var pm = PersistenceManager;
|
---|
[12691] | 377 | using (new PerformanceLogger("AddJob")) {
|
---|
| 378 | var jobDao = pm.JobDao;
|
---|
[12776] | 379 | var userPriorityDao = pm.UserPriorityDao;
|
---|
[12691] | 380 | return pm.UseTransaction(() => {
|
---|
| 381 | jobDto.OwnerUserId = UserManager.CurrentUserId;
|
---|
| 382 | jobDto.DateCreated = DateTime.Now;
|
---|
| 383 | var job = jobDao.Save(jobDto.ToEntity());
|
---|
[12776] | 384 | if (userPriorityDao.GetById(jobDto.OwnerUserId) == null) {
|
---|
| 385 | userPriorityDao.Save(new DA.UserPriority {
|
---|
| 386 | UserId = jobDto.OwnerUserId,
|
---|
| 387 | DateEnqueued = jobDto.DateCreated
|
---|
| 388 | });
|
---|
| 389 | }
|
---|
[12691] | 390 | pm.SubmitChanges();
|
---|
| 391 | return job.JobId;
|
---|
| 392 | });
|
---|
| 393 | }
|
---|
[12584] | 394 | }
|
---|
| 395 |
|
---|
[12691] | 396 | public void UpdateJob(DT.Job jobDto) {
|
---|
| 397 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 398 | AuthorizationManager.AuthorizeForJob(jobDto.Id, DT.Permission.Full);
|
---|
[12857] | 399 | var pm = PersistenceManager;
|
---|
[12691] | 400 | using (new PerformanceLogger("UpdateJob")) {
|
---|
| 401 | bool exists = true;
|
---|
| 402 | var jobDao = pm.JobDao;
|
---|
| 403 | pm.UseTransaction(() => {
|
---|
| 404 | var job = jobDao.GetById(jobDto.Id);
|
---|
| 405 | if (job == null) {
|
---|
| 406 | exists = false;
|
---|
| 407 | job = new DA.Job();
|
---|
| 408 | }
|
---|
| 409 | jobDto.CopyToEntity(job);
|
---|
| 410 | if (!exists) {
|
---|
| 411 | jobDao.Save(job);
|
---|
| 412 | }
|
---|
| 413 | pm.SubmitChanges();
|
---|
| 414 | });
|
---|
| 415 | }
|
---|
[12584] | 416 | }
|
---|
| 417 |
|
---|
[12691] | 418 | public void DeleteJob(Guid jobId) {
|
---|
| 419 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 420 | AuthorizationManager.AuthorizeForJob(jobId, DT.Permission.Full);
|
---|
[12857] | 421 | var pm = PersistenceManager;
|
---|
[12691] | 422 | using (new PerformanceLogger("DeleteJob")) {
|
---|
| 423 | var jobDao = pm.JobDao;
|
---|
| 424 | pm.UseTransaction(() => {
|
---|
[12776] | 425 | // child task will be deleted by db-trigger
|
---|
[12691] | 426 | jobDao.Delete(jobId);
|
---|
| 427 | pm.SubmitChanges();
|
---|
| 428 | });
|
---|
| 429 | }
|
---|
[12584] | 430 | }
|
---|
[12691] | 431 | #endregion
|
---|
[12584] | 432 |
|
---|
[12691] | 433 | #region JobPermission Methods
|
---|
| 434 | public void GrantPermission(Guid jobId, Guid grantedUserId, DT.Permission permission) {
|
---|
| 435 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 436 | AuthorizationManager.AuthorizeForJob(jobId, Permission.Full);
|
---|
[12857] | 437 | var pm = PersistenceManager;
|
---|
[12691] | 438 | using (new PerformanceLogger("GrantPermission")) {
|
---|
| 439 | var jobPermissionDao = pm.JobPermissionDao;
|
---|
| 440 | var currentUserId = UserManager.CurrentUserId;
|
---|
| 441 | pm.UseTransaction(() => {
|
---|
| 442 | jobPermissionDao.SetJobPermission(jobId, currentUserId, grantedUserId, permission.ToEntity());
|
---|
| 443 | pm.SubmitChanges();
|
---|
| 444 | });
|
---|
| 445 | }
|
---|
[12584] | 446 | }
|
---|
| 447 |
|
---|
[12691] | 448 | public void RevokePermission(Guid jobId, Guid grantedUserId) {
|
---|
| 449 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 450 | AuthorizationManager.AuthorizeForJob(jobId, Permission.Full);
|
---|
[12857] | 451 | var pm = PersistenceManager;
|
---|
[12691] | 452 | using (new PerformanceLogger("RevokePermission")) {
|
---|
| 453 | var jobPermissionDao = pm.JobPermissionDao;
|
---|
| 454 | var currentUserId = UserManager.CurrentUserId;
|
---|
| 455 | pm.UseTransaction(() => {
|
---|
| 456 | jobPermissionDao.SetJobPermission(jobId, currentUserId, grantedUserId, DA.Permission.NotAllowed);
|
---|
| 457 | pm.SubmitChanges();
|
---|
| 458 | });
|
---|
| 459 | }
|
---|
[12584] | 460 | }
|
---|
| 461 |
|
---|
| 462 | public IEnumerable<JobPermission> GetJobPermissions(Guid jobId) {
|
---|
[12691] | 463 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 464 | AuthorizationManager.AuthorizeForJob(jobId, Permission.Full);
|
---|
[12857] | 465 | var pm = PersistenceManager;
|
---|
[12691] | 466 | using (new PerformanceLogger("GetJobPermissions")) {
|
---|
| 467 | var jobPermissionDao = pm.JobPermissionDao;
|
---|
| 468 | return pm.UseTransaction(() => jobPermissionDao.GetByJobId(jobId)
|
---|
| 469 | .Select(x => x.ToDto())
|
---|
| 470 | .ToList()
|
---|
| 471 | );
|
---|
| 472 | }
|
---|
[12584] | 473 | }
|
---|
| 474 |
|
---|
[12963] | 475 | // BackwardsCompatibility3.3
|
---|
| 476 | #region Backwards compatible code, remove with 3.4
|
---|
[12584] | 477 | public bool IsAllowedPrivileged() {
|
---|
[12963] | 478 | return true;
|
---|
[12584] | 479 | }
|
---|
[12691] | 480 | #endregion
|
---|
[12963] | 481 | #endregion
|
---|
[12584] | 482 |
|
---|
[12691] | 483 | #region Login Methods
|
---|
| 484 | public void Hello(DT.Slave slaveInfo) {
|
---|
| 485 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Slave);
|
---|
| 486 | if (UserManager.CurrentUser.UserName != "hiveslave") {
|
---|
| 487 | slaveInfo.OwnerUserId = UserManager.CurrentUserId;
|
---|
| 488 | }
|
---|
[12857] | 489 | var pm = PersistenceManager;
|
---|
[12691] | 490 | using (new PerformanceLogger("Hello")) {
|
---|
| 491 | var slaveDao = pm.SlaveDao;
|
---|
| 492 | pm.UseTransaction(() => {
|
---|
| 493 | var slave = slaveDao.GetById(slaveInfo.Id);
|
---|
| 494 | if (slave == null) {
|
---|
| 495 | slaveDao.Save(slaveInfo.ToEntity());
|
---|
| 496 | } else {
|
---|
| 497 | bool oldIsAllowedToCalculate = slave.IsAllowedToCalculate;
|
---|
| 498 | Guid? oldParentResourceId = slave.ParentResourceId;
|
---|
| 499 | slaveInfo.CopyToEntity(slave);
|
---|
| 500 | slave.IsAllowedToCalculate = oldIsAllowedToCalculate;
|
---|
| 501 | slave.ParentResourceId = oldParentResourceId;
|
---|
| 502 | slave.LastHeartbeat = DateTime.Now;
|
---|
| 503 | slave.SlaveState = DA.SlaveState.Idle;
|
---|
| 504 | }
|
---|
| 505 | pm.SubmitChanges();
|
---|
| 506 | });
|
---|
| 507 | }
|
---|
[12584] | 508 | }
|
---|
| 509 |
|
---|
| 510 | public void GoodBye(Guid slaveId) {
|
---|
[12691] | 511 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Slave);
|
---|
[12857] | 512 | var pm = PersistenceManager;
|
---|
[12691] | 513 | using (new PerformanceLogger("GoodBye")) {
|
---|
| 514 | var slaveDao = pm.SlaveDao;
|
---|
| 515 | pm.UseTransaction(() => {
|
---|
| 516 | var slave = slaveDao.GetById(slaveId);
|
---|
| 517 | if (slave != null) {
|
---|
| 518 | slave.SlaveState = DA.SlaveState.Offline;
|
---|
| 519 | pm.SubmitChanges();
|
---|
| 520 | }
|
---|
| 521 | });
|
---|
| 522 | }
|
---|
[12584] | 523 | }
|
---|
[12691] | 524 | #endregion
|
---|
[12584] | 525 |
|
---|
[12691] | 526 | #region Heartbeat Methods
|
---|
[12776] | 527 | public List<MessageContainer> Heartbeat(DT.Heartbeat heartbeat) {
|
---|
[12691] | 528 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Slave);
|
---|
| 529 | List<MessageContainer> result = new List<MessageContainer>();
|
---|
| 530 | try {
|
---|
| 531 | using (new PerformanceLogger("ProcessHeartbeat")) {
|
---|
| 532 | result = HeartbeatManager.ProcessHeartbeat(heartbeat);
|
---|
| 533 | }
|
---|
| 534 | }
|
---|
| 535 | catch (Exception ex) {
|
---|
| 536 | DA.LogFactory.GetLogger(this.GetType().Namespace).Log(string.Format("Exception processing Heartbeat: {0}", ex));
|
---|
| 537 | }
|
---|
| 538 | if (HeuristicLab.Services.Hive.Properties.Settings.Default.TriggerEventManagerInHeartbeat) {
|
---|
| 539 | TriggerEventManager(false);
|
---|
| 540 | }
|
---|
| 541 | return result;
|
---|
[12584] | 542 | }
|
---|
[12691] | 543 | #endregion
|
---|
[12584] | 544 |
|
---|
[12691] | 545 | #region Plugin Methods
|
---|
[12776] | 546 | public DT.Plugin GetPlugin(Guid pluginId) {
|
---|
[12691] | 547 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
[12857] | 548 | var pm = PersistenceManager;
|
---|
[12691] | 549 | using (new PerformanceLogger("GetPlugin")) {
|
---|
| 550 | var pluginDao = pm.PluginDao;
|
---|
| 551 | return pm.UseTransaction(() => pluginDao.GetById(pluginId).ToDto());
|
---|
| 552 | }
|
---|
[12584] | 553 | }
|
---|
| 554 |
|
---|
[12776] | 555 | public Guid AddPlugin(DT.Plugin plugin, List<DT.PluginData> pluginData) {
|
---|
[12691] | 556 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12857] | 557 | var pm = PersistenceManager;
|
---|
[12691] | 558 | using (new PerformanceLogger("AddPlugin")) {
|
---|
| 559 | var pluginDao = pm.PluginDao;
|
---|
| 560 | plugin.UserId = UserManager.CurrentUserId;
|
---|
| 561 | plugin.DateCreated = DateTime.Now;
|
---|
| 562 | return pm.UseTransaction(() => {
|
---|
| 563 | var pluginEntity = pluginDao.GetByHash(plugin.Hash).SingleOrDefault();
|
---|
| 564 | if (pluginEntity != null) {
|
---|
| 565 | throw new FaultException<PluginAlreadyExistsFault>(new PluginAlreadyExistsFault(pluginEntity.PluginId));
|
---|
| 566 | }
|
---|
| 567 | pluginEntity = plugin.ToEntity();
|
---|
| 568 | foreach (var data in pluginData) {
|
---|
[12776] | 569 | data.PluginId = default(Guid); // real id will be assigned from linq2sql
|
---|
[12691] | 570 | pluginEntity.PluginData.Add(data.ToEntity());
|
---|
| 571 | }
|
---|
| 572 | pluginDao.Save(pluginEntity);
|
---|
| 573 | pm.SubmitChanges();
|
---|
| 574 | return pluginEntity.PluginId;
|
---|
| 575 | });
|
---|
| 576 | }
|
---|
[12584] | 577 | }
|
---|
| 578 |
|
---|
[12776] | 579 | public IEnumerable<DT.Plugin> GetPlugins() {
|
---|
[12691] | 580 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
[12857] | 581 | var pm = PersistenceManager;
|
---|
[12691] | 582 | using (new PerformanceLogger("GetPlugins")) {
|
---|
| 583 | var pluginDao = pm.PluginDao;
|
---|
| 584 | return pm.UseTransaction(() => pluginDao.GetAll()
|
---|
| 585 | .Where(x => x.Hash != null)
|
---|
| 586 | .Select(x => x.ToDto())
|
---|
| 587 | .ToList()
|
---|
| 588 | );
|
---|
| 589 | }
|
---|
[12584] | 590 | }
|
---|
| 591 |
|
---|
[12776] | 592 | public IEnumerable<DT.PluginData> GetPluginDatas(List<Guid> pluginIds) {
|
---|
[12691] | 593 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
[12857] | 594 | var pm = PersistenceManager;
|
---|
[12691] | 595 | using (new PerformanceLogger("GetPluginDatas")) {
|
---|
| 596 | var pluginDataDao = pm.PluginDataDao;
|
---|
| 597 | return pm.UseTransaction(() => pluginDataDao.GetAll()
|
---|
| 598 | .Where(x => pluginIds.Contains(x.PluginId))
|
---|
| 599 | .Select(x => x.ToDto())
|
---|
| 600 | .ToList()
|
---|
| 601 | );
|
---|
| 602 | }
|
---|
[12584] | 603 | }
|
---|
[12691] | 604 | #endregion
|
---|
[12584] | 605 |
|
---|
[12691] | 606 | #region ResourcePermission Methods
|
---|
[12584] | 607 | public void GrantResourcePermissions(Guid resourceId, Guid[] grantedUserIds) {
|
---|
[12691] | 608 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12857] | 609 | var pm = PersistenceManager;
|
---|
[12691] | 610 | using (new PerformanceLogger("GrantResourcePermissions")) {
|
---|
| 611 | pm.UseTransaction(() => {
|
---|
| 612 | var resource = AuthorizeForResource(pm, resourceId);
|
---|
| 613 | var resourcePermissions = resource.ResourcePermissions.ToList();
|
---|
| 614 | foreach (var id in grantedUserIds) {
|
---|
| 615 | if (resourcePermissions.All(x => x.GrantedUserId != id)) {
|
---|
| 616 | resource.ResourcePermissions.Add(new DA.ResourcePermission {
|
---|
| 617 | GrantedUserId = id,
|
---|
| 618 | GrantedByUserId = UserManager.CurrentUserId
|
---|
| 619 | });
|
---|
| 620 | }
|
---|
| 621 | }
|
---|
| 622 | pm.SubmitChanges();
|
---|
| 623 | });
|
---|
| 624 | }
|
---|
[12584] | 625 | }
|
---|
| 626 |
|
---|
| 627 | public void RevokeResourcePermissions(Guid resourceId, Guid[] grantedUserIds) {
|
---|
[12691] | 628 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12857] | 629 | var pm = PersistenceManager;
|
---|
[12691] | 630 | using (new PerformanceLogger("RevokeResourcePermissions")) {
|
---|
| 631 | var resourcePermissionDao = pm.ResourcePermissionDao;
|
---|
| 632 | pm.UseTransaction(() => {
|
---|
| 633 | AuthorizeForResource(pm, resourceId);
|
---|
| 634 | resourcePermissionDao.DeleteByResourceAndGrantedUserId(resourceId, grantedUserIds);
|
---|
[13007] | 635 | pm.SubmitChanges();
|
---|
[12691] | 636 | });
|
---|
| 637 | }
|
---|
[12584] | 638 | }
|
---|
| 639 |
|
---|
[12776] | 640 | public IEnumerable<DT.ResourcePermission> GetResourcePermissions(Guid resourceId) {
|
---|
[12691] | 641 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12857] | 642 | var pm = PersistenceManager;
|
---|
[12691] | 643 | using (new PerformanceLogger("GetResourcePermissions")) {
|
---|
| 644 | var resourcePermissionDao = pm.ResourcePermissionDao;
|
---|
| 645 | return pm.UseTransaction(() => resourcePermissionDao.GetByResourceId(resourceId)
|
---|
| 646 | .Select(x => x.ToDto())
|
---|
| 647 | .ToList()
|
---|
| 648 | );
|
---|
| 649 | }
|
---|
[12584] | 650 | }
|
---|
[12691] | 651 | #endregion
|
---|
[12584] | 652 |
|
---|
[12691] | 653 | #region Slave Methods
|
---|
| 654 | public Guid AddSlave(DT.Slave slaveDto) {
|
---|
| 655 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
[12857] | 656 | var pm = PersistenceManager;
|
---|
[12691] | 657 | using (new PerformanceLogger("AddSlave")) {
|
---|
| 658 | var slaveDao = pm.SlaveDao;
|
---|
| 659 | return pm.UseTransaction(() => {
|
---|
| 660 | var slave = slaveDao.Save(slaveDto.ToEntity());
|
---|
| 661 | pm.SubmitChanges();
|
---|
| 662 | return slave.ResourceId;
|
---|
| 663 | });
|
---|
| 664 | }
|
---|
[12584] | 665 | }
|
---|
| 666 |
|
---|
[12691] | 667 | public Guid AddSlaveGroup(DT.SlaveGroup slaveGroupDto) {
|
---|
| 668 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12857] | 669 | var pm = PersistenceManager;
|
---|
[12691] | 670 | using (new PerformanceLogger("AddSlaveGroup")) {
|
---|
| 671 | var slaveGroupDao = pm.SlaveGroupDao;
|
---|
| 672 | return pm.UseTransaction(() => {
|
---|
| 673 | if (slaveGroupDto.Id == Guid.Empty) {
|
---|
| 674 | slaveGroupDto.Id = Guid.NewGuid();
|
---|
| 675 | }
|
---|
| 676 | var slaveGroup = slaveGroupDao.Save(slaveGroupDto.ToEntity());
|
---|
| 677 | pm.SubmitChanges();
|
---|
| 678 | return slaveGroup.ResourceId;
|
---|
| 679 | });
|
---|
| 680 | }
|
---|
[12584] | 681 | }
|
---|
| 682 |
|
---|
[12776] | 683 | public DT.Slave GetSlave(Guid slaveId) {
|
---|
[12691] | 684 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
[12857] | 685 | var pm = PersistenceManager;
|
---|
[12691] | 686 | using (new PerformanceLogger("GetSlave")) {
|
---|
| 687 | var slaveDao = pm.SlaveDao;
|
---|
| 688 | return pm.UseTransaction(() => slaveDao.GetById(slaveId).ToDto());
|
---|
| 689 | }
|
---|
[12584] | 690 | }
|
---|
| 691 |
|
---|
[12776] | 692 | public IEnumerable<DT.Slave> GetSlaves() {
|
---|
[12691] | 693 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 694 | bool isAdministrator = RoleVerifier.IsInRole(HiveRoles.Administrator);
|
---|
[12857] | 695 | var pm = PersistenceManager;
|
---|
[12691] | 696 | using (new PerformanceLogger("GetSlaves")) {
|
---|
| 697 | var slaveDao = pm.SlaveDao;
|
---|
| 698 | var resourcePermissionDao = pm.ResourcePermissionDao;
|
---|
| 699 | var currentUserId = UserManager.CurrentUserId;
|
---|
| 700 | return pm.UseTransaction(() => {
|
---|
| 701 | var resourcePermissions = resourcePermissionDao.GetAll();
|
---|
[13166] | 702 | return slaveDao.GetAll().ToList()
|
---|
[12691] | 703 | .Where(x => isAdministrator
|
---|
| 704 | || x.OwnerUserId == null
|
---|
| 705 | || x.OwnerUserId == currentUserId
|
---|
| 706 | || UserManager.VerifyUser(currentUserId, resourcePermissions
|
---|
| 707 | .Where(y => y.ResourceId == x.ResourceId)
|
---|
| 708 | .Select(z => z.GrantedUserId)
|
---|
| 709 | .ToList())
|
---|
| 710 | )
|
---|
| 711 | .Select(x => x.ToDto())
|
---|
| 712 | .ToList();
|
---|
| 713 | });
|
---|
| 714 | }
|
---|
[12584] | 715 | }
|
---|
| 716 |
|
---|
[12776] | 717 | public IEnumerable<DT.SlaveGroup> GetSlaveGroups() {
|
---|
[12691] | 718 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 719 | bool isAdministrator = RoleVerifier.IsInRole(HiveRoles.Administrator);
|
---|
[12857] | 720 | var pm = PersistenceManager;
|
---|
[12691] | 721 | using (new PerformanceLogger("GetSlaveGroups")) {
|
---|
| 722 | var slaveGroupDao = pm.SlaveGroupDao;
|
---|
| 723 | var resourcePermissionDao = pm.ResourcePermissionDao;
|
---|
| 724 | var currentUserId = UserManager.CurrentUserId;
|
---|
| 725 | return pm.UseTransaction(() => {
|
---|
| 726 | var resourcePermissions = resourcePermissionDao.GetAll();
|
---|
[13166] | 727 | return slaveGroupDao.GetAll().ToList()
|
---|
[12691] | 728 | .Where(x => isAdministrator
|
---|
| 729 | || x.OwnerUserId == null
|
---|
| 730 | || x.OwnerUserId == currentUserId
|
---|
| 731 | || UserManager.VerifyUser(currentUserId, resourcePermissions
|
---|
| 732 | .Where(y => y.ResourceId == x.ResourceId)
|
---|
| 733 | .Select(z => z.GrantedUserId)
|
---|
| 734 | .ToList())
|
---|
| 735 | )
|
---|
| 736 | .Select(x => x.ToDto())
|
---|
| 737 | .ToList();
|
---|
| 738 | });
|
---|
| 739 | }
|
---|
[12584] | 740 | }
|
---|
| 741 |
|
---|
[12691] | 742 | public void UpdateSlave(DT.Slave slaveDto) {
|
---|
| 743 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12857] | 744 | var pm = PersistenceManager;
|
---|
[12691] | 745 | using (new PerformanceLogger("UpdateSlave")) {
|
---|
| 746 | var slaveDao = pm.SlaveDao;
|
---|
| 747 | pm.UseTransaction(() => {
|
---|
| 748 | var slave = slaveDao.GetById(slaveDto.Id);
|
---|
| 749 | if (slave != null) {
|
---|
| 750 | slaveDto.CopyToEntity(slave);
|
---|
| 751 | } else {
|
---|
| 752 | slaveDao.Save(slaveDto.ToEntity());
|
---|
| 753 | }
|
---|
| 754 | pm.SubmitChanges();
|
---|
| 755 | });
|
---|
| 756 | }
|
---|
[12584] | 757 | }
|
---|
| 758 |
|
---|
[12691] | 759 | public void UpdateSlaveGroup(DT.SlaveGroup slaveGroupDto) {
|
---|
| 760 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12857] | 761 | var pm = PersistenceManager;
|
---|
[12691] | 762 | using (new PerformanceLogger("UpdateSlaveGroup")) {
|
---|
| 763 | var slaveGroupDao = pm.SlaveGroupDao;
|
---|
| 764 | pm.UseTransaction(() => {
|
---|
| 765 | var slaveGroup = slaveGroupDao.GetById(slaveGroupDto.Id);
|
---|
| 766 | if (slaveGroup != null) {
|
---|
| 767 | slaveGroupDto.CopyToEntity(slaveGroup);
|
---|
| 768 | } else {
|
---|
| 769 | slaveGroupDao.Save(slaveGroupDto.ToEntity());
|
---|
| 770 | }
|
---|
| 771 | pm.SubmitChanges();
|
---|
| 772 | });
|
---|
| 773 | }
|
---|
[12584] | 774 | }
|
---|
| 775 |
|
---|
| 776 | public void DeleteSlave(Guid slaveId) {
|
---|
[12691] | 777 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 778 | AuthorizationManager.AuthorizeForResourceAdministration(slaveId);
|
---|
[12857] | 779 | var pm = PersistenceManager;
|
---|
[12691] | 780 | using (new PerformanceLogger("DeleteSlave")) {
|
---|
| 781 | var slaveDao = pm.SlaveDao;
|
---|
| 782 | pm.UseTransaction(() => {
|
---|
| 783 | slaveDao.Delete(slaveId);
|
---|
[13007] | 784 | pm.SubmitChanges();
|
---|
[12691] | 785 | });
|
---|
| 786 | }
|
---|
[12584] | 787 | }
|
---|
| 788 |
|
---|
| 789 | public void DeleteSlaveGroup(Guid slaveGroupId) {
|
---|
[12691] | 790 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 791 | AuthorizationManager.AuthorizeForResourceAdministration(slaveGroupId);
|
---|
[12857] | 792 | var pm = PersistenceManager;
|
---|
[12691] | 793 | using (new PerformanceLogger("DeleteSlaveGroup")) {
|
---|
| 794 | var slaveGroupDao = pm.SlaveGroupDao;
|
---|
| 795 | pm.UseTransaction(() => {
|
---|
| 796 | slaveGroupDao.Delete(slaveGroupId);
|
---|
[13007] | 797 | pm.SubmitChanges();
|
---|
[12691] | 798 | });
|
---|
| 799 | }
|
---|
[12584] | 800 | }
|
---|
| 801 |
|
---|
| 802 | public void AddResourceToGroup(Guid slaveGroupId, Guid resourceId) {
|
---|
[12691] | 803 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
[12857] | 804 | var pm = PersistenceManager;
|
---|
[12691] | 805 | using (new PerformanceLogger("AddResourceToGroup")) {
|
---|
| 806 | var resourceDao = pm.ResourceDao;
|
---|
| 807 | pm.UseTransaction(() => {
|
---|
| 808 | var resource = resourceDao.GetById(resourceId);
|
---|
| 809 | resource.ParentResourceId = slaveGroupId;
|
---|
| 810 | pm.SubmitChanges();
|
---|
| 811 | });
|
---|
| 812 | }
|
---|
[12584] | 813 | }
|
---|
| 814 |
|
---|
| 815 | public void RemoveResourceFromGroup(Guid slaveGroupId, Guid resourceId) {
|
---|
[12691] | 816 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
[12857] | 817 | var pm = PersistenceManager;
|
---|
[12691] | 818 | using (new PerformanceLogger("RemoveResourceFromGroup")) {
|
---|
| 819 | var resourceDao = pm.ResourceDao;
|
---|
| 820 | pm.UseTransaction(() => {
|
---|
| 821 | var resource = resourceDao.GetById(resourceId);
|
---|
| 822 | resource.ParentResourceId = null;
|
---|
| 823 | pm.SubmitChanges();
|
---|
| 824 | });
|
---|
| 825 | }
|
---|
[12584] | 826 | }
|
---|
| 827 |
|
---|
| 828 | public Guid GetResourceId(string resourceName) {
|
---|
[12691] | 829 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12857] | 830 | var pm = PersistenceManager;
|
---|
[12691] | 831 | using (new PerformanceLogger("GetResourceId")) {
|
---|
| 832 | var resourceDao = pm.ResourceDao;
|
---|
| 833 | return pm.UseTransaction(() => {
|
---|
| 834 | var resource = resourceDao.GetByName(resourceName);
|
---|
| 835 | return resource != null ? resource.ResourceId : Guid.Empty;
|
---|
| 836 | });
|
---|
| 837 | }
|
---|
[12584] | 838 | }
|
---|
| 839 |
|
---|
| 840 | public void TriggerEventManager(bool force) {
|
---|
[12857] | 841 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
[12691] | 842 | // use a serializable transaction here to ensure not two threads execute this simultaniously (mutex-lock would not work since IIS may use multiple AppDomains)
|
---|
| 843 | bool cleanup;
|
---|
[12857] | 844 | var pm = PersistenceManager;
|
---|
[12691] | 845 | using (new PerformanceLogger("TriggerEventManager")) {
|
---|
| 846 | cleanup = false;
|
---|
| 847 | var lifecycleDao = pm.LifecycleDao;
|
---|
| 848 | pm.UseTransaction(() => {
|
---|
| 849 | var lastLifecycle = lifecycleDao.GetLastLifecycle();
|
---|
| 850 | DateTime lastCleanup = lastLifecycle != null ? lastLifecycle.LastCleanup : DateTime.MinValue;
|
---|
| 851 | if (force || DateTime.Now - lastCleanup > HeuristicLab.Services.Hive.Properties.Settings.Default.CleanupInterval) {
|
---|
| 852 | lifecycleDao.UpdateLifecycle();
|
---|
| 853 | cleanup = true;
|
---|
| 854 | pm.SubmitChanges();
|
---|
| 855 | }
|
---|
| 856 | }, true);
|
---|
| 857 | }
|
---|
| 858 | if (cleanup) {
|
---|
| 859 | EventManager.Cleanup();
|
---|
| 860 | }
|
---|
[12584] | 861 | }
|
---|
| 862 |
|
---|
| 863 | public int GetNewHeartbeatInterval(Guid slaveId) {
|
---|
[12691] | 864 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Slave);
|
---|
[12857] | 865 | var pm = PersistenceManager;
|
---|
[12691] | 866 | using (new PerformanceLogger("GetNewHeartbeatInterval")) {
|
---|
| 867 | var slaveDao = pm.SlaveDao;
|
---|
| 868 | return pm.UseTransaction(() => {
|
---|
| 869 | var slave = slaveDao.GetById(slaveId);
|
---|
| 870 | if (slave != null) {
|
---|
| 871 | return slave.HbInterval;
|
---|
| 872 | }
|
---|
| 873 | return -1;
|
---|
| 874 | });
|
---|
| 875 | }
|
---|
[12584] | 876 | }
|
---|
[12691] | 877 | #endregion
|
---|
[12584] | 878 |
|
---|
[12691] | 879 | #region Downtime Methods
|
---|
| 880 | public Guid AddDowntime(DT.Downtime downtimeDto) {
|
---|
| 881 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 882 | AuthorizationManager.AuthorizeForResourceAdministration(downtimeDto.ResourceId);
|
---|
[12857] | 883 | var pm = PersistenceManager;
|
---|
[12691] | 884 | using (new PerformanceLogger("AddDowntime")) {
|
---|
| 885 | var downtimeDao = pm.DowntimeDao;
|
---|
| 886 | return pm.UseTransaction(() => {
|
---|
| 887 | var downtime = downtimeDao.Save(downtimeDto.ToEntity());
|
---|
| 888 | pm.SubmitChanges();
|
---|
| 889 | return downtime.ResourceId;
|
---|
| 890 | });
|
---|
| 891 | }
|
---|
[12584] | 892 | }
|
---|
| 893 |
|
---|
| 894 | public void DeleteDowntime(Guid downtimeId) {
|
---|
[12691] | 895 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12857] | 896 | var pm = PersistenceManager;
|
---|
[12691] | 897 | using (new PerformanceLogger("DeleteDowntime")) {
|
---|
| 898 | var downtimeDao = pm.DowntimeDao;
|
---|
| 899 | pm.UseTransaction(() => {
|
---|
| 900 | downtimeDao.Delete(downtimeId);
|
---|
| 901 | pm.SubmitChanges();
|
---|
| 902 | });
|
---|
| 903 | }
|
---|
[12584] | 904 | }
|
---|
| 905 |
|
---|
[12691] | 906 | public void UpdateDowntime(DT.Downtime downtimeDto) {
|
---|
| 907 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 908 | AuthorizationManager.AuthorizeForResourceAdministration(downtimeDto.ResourceId);
|
---|
[12857] | 909 | var pm = PersistenceManager;
|
---|
[12691] | 910 | using (new PerformanceLogger("UpdateDowntime")) {
|
---|
| 911 | var downtimeDao = pm.DowntimeDao;
|
---|
| 912 | pm.UseTransaction(() => {
|
---|
| 913 | var downtime = downtimeDao.GetById(downtimeDto.Id);
|
---|
| 914 | if (downtime != null) {
|
---|
| 915 | downtimeDto.CopyToEntity(downtime);
|
---|
| 916 | } else {
|
---|
| 917 | downtimeDao.Save(downtimeDto.ToEntity());
|
---|
| 918 | }
|
---|
| 919 | pm.SubmitChanges();
|
---|
| 920 | });
|
---|
| 921 | }
|
---|
[12584] | 922 | }
|
---|
| 923 |
|
---|
[12776] | 924 | public IEnumerable<DT.Downtime> GetDowntimesForResource(Guid resourceId) {
|
---|
[12691] | 925 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12857] | 926 | var pm = PersistenceManager;
|
---|
[12691] | 927 | using (new PerformanceLogger("GetDowntimesForResource")) {
|
---|
| 928 | var downtimeDao = pm.DowntimeDao;
|
---|
| 929 | return pm.UseTransaction(() => downtimeDao.GetByResourceId(resourceId)
|
---|
| 930 | .Select(x => x.ToDto())
|
---|
| 931 | .ToList()
|
---|
| 932 | );
|
---|
| 933 | }
|
---|
[12584] | 934 | }
|
---|
[12691] | 935 | #endregion
|
---|
[12584] | 936 |
|
---|
[12691] | 937 | #region User Methods
|
---|
[12584] | 938 | public string GetUsernameByUserId(Guid userId) {
|
---|
[12691] | 939 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 940 | var user = UserManager.GetUserById(userId);
|
---|
| 941 | return user != null ? user.UserName : null;
|
---|
[12584] | 942 | }
|
---|
| 943 |
|
---|
| 944 | public Guid GetUserIdByUsername(string username) {
|
---|
[12691] | 945 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 946 | var user = ServiceLocator.Instance.UserManager.GetUserByName(username);
|
---|
| 947 | return user != null ? (Guid?)user.ProviderUserKey ?? Guid.Empty : Guid.Empty;
|
---|
[12584] | 948 | }
|
---|
[12691] | 949 | #endregion
|
---|
[12584] | 950 |
|
---|
[12691] | 951 | #region UserPriorities Methods
|
---|
[12776] | 952 | public IEnumerable<DT.UserPriority> GetUserPriorities() {
|
---|
[12857] | 953 | var pm = PersistenceManager;
|
---|
[12691] | 954 | using (new PerformanceLogger("GetUserPriorities")) {
|
---|
| 955 | var userPriorityDao = pm.UserPriorityDao;
|
---|
| 956 | return pm.UseTransaction(() => userPriorityDao.GetAll()
|
---|
| 957 | .Select(x => x.ToDto())
|
---|
| 958 | .ToList()
|
---|
| 959 | );
|
---|
| 960 | }
|
---|
[12584] | 961 | }
|
---|
[12691] | 962 | #endregion
|
---|
[12584] | 963 |
|
---|
[12691] | 964 | #region Private Helper Methods
|
---|
| 965 | private void UpdateTaskState(IPersistenceManager pm, DA.Task task, DT.TaskState taskState, Guid? slaveId, Guid? userId, string exception) {
|
---|
| 966 | var stateLogDao = pm.StateLogDao;
|
---|
| 967 | var taskStateEntity = taskState.ToEntity();
|
---|
[15262] | 968 |
|
---|
[15385] | 969 | if (task.State == DA.TaskState.Transferring && taskStateEntity == DA.TaskState.Paused && task.Command == null) {
|
---|
[12857] | 970 | // slave paused and uploaded the task (no user-command) -> set waiting.
|
---|
[12691] | 971 | taskStateEntity = DA.TaskState.Waiting;
|
---|
| 972 | }
|
---|
[15262] | 973 |
|
---|
[12691] | 974 | stateLogDao.Save(new DA.StateLog {
|
---|
| 975 | State = taskStateEntity,
|
---|
| 976 | DateTime = DateTime.Now,
|
---|
| 977 | TaskId = task.TaskId,
|
---|
| 978 | UserId = userId,
|
---|
| 979 | SlaveId = slaveId,
|
---|
| 980 | Exception = exception
|
---|
| 981 | });
|
---|
[15262] | 982 |
|
---|
[12691] | 983 | task.State = taskStateEntity;
|
---|
[15262] | 984 |
|
---|
| 985 | if (task.Command == DA.Command.Pause && task.State == DA.TaskState.Paused
|
---|
| 986 | || task.Command == DA.Command.Abort && task.State == DA.TaskState.Aborted
|
---|
| 987 | || task.Command == DA.Command.Stop && task.State == DA.TaskState.Aborted) {
|
---|
| 988 | task.Command = null;
|
---|
| 989 | }
|
---|
[12691] | 990 | }
|
---|
| 991 |
|
---|
| 992 | private DA.Resource AuthorizeForResource(IPersistenceManager pm, Guid resourceId) {
|
---|
| 993 | var resourceDao = pm.ResourceDao;
|
---|
| 994 | var resource = resourceDao.GetById(resourceId);
|
---|
| 995 | if (resource == null) throw new SecurityException("Not authorized");
|
---|
| 996 | if (resource.OwnerUserId != UserManager.CurrentUserId
|
---|
| 997 | && !RoleVerifier.IsInRole(HiveRoles.Administrator)) {
|
---|
| 998 | throw new SecurityException("Not authorized");
|
---|
| 999 | }
|
---|
| 1000 | return resource;
|
---|
| 1001 | }
|
---|
| 1002 | #endregion
|
---|
[12584] | 1003 | }
|
---|
| 1004 | }
|
---|