[12691] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 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;
|
---|
[14901] | 265 | } else if (task.State != DA.TaskState.Aborted
|
---|
| 266 | && task.State != DA.TaskState.Finished
|
---|
| 267 | && task.State != DA.TaskState.Failed) {
|
---|
| 268 | UpdateTaskState(pm, task, DT.TaskState.Paused, null, null, string.Empty);
|
---|
[12584] | 269 | }
|
---|
| 270 | pm.SubmitChanges();
|
---|
| 271 | });
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | public void RestartTask(Guid taskId) {
|
---|
| 276 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
| 277 | AuthorizationManager.AuthorizeForTask(taskId, Permission.Full);
|
---|
[12857] | 278 | var pm = PersistenceManager;
|
---|
[12691] | 279 | using (new PerformanceLogger("RestartTask")) {
|
---|
[12584] | 280 | var taskDao = pm.TaskDao;
|
---|
| 281 | pm.UseTransaction(() => {
|
---|
| 282 | var task = taskDao.GetById(taskId);
|
---|
| 283 | task.Command = null;
|
---|
| 284 | UpdateTaskState(pm, task, DT.TaskState.Waiting, null, UserManager.CurrentUserId, string.Empty);
|
---|
| 285 | pm.SubmitChanges();
|
---|
| 286 | });
|
---|
| 287 | }
|
---|
| 288 | }
|
---|
[12691] | 289 | #endregion
|
---|
[12584] | 290 |
|
---|
[12691] | 291 | #region Job Methods
|
---|
| 292 | public DT.Job GetJob(Guid id) {
|
---|
[12584] | 293 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12691] | 294 | AuthorizationManager.AuthorizeForJob(id, DT.Permission.Read);
|
---|
[12857] | 295 | var pm = PersistenceManager;
|
---|
[12691] | 296 | using (new PerformanceLogger("GetJob")) {
|
---|
[12584] | 297 | var jobDao = pm.JobDao;
|
---|
| 298 | var jobPermissionDao = pm.JobPermissionDao;
|
---|
| 299 | var taskDao = pm.TaskDao;
|
---|
[12691] | 300 | var currentUserId = UserManager.CurrentUserId;
|
---|
[12584] | 301 | return pm.UseTransaction(() => {
|
---|
| 302 | var job = jobDao.GetById(id).ToDto();
|
---|
| 303 | if (job != null) {
|
---|
| 304 | var statistics = taskDao.GetByJobId(job.Id)
|
---|
| 305 | .GroupBy(x => x.JobId)
|
---|
| 306 | .Select(x => new {
|
---|
| 307 | TotalCount = x.Count(),
|
---|
| 308 | CalculatingCount = x.Count(y => y.State == DA.TaskState.Calculating),
|
---|
| 309 | FinishedCount = x.Count(y => CompletedStates.Contains(y.State))
|
---|
| 310 | }).FirstOrDefault();
|
---|
| 311 | if (statistics != null) {
|
---|
| 312 | job.JobCount = statistics.TotalCount;
|
---|
| 313 | job.CalculatingCount = statistics.CalculatingCount;
|
---|
| 314 | job.FinishedCount = statistics.FinishedCount;
|
---|
| 315 | }
|
---|
[12932] | 316 | job.OwnerUsername = UserManager.GetUserNameById(job.OwnerUserId);
|
---|
[12691] | 317 | if (currentUserId == job.OwnerUserId) {
|
---|
| 318 | job.Permission = Permission.Full;
|
---|
| 319 | } else {
|
---|
| 320 | var jobPermission = jobPermissionDao.GetByJobAndUserId(job.Id, currentUserId);
|
---|
| 321 | job.Permission = jobPermission == null ? Permission.NotAllowed : jobPermission.Permission.ToDto();
|
---|
| 322 | }
|
---|
[12584] | 323 | }
|
---|
| 324 | return job;
|
---|
| 325 | });
|
---|
| 326 | }
|
---|
| 327 | }
|
---|
| 328 |
|
---|
[12691] | 329 | public IEnumerable<DT.Job> GetJobs() {
|
---|
| 330 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12857] | 331 | var pm = PersistenceManager;
|
---|
[12691] | 332 | using (new PerformanceLogger("GetJobs")) {
|
---|
| 333 | var jobDao = pm.JobDao;
|
---|
| 334 | var jobPermissionDao = pm.JobPermissionDao;
|
---|
| 335 | var taskDao = pm.TaskDao;
|
---|
| 336 | var currentUserId = UserManager.CurrentUserId;
|
---|
| 337 | return pm.UseTransaction(() => {
|
---|
| 338 | var jobs = jobDao.GetAll()
|
---|
| 339 | .Where(x => x.OwnerUserId == currentUserId
|
---|
| 340 | || x.JobPermissions.Count(y => y.Permission != DA.Permission.NotAllowed
|
---|
| 341 | && y.GrantedUserId == currentUserId) > 0)
|
---|
| 342 | .Select(x => x.ToDto())
|
---|
| 343 | .ToList();
|
---|
| 344 | var statistics = taskDao.GetAll()
|
---|
| 345 | .GroupBy(x => x.JobId)
|
---|
| 346 | .Select(x => new {
|
---|
| 347 | x.Key,
|
---|
| 348 | TotalCount = x.Count(),
|
---|
| 349 | CalculatingCount = x.Count(y => y.State == DA.TaskState.Calculating),
|
---|
| 350 | FinishedCount = x.Count(y => CompletedStates.Contains(y.State))
|
---|
| 351 | })
|
---|
| 352 | .ToList();
|
---|
| 353 | foreach (var job in jobs) {
|
---|
| 354 | var statistic = statistics.FirstOrDefault(x => x.Key == job.Id);
|
---|
| 355 | if (statistic != null) {
|
---|
| 356 | job.JobCount = statistic.TotalCount;
|
---|
| 357 | job.CalculatingCount = statistic.CalculatingCount;
|
---|
| 358 | job.FinishedCount = statistic.FinishedCount;
|
---|
| 359 | }
|
---|
[12932] | 360 | job.OwnerUsername = UserManager.GetUserNameById(job.OwnerUserId);
|
---|
[12691] | 361 | if (currentUserId == job.OwnerUserId) {
|
---|
| 362 | job.Permission = Permission.Full;
|
---|
| 363 | } else {
|
---|
| 364 | var jobPermission = jobPermissionDao.GetByJobAndUserId(job.Id, currentUserId);
|
---|
| 365 | job.Permission = jobPermission == null ? Permission.NotAllowed : jobPermission.Permission.ToDto();
|
---|
| 366 | }
|
---|
| 367 | }
|
---|
| 368 | return jobs;
|
---|
| 369 | });
|
---|
| 370 | }
|
---|
[12584] | 371 | }
|
---|
| 372 |
|
---|
[12776] | 373 | public Guid AddJob(DT.Job jobDto) {
|
---|
[12691] | 374 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12857] | 375 | var pm = PersistenceManager;
|
---|
[12691] | 376 | using (new PerformanceLogger("AddJob")) {
|
---|
| 377 | var jobDao = pm.JobDao;
|
---|
[12776] | 378 | var userPriorityDao = pm.UserPriorityDao;
|
---|
[12691] | 379 | return pm.UseTransaction(() => {
|
---|
| 380 | jobDto.OwnerUserId = UserManager.CurrentUserId;
|
---|
| 381 | jobDto.DateCreated = DateTime.Now;
|
---|
| 382 | var job = jobDao.Save(jobDto.ToEntity());
|
---|
[12776] | 383 | if (userPriorityDao.GetById(jobDto.OwnerUserId) == null) {
|
---|
| 384 | userPriorityDao.Save(new DA.UserPriority {
|
---|
| 385 | UserId = jobDto.OwnerUserId,
|
---|
| 386 | DateEnqueued = jobDto.DateCreated
|
---|
| 387 | });
|
---|
| 388 | }
|
---|
[12691] | 389 | pm.SubmitChanges();
|
---|
| 390 | return job.JobId;
|
---|
| 391 | });
|
---|
| 392 | }
|
---|
[12584] | 393 | }
|
---|
| 394 |
|
---|
[12691] | 395 | public void UpdateJob(DT.Job jobDto) {
|
---|
| 396 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 397 | AuthorizationManager.AuthorizeForJob(jobDto.Id, DT.Permission.Full);
|
---|
[12857] | 398 | var pm = PersistenceManager;
|
---|
[12691] | 399 | using (new PerformanceLogger("UpdateJob")) {
|
---|
| 400 | bool exists = true;
|
---|
| 401 | var jobDao = pm.JobDao;
|
---|
| 402 | pm.UseTransaction(() => {
|
---|
| 403 | var job = jobDao.GetById(jobDto.Id);
|
---|
| 404 | if (job == null) {
|
---|
| 405 | exists = false;
|
---|
| 406 | job = new DA.Job();
|
---|
| 407 | }
|
---|
| 408 | jobDto.CopyToEntity(job);
|
---|
| 409 | if (!exists) {
|
---|
| 410 | jobDao.Save(job);
|
---|
| 411 | }
|
---|
| 412 | pm.SubmitChanges();
|
---|
| 413 | });
|
---|
| 414 | }
|
---|
[12584] | 415 | }
|
---|
| 416 |
|
---|
[12691] | 417 | public void DeleteJob(Guid jobId) {
|
---|
| 418 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 419 | AuthorizationManager.AuthorizeForJob(jobId, DT.Permission.Full);
|
---|
[12857] | 420 | var pm = PersistenceManager;
|
---|
[12691] | 421 | using (new PerformanceLogger("DeleteJob")) {
|
---|
| 422 | var jobDao = pm.JobDao;
|
---|
| 423 | pm.UseTransaction(() => {
|
---|
[12776] | 424 | // child task will be deleted by db-trigger
|
---|
[12691] | 425 | jobDao.Delete(jobId);
|
---|
| 426 | pm.SubmitChanges();
|
---|
| 427 | });
|
---|
| 428 | }
|
---|
[12584] | 429 | }
|
---|
[12691] | 430 | #endregion
|
---|
[12584] | 431 |
|
---|
[12691] | 432 | #region JobPermission Methods
|
---|
| 433 | public void GrantPermission(Guid jobId, Guid grantedUserId, DT.Permission permission) {
|
---|
| 434 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 435 | AuthorizationManager.AuthorizeForJob(jobId, Permission.Full);
|
---|
[12857] | 436 | var pm = PersistenceManager;
|
---|
[12691] | 437 | using (new PerformanceLogger("GrantPermission")) {
|
---|
| 438 | var jobPermissionDao = pm.JobPermissionDao;
|
---|
| 439 | var currentUserId = UserManager.CurrentUserId;
|
---|
| 440 | pm.UseTransaction(() => {
|
---|
| 441 | jobPermissionDao.SetJobPermission(jobId, currentUserId, grantedUserId, permission.ToEntity());
|
---|
| 442 | pm.SubmitChanges();
|
---|
| 443 | });
|
---|
| 444 | }
|
---|
[12584] | 445 | }
|
---|
| 446 |
|
---|
[12691] | 447 | public void RevokePermission(Guid jobId, Guid grantedUserId) {
|
---|
| 448 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 449 | AuthorizationManager.AuthorizeForJob(jobId, Permission.Full);
|
---|
[12857] | 450 | var pm = PersistenceManager;
|
---|
[12691] | 451 | using (new PerformanceLogger("RevokePermission")) {
|
---|
| 452 | var jobPermissionDao = pm.JobPermissionDao;
|
---|
| 453 | var currentUserId = UserManager.CurrentUserId;
|
---|
| 454 | pm.UseTransaction(() => {
|
---|
| 455 | jobPermissionDao.SetJobPermission(jobId, currentUserId, grantedUserId, DA.Permission.NotAllowed);
|
---|
| 456 | pm.SubmitChanges();
|
---|
| 457 | });
|
---|
| 458 | }
|
---|
[12584] | 459 | }
|
---|
| 460 |
|
---|
| 461 | public IEnumerable<JobPermission> GetJobPermissions(Guid jobId) {
|
---|
[12691] | 462 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 463 | AuthorizationManager.AuthorizeForJob(jobId, Permission.Full);
|
---|
[12857] | 464 | var pm = PersistenceManager;
|
---|
[12691] | 465 | using (new PerformanceLogger("GetJobPermissions")) {
|
---|
| 466 | var jobPermissionDao = pm.JobPermissionDao;
|
---|
| 467 | return pm.UseTransaction(() => jobPermissionDao.GetByJobId(jobId)
|
---|
| 468 | .Select(x => x.ToDto())
|
---|
| 469 | .ToList()
|
---|
| 470 | );
|
---|
| 471 | }
|
---|
[12584] | 472 | }
|
---|
| 473 |
|
---|
[12926] | 474 | // BackwardsCompatibility3.3
|
---|
| 475 | #region Backwards compatible code, remove with 3.4
|
---|
[12584] | 476 | public bool IsAllowedPrivileged() {
|
---|
[12926] | 477 | return true;
|
---|
[12584] | 478 | }
|
---|
[12691] | 479 | #endregion
|
---|
[12926] | 480 | #endregion
|
---|
[12584] | 481 |
|
---|
[12691] | 482 | #region Login Methods
|
---|
| 483 | public void Hello(DT.Slave slaveInfo) {
|
---|
| 484 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Slave);
|
---|
| 485 | if (UserManager.CurrentUser.UserName != "hiveslave") {
|
---|
| 486 | slaveInfo.OwnerUserId = UserManager.CurrentUserId;
|
---|
| 487 | }
|
---|
[12857] | 488 | var pm = PersistenceManager;
|
---|
[12691] | 489 | using (new PerformanceLogger("Hello")) {
|
---|
| 490 | var slaveDao = pm.SlaveDao;
|
---|
| 491 | pm.UseTransaction(() => {
|
---|
| 492 | var slave = slaveDao.GetById(slaveInfo.Id);
|
---|
| 493 | if (slave == null) {
|
---|
| 494 | slaveDao.Save(slaveInfo.ToEntity());
|
---|
| 495 | } else {
|
---|
| 496 | bool oldIsAllowedToCalculate = slave.IsAllowedToCalculate;
|
---|
| 497 | Guid? oldParentResourceId = slave.ParentResourceId;
|
---|
| 498 | slaveInfo.CopyToEntity(slave);
|
---|
| 499 | slave.IsAllowedToCalculate = oldIsAllowedToCalculate;
|
---|
| 500 | slave.ParentResourceId = oldParentResourceId;
|
---|
| 501 | slave.LastHeartbeat = DateTime.Now;
|
---|
| 502 | slave.SlaveState = DA.SlaveState.Idle;
|
---|
| 503 | }
|
---|
| 504 | pm.SubmitChanges();
|
---|
| 505 | });
|
---|
| 506 | }
|
---|
[12584] | 507 | }
|
---|
| 508 |
|
---|
| 509 | public void GoodBye(Guid slaveId) {
|
---|
[12691] | 510 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Slave);
|
---|
[12857] | 511 | var pm = PersistenceManager;
|
---|
[12691] | 512 | using (new PerformanceLogger("GoodBye")) {
|
---|
| 513 | var slaveDao = pm.SlaveDao;
|
---|
| 514 | pm.UseTransaction(() => {
|
---|
| 515 | var slave = slaveDao.GetById(slaveId);
|
---|
| 516 | if (slave != null) {
|
---|
| 517 | slave.SlaveState = DA.SlaveState.Offline;
|
---|
| 518 | pm.SubmitChanges();
|
---|
| 519 | }
|
---|
| 520 | });
|
---|
| 521 | }
|
---|
[12584] | 522 | }
|
---|
[12691] | 523 | #endregion
|
---|
[12584] | 524 |
|
---|
[12691] | 525 | #region Heartbeat Methods
|
---|
[12776] | 526 | public List<MessageContainer> Heartbeat(DT.Heartbeat heartbeat) {
|
---|
[12691] | 527 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Slave);
|
---|
| 528 | List<MessageContainer> result = new List<MessageContainer>();
|
---|
| 529 | try {
|
---|
| 530 | using (new PerformanceLogger("ProcessHeartbeat")) {
|
---|
| 531 | result = HeartbeatManager.ProcessHeartbeat(heartbeat);
|
---|
| 532 | }
|
---|
| 533 | }
|
---|
| 534 | catch (Exception ex) {
|
---|
| 535 | DA.LogFactory.GetLogger(this.GetType().Namespace).Log(string.Format("Exception processing Heartbeat: {0}", ex));
|
---|
| 536 | }
|
---|
| 537 | if (HeuristicLab.Services.Hive.Properties.Settings.Default.TriggerEventManagerInHeartbeat) {
|
---|
| 538 | TriggerEventManager(false);
|
---|
| 539 | }
|
---|
| 540 | return result;
|
---|
[12584] | 541 | }
|
---|
[12691] | 542 | #endregion
|
---|
[12584] | 543 |
|
---|
[12691] | 544 | #region Plugin Methods
|
---|
[12776] | 545 | public DT.Plugin GetPlugin(Guid pluginId) {
|
---|
[12691] | 546 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
[12857] | 547 | var pm = PersistenceManager;
|
---|
[12691] | 548 | using (new PerformanceLogger("GetPlugin")) {
|
---|
| 549 | var pluginDao = pm.PluginDao;
|
---|
| 550 | return pm.UseTransaction(() => pluginDao.GetById(pluginId).ToDto());
|
---|
| 551 | }
|
---|
[12584] | 552 | }
|
---|
| 553 |
|
---|
[12776] | 554 | public Guid AddPlugin(DT.Plugin plugin, List<DT.PluginData> pluginData) {
|
---|
[12691] | 555 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12857] | 556 | var pm = PersistenceManager;
|
---|
[12691] | 557 | using (new PerformanceLogger("AddPlugin")) {
|
---|
| 558 | var pluginDao = pm.PluginDao;
|
---|
| 559 | plugin.UserId = UserManager.CurrentUserId;
|
---|
| 560 | plugin.DateCreated = DateTime.Now;
|
---|
| 561 | return pm.UseTransaction(() => {
|
---|
| 562 | var pluginEntity = pluginDao.GetByHash(plugin.Hash).SingleOrDefault();
|
---|
| 563 | if (pluginEntity != null) {
|
---|
| 564 | throw new FaultException<PluginAlreadyExistsFault>(new PluginAlreadyExistsFault(pluginEntity.PluginId));
|
---|
| 565 | }
|
---|
| 566 | pluginEntity = plugin.ToEntity();
|
---|
| 567 | foreach (var data in pluginData) {
|
---|
[12776] | 568 | data.PluginId = default(Guid); // real id will be assigned from linq2sql
|
---|
[12691] | 569 | pluginEntity.PluginData.Add(data.ToEntity());
|
---|
| 570 | }
|
---|
| 571 | pluginDao.Save(pluginEntity);
|
---|
| 572 | pm.SubmitChanges();
|
---|
| 573 | return pluginEntity.PluginId;
|
---|
| 574 | });
|
---|
| 575 | }
|
---|
[12584] | 576 | }
|
---|
| 577 |
|
---|
[12776] | 578 | public IEnumerable<DT.Plugin> GetPlugins() {
|
---|
[12691] | 579 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
[12857] | 580 | var pm = PersistenceManager;
|
---|
[12691] | 581 | using (new PerformanceLogger("GetPlugins")) {
|
---|
| 582 | var pluginDao = pm.PluginDao;
|
---|
| 583 | return pm.UseTransaction(() => pluginDao.GetAll()
|
---|
| 584 | .Where(x => x.Hash != null)
|
---|
| 585 | .Select(x => x.ToDto())
|
---|
| 586 | .ToList()
|
---|
| 587 | );
|
---|
| 588 | }
|
---|
[12584] | 589 | }
|
---|
| 590 |
|
---|
[12776] | 591 | public IEnumerable<DT.PluginData> GetPluginDatas(List<Guid> pluginIds) {
|
---|
[12691] | 592 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
[12857] | 593 | var pm = PersistenceManager;
|
---|
[12691] | 594 | using (new PerformanceLogger("GetPluginDatas")) {
|
---|
| 595 | var pluginDataDao = pm.PluginDataDao;
|
---|
| 596 | return pm.UseTransaction(() => pluginDataDao.GetAll()
|
---|
| 597 | .Where(x => pluginIds.Contains(x.PluginId))
|
---|
| 598 | .Select(x => x.ToDto())
|
---|
| 599 | .ToList()
|
---|
| 600 | );
|
---|
| 601 | }
|
---|
[12584] | 602 | }
|
---|
[12691] | 603 | #endregion
|
---|
[12584] | 604 |
|
---|
[12691] | 605 | #region ResourcePermission Methods
|
---|
[12584] | 606 | public void GrantResourcePermissions(Guid resourceId, Guid[] grantedUserIds) {
|
---|
[12691] | 607 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12857] | 608 | var pm = PersistenceManager;
|
---|
[12691] | 609 | using (new PerformanceLogger("GrantResourcePermissions")) {
|
---|
| 610 | pm.UseTransaction(() => {
|
---|
| 611 | var resource = AuthorizeForResource(pm, resourceId);
|
---|
| 612 | var resourcePermissions = resource.ResourcePermissions.ToList();
|
---|
| 613 | foreach (var id in grantedUserIds) {
|
---|
| 614 | if (resourcePermissions.All(x => x.GrantedUserId != id)) {
|
---|
| 615 | resource.ResourcePermissions.Add(new DA.ResourcePermission {
|
---|
| 616 | GrantedUserId = id,
|
---|
| 617 | GrantedByUserId = UserManager.CurrentUserId
|
---|
| 618 | });
|
---|
| 619 | }
|
---|
| 620 | }
|
---|
| 621 | pm.SubmitChanges();
|
---|
| 622 | });
|
---|
| 623 | }
|
---|
[12584] | 624 | }
|
---|
| 625 |
|
---|
| 626 | public void RevokeResourcePermissions(Guid resourceId, Guid[] grantedUserIds) {
|
---|
[12691] | 627 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12857] | 628 | var pm = PersistenceManager;
|
---|
[12691] | 629 | using (new PerformanceLogger("RevokeResourcePermissions")) {
|
---|
| 630 | var resourcePermissionDao = pm.ResourcePermissionDao;
|
---|
| 631 | pm.UseTransaction(() => {
|
---|
| 632 | AuthorizeForResource(pm, resourceId);
|
---|
| 633 | resourcePermissionDao.DeleteByResourceAndGrantedUserId(resourceId, grantedUserIds);
|
---|
[12972] | 634 | pm.SubmitChanges();
|
---|
[12691] | 635 | });
|
---|
| 636 | }
|
---|
[12584] | 637 | }
|
---|
| 638 |
|
---|
[12776] | 639 | public IEnumerable<DT.ResourcePermission> GetResourcePermissions(Guid resourceId) {
|
---|
[12691] | 640 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12857] | 641 | var pm = PersistenceManager;
|
---|
[12691] | 642 | using (new PerformanceLogger("GetResourcePermissions")) {
|
---|
| 643 | var resourcePermissionDao = pm.ResourcePermissionDao;
|
---|
| 644 | return pm.UseTransaction(() => resourcePermissionDao.GetByResourceId(resourceId)
|
---|
| 645 | .Select(x => x.ToDto())
|
---|
| 646 | .ToList()
|
---|
| 647 | );
|
---|
| 648 | }
|
---|
[12584] | 649 | }
|
---|
[12691] | 650 | #endregion
|
---|
[12584] | 651 |
|
---|
[12691] | 652 | #region Slave Methods
|
---|
| 653 | public Guid AddSlave(DT.Slave slaveDto) {
|
---|
| 654 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
[12857] | 655 | var pm = PersistenceManager;
|
---|
[12691] | 656 | using (new PerformanceLogger("AddSlave")) {
|
---|
| 657 | var slaveDao = pm.SlaveDao;
|
---|
| 658 | return pm.UseTransaction(() => {
|
---|
| 659 | var slave = slaveDao.Save(slaveDto.ToEntity());
|
---|
| 660 | pm.SubmitChanges();
|
---|
| 661 | return slave.ResourceId;
|
---|
| 662 | });
|
---|
| 663 | }
|
---|
[12584] | 664 | }
|
---|
| 665 |
|
---|
[12691] | 666 | public Guid AddSlaveGroup(DT.SlaveGroup slaveGroupDto) {
|
---|
| 667 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12857] | 668 | var pm = PersistenceManager;
|
---|
[12691] | 669 | using (new PerformanceLogger("AddSlaveGroup")) {
|
---|
| 670 | var slaveGroupDao = pm.SlaveGroupDao;
|
---|
| 671 | return pm.UseTransaction(() => {
|
---|
| 672 | if (slaveGroupDto.Id == Guid.Empty) {
|
---|
| 673 | slaveGroupDto.Id = Guid.NewGuid();
|
---|
| 674 | }
|
---|
| 675 | var slaveGroup = slaveGroupDao.Save(slaveGroupDto.ToEntity());
|
---|
| 676 | pm.SubmitChanges();
|
---|
| 677 | return slaveGroup.ResourceId;
|
---|
| 678 | });
|
---|
| 679 | }
|
---|
[12584] | 680 | }
|
---|
| 681 |
|
---|
[12776] | 682 | public DT.Slave GetSlave(Guid slaveId) {
|
---|
[12691] | 683 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
[12857] | 684 | var pm = PersistenceManager;
|
---|
[12691] | 685 | using (new PerformanceLogger("GetSlave")) {
|
---|
| 686 | var slaveDao = pm.SlaveDao;
|
---|
| 687 | return pm.UseTransaction(() => slaveDao.GetById(slaveId).ToDto());
|
---|
| 688 | }
|
---|
[12584] | 689 | }
|
---|
| 690 |
|
---|
[12776] | 691 | public IEnumerable<DT.Slave> GetSlaves() {
|
---|
[12691] | 692 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 693 | bool isAdministrator = RoleVerifier.IsInRole(HiveRoles.Administrator);
|
---|
[12857] | 694 | var pm = PersistenceManager;
|
---|
[12691] | 695 | using (new PerformanceLogger("GetSlaves")) {
|
---|
| 696 | var slaveDao = pm.SlaveDao;
|
---|
| 697 | var resourcePermissionDao = pm.ResourcePermissionDao;
|
---|
| 698 | var currentUserId = UserManager.CurrentUserId;
|
---|
| 699 | return pm.UseTransaction(() => {
|
---|
| 700 | var resourcePermissions = resourcePermissionDao.GetAll();
|
---|
[13142] | 701 | return slaveDao.GetAll().ToList()
|
---|
[12691] | 702 | .Where(x => isAdministrator
|
---|
| 703 | || x.OwnerUserId == null
|
---|
| 704 | || x.OwnerUserId == currentUserId
|
---|
| 705 | || UserManager.VerifyUser(currentUserId, resourcePermissions
|
---|
| 706 | .Where(y => y.ResourceId == x.ResourceId)
|
---|
| 707 | .Select(z => z.GrantedUserId)
|
---|
| 708 | .ToList())
|
---|
| 709 | )
|
---|
| 710 | .Select(x => x.ToDto())
|
---|
| 711 | .ToList();
|
---|
| 712 | });
|
---|
| 713 | }
|
---|
[12584] | 714 | }
|
---|
| 715 |
|
---|
[12776] | 716 | public IEnumerable<DT.SlaveGroup> GetSlaveGroups() {
|
---|
[12691] | 717 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 718 | bool isAdministrator = RoleVerifier.IsInRole(HiveRoles.Administrator);
|
---|
[12857] | 719 | var pm = PersistenceManager;
|
---|
[12691] | 720 | using (new PerformanceLogger("GetSlaveGroups")) {
|
---|
| 721 | var slaveGroupDao = pm.SlaveGroupDao;
|
---|
| 722 | var resourcePermissionDao = pm.ResourcePermissionDao;
|
---|
| 723 | var currentUserId = UserManager.CurrentUserId;
|
---|
| 724 | return pm.UseTransaction(() => {
|
---|
| 725 | var resourcePermissions = resourcePermissionDao.GetAll();
|
---|
[13142] | 726 | return slaveGroupDao.GetAll().ToList()
|
---|
[12691] | 727 | .Where(x => isAdministrator
|
---|
| 728 | || x.OwnerUserId == null
|
---|
| 729 | || x.OwnerUserId == currentUserId
|
---|
| 730 | || UserManager.VerifyUser(currentUserId, resourcePermissions
|
---|
| 731 | .Where(y => y.ResourceId == x.ResourceId)
|
---|
| 732 | .Select(z => z.GrantedUserId)
|
---|
| 733 | .ToList())
|
---|
| 734 | )
|
---|
| 735 | .Select(x => x.ToDto())
|
---|
| 736 | .ToList();
|
---|
| 737 | });
|
---|
| 738 | }
|
---|
[12584] | 739 | }
|
---|
| 740 |
|
---|
[12691] | 741 | public void UpdateSlave(DT.Slave slaveDto) {
|
---|
| 742 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12857] | 743 | var pm = PersistenceManager;
|
---|
[12691] | 744 | using (new PerformanceLogger("UpdateSlave")) {
|
---|
| 745 | var slaveDao = pm.SlaveDao;
|
---|
| 746 | pm.UseTransaction(() => {
|
---|
| 747 | var slave = slaveDao.GetById(slaveDto.Id);
|
---|
| 748 | if (slave != null) {
|
---|
| 749 | slaveDto.CopyToEntity(slave);
|
---|
| 750 | } else {
|
---|
| 751 | slaveDao.Save(slaveDto.ToEntity());
|
---|
| 752 | }
|
---|
| 753 | pm.SubmitChanges();
|
---|
| 754 | });
|
---|
| 755 | }
|
---|
[12584] | 756 | }
|
---|
| 757 |
|
---|
[12691] | 758 | public void UpdateSlaveGroup(DT.SlaveGroup slaveGroupDto) {
|
---|
| 759 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12857] | 760 | var pm = PersistenceManager;
|
---|
[12691] | 761 | using (new PerformanceLogger("UpdateSlaveGroup")) {
|
---|
| 762 | var slaveGroupDao = pm.SlaveGroupDao;
|
---|
| 763 | pm.UseTransaction(() => {
|
---|
| 764 | var slaveGroup = slaveGroupDao.GetById(slaveGroupDto.Id);
|
---|
| 765 | if (slaveGroup != null) {
|
---|
| 766 | slaveGroupDto.CopyToEntity(slaveGroup);
|
---|
| 767 | } else {
|
---|
| 768 | slaveGroupDao.Save(slaveGroupDto.ToEntity());
|
---|
| 769 | }
|
---|
| 770 | pm.SubmitChanges();
|
---|
| 771 | });
|
---|
| 772 | }
|
---|
[12584] | 773 | }
|
---|
| 774 |
|
---|
| 775 | public void DeleteSlave(Guid slaveId) {
|
---|
[12691] | 776 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 777 | AuthorizationManager.AuthorizeForResourceAdministration(slaveId);
|
---|
[12857] | 778 | var pm = PersistenceManager;
|
---|
[12691] | 779 | using (new PerformanceLogger("DeleteSlave")) {
|
---|
| 780 | var slaveDao = pm.SlaveDao;
|
---|
| 781 | pm.UseTransaction(() => {
|
---|
| 782 | slaveDao.Delete(slaveId);
|
---|
[12972] | 783 | pm.SubmitChanges();
|
---|
[12691] | 784 | });
|
---|
| 785 | }
|
---|
[12584] | 786 | }
|
---|
| 787 |
|
---|
| 788 | public void DeleteSlaveGroup(Guid slaveGroupId) {
|
---|
[12691] | 789 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 790 | AuthorizationManager.AuthorizeForResourceAdministration(slaveGroupId);
|
---|
[12857] | 791 | var pm = PersistenceManager;
|
---|
[12691] | 792 | using (new PerformanceLogger("DeleteSlaveGroup")) {
|
---|
| 793 | var slaveGroupDao = pm.SlaveGroupDao;
|
---|
| 794 | pm.UseTransaction(() => {
|
---|
| 795 | slaveGroupDao.Delete(slaveGroupId);
|
---|
[12972] | 796 | pm.SubmitChanges();
|
---|
[12691] | 797 | });
|
---|
| 798 | }
|
---|
[12584] | 799 | }
|
---|
| 800 |
|
---|
| 801 | public void AddResourceToGroup(Guid slaveGroupId, Guid resourceId) {
|
---|
[12691] | 802 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
[12857] | 803 | var pm = PersistenceManager;
|
---|
[12691] | 804 | using (new PerformanceLogger("AddResourceToGroup")) {
|
---|
| 805 | var resourceDao = pm.ResourceDao;
|
---|
| 806 | pm.UseTransaction(() => {
|
---|
| 807 | var resource = resourceDao.GetById(resourceId);
|
---|
| 808 | resource.ParentResourceId = slaveGroupId;
|
---|
| 809 | pm.SubmitChanges();
|
---|
| 810 | });
|
---|
| 811 | }
|
---|
[12584] | 812 | }
|
---|
| 813 |
|
---|
| 814 | public void RemoveResourceFromGroup(Guid slaveGroupId, Guid resourceId) {
|
---|
[12691] | 815 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
[12857] | 816 | var pm = PersistenceManager;
|
---|
[12691] | 817 | using (new PerformanceLogger("RemoveResourceFromGroup")) {
|
---|
| 818 | var resourceDao = pm.ResourceDao;
|
---|
| 819 | pm.UseTransaction(() => {
|
---|
| 820 | var resource = resourceDao.GetById(resourceId);
|
---|
| 821 | resource.ParentResourceId = null;
|
---|
| 822 | pm.SubmitChanges();
|
---|
| 823 | });
|
---|
| 824 | }
|
---|
[12584] | 825 | }
|
---|
| 826 |
|
---|
| 827 | public Guid GetResourceId(string resourceName) {
|
---|
[12691] | 828 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12857] | 829 | var pm = PersistenceManager;
|
---|
[12691] | 830 | using (new PerformanceLogger("GetResourceId")) {
|
---|
| 831 | var resourceDao = pm.ResourceDao;
|
---|
| 832 | return pm.UseTransaction(() => {
|
---|
| 833 | var resource = resourceDao.GetByName(resourceName);
|
---|
| 834 | return resource != null ? resource.ResourceId : Guid.Empty;
|
---|
| 835 | });
|
---|
| 836 | }
|
---|
[12584] | 837 | }
|
---|
| 838 |
|
---|
| 839 | public void TriggerEventManager(bool force) {
|
---|
[12857] | 840 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
[12691] | 841 | // use a serializable transaction here to ensure not two threads execute this simultaniously (mutex-lock would not work since IIS may use multiple AppDomains)
|
---|
| 842 | bool cleanup;
|
---|
[12857] | 843 | var pm = PersistenceManager;
|
---|
[12691] | 844 | using (new PerformanceLogger("TriggerEventManager")) {
|
---|
| 845 | cleanup = false;
|
---|
| 846 | var lifecycleDao = pm.LifecycleDao;
|
---|
| 847 | pm.UseTransaction(() => {
|
---|
| 848 | var lastLifecycle = lifecycleDao.GetLastLifecycle();
|
---|
| 849 | DateTime lastCleanup = lastLifecycle != null ? lastLifecycle.LastCleanup : DateTime.MinValue;
|
---|
| 850 | if (force || DateTime.Now - lastCleanup > HeuristicLab.Services.Hive.Properties.Settings.Default.CleanupInterval) {
|
---|
| 851 | lifecycleDao.UpdateLifecycle();
|
---|
| 852 | cleanup = true;
|
---|
| 853 | pm.SubmitChanges();
|
---|
| 854 | }
|
---|
| 855 | }, true);
|
---|
| 856 | }
|
---|
| 857 | if (cleanup) {
|
---|
| 858 | EventManager.Cleanup();
|
---|
| 859 | }
|
---|
[12584] | 860 | }
|
---|
| 861 |
|
---|
| 862 | public int GetNewHeartbeatInterval(Guid slaveId) {
|
---|
[12691] | 863 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Slave);
|
---|
[12857] | 864 | var pm = PersistenceManager;
|
---|
[12691] | 865 | using (new PerformanceLogger("GetNewHeartbeatInterval")) {
|
---|
| 866 | var slaveDao = pm.SlaveDao;
|
---|
| 867 | return pm.UseTransaction(() => {
|
---|
| 868 | var slave = slaveDao.GetById(slaveId);
|
---|
| 869 | if (slave != null) {
|
---|
| 870 | return slave.HbInterval;
|
---|
| 871 | }
|
---|
| 872 | return -1;
|
---|
| 873 | });
|
---|
| 874 | }
|
---|
[12584] | 875 | }
|
---|
[12691] | 876 | #endregion
|
---|
[12584] | 877 |
|
---|
[12691] | 878 | #region Downtime Methods
|
---|
| 879 | public Guid AddDowntime(DT.Downtime downtimeDto) {
|
---|
| 880 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 881 | AuthorizationManager.AuthorizeForResourceAdministration(downtimeDto.ResourceId);
|
---|
[12857] | 882 | var pm = PersistenceManager;
|
---|
[12691] | 883 | using (new PerformanceLogger("AddDowntime")) {
|
---|
| 884 | var downtimeDao = pm.DowntimeDao;
|
---|
| 885 | return pm.UseTransaction(() => {
|
---|
| 886 | var downtime = downtimeDao.Save(downtimeDto.ToEntity());
|
---|
| 887 | pm.SubmitChanges();
|
---|
| 888 | return downtime.ResourceId;
|
---|
| 889 | });
|
---|
| 890 | }
|
---|
[12584] | 891 | }
|
---|
| 892 |
|
---|
| 893 | public void DeleteDowntime(Guid downtimeId) {
|
---|
[12691] | 894 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12857] | 895 | var pm = PersistenceManager;
|
---|
[12691] | 896 | using (new PerformanceLogger("DeleteDowntime")) {
|
---|
| 897 | var downtimeDao = pm.DowntimeDao;
|
---|
| 898 | pm.UseTransaction(() => {
|
---|
| 899 | downtimeDao.Delete(downtimeId);
|
---|
| 900 | pm.SubmitChanges();
|
---|
| 901 | });
|
---|
| 902 | }
|
---|
[12584] | 903 | }
|
---|
| 904 |
|
---|
[12691] | 905 | public void UpdateDowntime(DT.Downtime downtimeDto) {
|
---|
| 906 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 907 | AuthorizationManager.AuthorizeForResourceAdministration(downtimeDto.ResourceId);
|
---|
[12857] | 908 | var pm = PersistenceManager;
|
---|
[12691] | 909 | using (new PerformanceLogger("UpdateDowntime")) {
|
---|
| 910 | var downtimeDao = pm.DowntimeDao;
|
---|
| 911 | pm.UseTransaction(() => {
|
---|
| 912 | var downtime = downtimeDao.GetById(downtimeDto.Id);
|
---|
| 913 | if (downtime != null) {
|
---|
| 914 | downtimeDto.CopyToEntity(downtime);
|
---|
| 915 | } else {
|
---|
| 916 | downtimeDao.Save(downtimeDto.ToEntity());
|
---|
| 917 | }
|
---|
| 918 | pm.SubmitChanges();
|
---|
| 919 | });
|
---|
| 920 | }
|
---|
[12584] | 921 | }
|
---|
| 922 |
|
---|
[12776] | 923 | public IEnumerable<DT.Downtime> GetDowntimesForResource(Guid resourceId) {
|
---|
[12691] | 924 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
[12857] | 925 | var pm = PersistenceManager;
|
---|
[12691] | 926 | using (new PerformanceLogger("GetDowntimesForResource")) {
|
---|
| 927 | var downtimeDao = pm.DowntimeDao;
|
---|
| 928 | return pm.UseTransaction(() => downtimeDao.GetByResourceId(resourceId)
|
---|
| 929 | .Select(x => x.ToDto())
|
---|
| 930 | .ToList()
|
---|
| 931 | );
|
---|
| 932 | }
|
---|
[12584] | 933 | }
|
---|
[12691] | 934 | #endregion
|
---|
[12584] | 935 |
|
---|
[12691] | 936 | #region User Methods
|
---|
[12584] | 937 | public string GetUsernameByUserId(Guid userId) {
|
---|
[12691] | 938 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 939 | var user = UserManager.GetUserById(userId);
|
---|
| 940 | return user != null ? user.UserName : null;
|
---|
[12584] | 941 | }
|
---|
| 942 |
|
---|
| 943 | public Guid GetUserIdByUsername(string username) {
|
---|
[12691] | 944 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
| 945 | var user = ServiceLocator.Instance.UserManager.GetUserByName(username);
|
---|
| 946 | return user != null ? (Guid?)user.ProviderUserKey ?? Guid.Empty : Guid.Empty;
|
---|
[12584] | 947 | }
|
---|
[12691] | 948 | #endregion
|
---|
[12584] | 949 |
|
---|
[12691] | 950 | #region UserPriorities Methods
|
---|
[12776] | 951 | public IEnumerable<DT.UserPriority> GetUserPriorities() {
|
---|
[12857] | 952 | var pm = PersistenceManager;
|
---|
[12691] | 953 | using (new PerformanceLogger("GetUserPriorities")) {
|
---|
| 954 | var userPriorityDao = pm.UserPriorityDao;
|
---|
| 955 | return pm.UseTransaction(() => userPriorityDao.GetAll()
|
---|
| 956 | .Select(x => x.ToDto())
|
---|
| 957 | .ToList()
|
---|
| 958 | );
|
---|
| 959 | }
|
---|
[12584] | 960 | }
|
---|
[12691] | 961 | #endregion
|
---|
[12584] | 962 |
|
---|
[12691] | 963 | #region Private Helper Methods
|
---|
| 964 | private void UpdateTaskState(IPersistenceManager pm, DA.Task task, DT.TaskState taskState, Guid? slaveId, Guid? userId, string exception) {
|
---|
| 965 | var stateLogDao = pm.StateLogDao;
|
---|
| 966 | var taskStateEntity = taskState.ToEntity();
|
---|
[14901] | 967 |
|
---|
| 968 | if (taskStateEntity == DA.TaskState.Paused && task.Command == null) {
|
---|
[12857] | 969 | // slave paused and uploaded the task (no user-command) -> set waiting.
|
---|
[12691] | 970 | taskStateEntity = DA.TaskState.Waiting;
|
---|
| 971 | }
|
---|
[14901] | 972 |
|
---|
[12691] | 973 | stateLogDao.Save(new DA.StateLog {
|
---|
| 974 | State = taskStateEntity,
|
---|
| 975 | DateTime = DateTime.Now,
|
---|
| 976 | TaskId = task.TaskId,
|
---|
| 977 | UserId = userId,
|
---|
| 978 | SlaveId = slaveId,
|
---|
| 979 | Exception = exception
|
---|
| 980 | });
|
---|
[14901] | 981 |
|
---|
[12691] | 982 | task.State = taskStateEntity;
|
---|
[14901] | 983 |
|
---|
| 984 | if (task.Command == DA.Command.Pause && task.State == DA.TaskState.Paused
|
---|
| 985 | || task.Command == DA.Command.Abort && task.State == DA.TaskState.Aborted
|
---|
| 986 | || task.Command == DA.Command.Stop && task.State == DA.TaskState.Aborted) {
|
---|
| 987 | task.Command = null;
|
---|
| 988 | }
|
---|
[12691] | 989 | }
|
---|
| 990 |
|
---|
| 991 | private DA.Resource AuthorizeForResource(IPersistenceManager pm, Guid resourceId) {
|
---|
| 992 | var resourceDao = pm.ResourceDao;
|
---|
| 993 | var resource = resourceDao.GetById(resourceId);
|
---|
| 994 | if (resource == null) throw new SecurityException("Not authorized");
|
---|
| 995 | if (resource.OwnerUserId != UserManager.CurrentUserId
|
---|
| 996 | && !RoleVerifier.IsInRole(HiveRoles.Administrator)) {
|
---|
| 997 | throw new SecurityException("Not authorized");
|
---|
| 998 | }
|
---|
| 999 | return resource;
|
---|
| 1000 | }
|
---|
| 1001 | #endregion
|
---|
[12584] | 1002 | }
|
---|
| 1003 | }
|
---|