1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.ServiceModel;
|
---|
26 | using HeuristicLab.Services.Hive.DataTransfer;
|
---|
27 | using HeuristicLab.Services.Hive.ServiceContracts;
|
---|
28 | using DA = HeuristicLab.Services.Hive.DataAccess;
|
---|
29 | using DT = HeuristicLab.Services.Hive.DataTransfer;
|
---|
30 |
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Services.Hive {
|
---|
33 |
|
---|
34 | /// <summary>
|
---|
35 | /// Implementation of the Hive service (interface <see cref="IHiveService"/>).
|
---|
36 | /// We need 'IgnoreExtensionDataObject' Attribute for the slave to work.
|
---|
37 | /// </summary>
|
---|
38 | [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, IgnoreExtensionDataObject = true)]
|
---|
39 | public class HiveService : IHiveService {
|
---|
40 | private IHiveDao dao {
|
---|
41 | get { return ServiceLocator.Instance.HiveDao; }
|
---|
42 | }
|
---|
43 | private IAuthenticationManager authen {
|
---|
44 | get { return ServiceLocator.Instance.AuthenticationManager; }
|
---|
45 | }
|
---|
46 | private IAuthorizationManager author {
|
---|
47 | get { return ServiceLocator.Instance.AuthorizationManager; }
|
---|
48 | }
|
---|
49 | private DataAccess.ITransactionManager trans {
|
---|
50 | get { return ServiceLocator.Instance.TransactionManager; }
|
---|
51 | }
|
---|
52 | private IEventManager eventManager {
|
---|
53 | get { return ServiceLocator.Instance.EventManager; }
|
---|
54 | }
|
---|
55 | private IUserManager userManager {
|
---|
56 | get { return ServiceLocator.Instance.UserManager; }
|
---|
57 | }
|
---|
58 | private HeartbeatManager heartbeatManager {
|
---|
59 | get { return ServiceLocator.Instance.HeartbeatManager; }
|
---|
60 | }
|
---|
61 |
|
---|
62 | #region Task Methods
|
---|
63 | public Guid AddTask(Task task, TaskData taskData, IEnumerable<Guid> resourceIds) {
|
---|
64 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
65 | return trans.UseTransaction(() => {
|
---|
66 | task.Id = dao.AddTask(task);
|
---|
67 | taskData.TaskId = task.Id;
|
---|
68 | taskData.LastUpdate = DateTime.Now;
|
---|
69 | foreach (Guid slaveGroupId in resourceIds) {
|
---|
70 | dao.AssignJobToResource(task.Id, slaveGroupId);
|
---|
71 | }
|
---|
72 | dao.AddTaskData(taskData);
|
---|
73 | //TODO: hack!! change this
|
---|
74 | if (task.State != TaskState.Aborted) {
|
---|
75 | dao.UpdateTaskState(task.Id, DA.TaskState.Waiting, null, userManager.CurrentUserId, null);
|
---|
76 | }
|
---|
77 | return taskData.TaskId;
|
---|
78 | }, false, true);
|
---|
79 | }
|
---|
80 |
|
---|
81 | public Guid AddChildTask(Guid parentTaskId, Task task, TaskData taskData) {
|
---|
82 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
83 | return trans.UseTransaction(() => {
|
---|
84 | task.ParentTaskId = parentTaskId;
|
---|
85 | return AddTask(task, taskData, dao.GetAssignedResources(parentTaskId).Select(x => x.Id));
|
---|
86 | }, false, true);
|
---|
87 | }
|
---|
88 |
|
---|
89 | public Task GetTask(Guid taskId) {
|
---|
90 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
91 | author.AuthorizeForTask(taskId, Permission.Read);
|
---|
92 |
|
---|
93 | return trans.UseTransaction(() => {
|
---|
94 | return dao.GetTask(taskId);
|
---|
95 | }, false, false);
|
---|
96 | }
|
---|
97 |
|
---|
98 | public IEnumerable<Task> GetTasks() {
|
---|
99 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
100 | var tasks = dao.GetTasks(x => true);
|
---|
101 | foreach (var task in tasks)
|
---|
102 | author.AuthorizeForTask(task.Id, Permission.Read);
|
---|
103 | return tasks;
|
---|
104 | }
|
---|
105 |
|
---|
106 | public IEnumerable<LightweightTask> GetLightweightTasks(IEnumerable<Guid> taskIds) {
|
---|
107 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
108 |
|
---|
109 | return trans.UseTransaction(() => {
|
---|
110 | var tasks = dao.GetTasks(x => taskIds.Contains(x.TaskId)).Select(x => new LightweightTask(x)).ToArray();
|
---|
111 | foreach (var task in tasks)
|
---|
112 | author.AuthorizeForTask(task.Id, Permission.Read);
|
---|
113 | return tasks;
|
---|
114 | }, false, false);
|
---|
115 | }
|
---|
116 |
|
---|
117 | public IEnumerable<LightweightTask> GetLightweightChildTasks(Guid? parentTaskId, bool recursive, bool includeParent) {
|
---|
118 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
119 |
|
---|
120 | return trans.UseTransaction(() => {
|
---|
121 | var tasks = GetChildTasks(parentTaskId, recursive, includeParent).Select(x => new LightweightTask(x)).ToArray();
|
---|
122 | foreach (var task in tasks)
|
---|
123 | author.AuthorizeForTask(task.Id, Permission.Read);
|
---|
124 | return tasks;
|
---|
125 | }, false, false);
|
---|
126 | }
|
---|
127 |
|
---|
128 | public IEnumerable<LightweightTask> GetLightweightJobTasks(Guid jobId) {
|
---|
129 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
130 | author.AuthorizeForJob(jobId, Permission.Read);
|
---|
131 |
|
---|
132 | return trans.UseTransaction(() => {
|
---|
133 | return dao.GetTasks(x => x.JobId == jobId).Select(x => new LightweightTask(x)).ToArray();
|
---|
134 | }, false, false);
|
---|
135 | }
|
---|
136 |
|
---|
137 | public TaskData GetTaskData(Guid taskId) {
|
---|
138 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
139 | author.AuthorizeForTask(taskId, Permission.Read);
|
---|
140 | return dao.GetTaskData(taskId);
|
---|
141 | }
|
---|
142 |
|
---|
143 | public void UpdateTask(Task taskDto) {
|
---|
144 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
145 | author.AuthorizeForTask(taskDto.Id, Permission.Full);
|
---|
146 | trans.UseTransaction(() => {
|
---|
147 | dao.UpdateTask(taskDto);
|
---|
148 | });
|
---|
149 | }
|
---|
150 |
|
---|
151 | public void UpdateTaskData(Task task, TaskData taskData) {
|
---|
152 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
153 | author.AuthorizeForTask(task.Id, Permission.Full);
|
---|
154 | author.AuthorizeForTask(taskData.TaskId, Permission.Full);
|
---|
155 | //trans.UseTransaction(() => { // cneumuel: try without transaction
|
---|
156 | taskData.LastUpdate = DateTime.Now;
|
---|
157 | dao.UpdateTask(task);
|
---|
158 | dao.UpdateTaskData(taskData);
|
---|
159 | //}, false, true);
|
---|
160 | }
|
---|
161 |
|
---|
162 | public void DeleteTask(Guid taskId) {
|
---|
163 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
164 | author.AuthorizeForTask(taskId, Permission.Full);
|
---|
165 | trans.UseTransaction(() => {
|
---|
166 | dao.DeleteTask(taskId);
|
---|
167 | });
|
---|
168 | }
|
---|
169 |
|
---|
170 | public void DeleteChildTasks(Guid parentTaskId) {
|
---|
171 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
172 | author.AuthorizeForTask(parentTaskId, Permission.Full);
|
---|
173 | trans.UseTransaction(() => {
|
---|
174 | var tasks = GetChildTasks(parentTaskId, true, false);
|
---|
175 | foreach (var task in tasks) {
|
---|
176 | dao.DeleteTask(task.Id);
|
---|
177 | dao.DeleteTaskData(task.Id);
|
---|
178 | };
|
---|
179 | });
|
---|
180 | }
|
---|
181 |
|
---|
182 | public Task UpdateTaskState(Guid taskId, TaskState taskState, Guid? slaveId, Guid? userId, string exception) {
|
---|
183 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
184 | author.AuthorizeForTask(taskId, Permission.Full);
|
---|
185 | return trans.UseTransaction(() => {
|
---|
186 | Task task = dao.UpdateTaskState(taskId, DataTransfer.Convert.ToEntity(taskState), slaveId, userId, exception);
|
---|
187 |
|
---|
188 | if (task.Command.HasValue && task.Command.Value == Command.Pause && task.State == TaskState.Paused) {
|
---|
189 | task.Command = null;
|
---|
190 | } else if (task.Command.HasValue && task.Command.Value == Command.Abort && task.State == TaskState.Aborted) {
|
---|
191 | task.Command = null;
|
---|
192 | } else if (task.Command.HasValue && task.Command.Value == Command.Stop && task.State == TaskState.Aborted) {
|
---|
193 | task.Command = null;
|
---|
194 | } else if (taskState == TaskState.Paused && !task.Command.HasValue) {
|
---|
195 | // slave paused and uploaded the task (no user-command) -> set waiting.
|
---|
196 | task = dao.UpdateTaskState(taskId, DataTransfer.Convert.ToEntity(TaskState.Waiting), slaveId, userId, exception);
|
---|
197 | }
|
---|
198 |
|
---|
199 | dao.UpdateTask(task);
|
---|
200 | return task;
|
---|
201 | });
|
---|
202 | }
|
---|
203 |
|
---|
204 | public IEnumerable<Task> GetTasksByResourceId(Guid resourceId) {
|
---|
205 | authen.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
206 | var tasks = trans.UseTransaction(() => dao.GetJobsByResourceId(resourceId));
|
---|
207 | foreach (var task in tasks)
|
---|
208 | author.AuthorizeForTask(task.Id, Permission.Read);
|
---|
209 | return tasks;
|
---|
210 | }
|
---|
211 | #endregion
|
---|
212 |
|
---|
213 | #region Task Control Methods
|
---|
214 | public void StopTask(Guid taskId) {
|
---|
215 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
216 | author.AuthorizeForTask(taskId, Permission.Full);
|
---|
217 | trans.UseTransaction(() => {
|
---|
218 | var task = dao.GetTask(taskId);
|
---|
219 | if (task.State == TaskState.Calculating || task.State == TaskState.Transferring) {
|
---|
220 | task.Command = Command.Stop;
|
---|
221 | dao.UpdateTask(task);
|
---|
222 | } else {
|
---|
223 | if (task.State != TaskState.Aborted && task.State != TaskState.Finished && task.State != TaskState.Failed) {
|
---|
224 | task = UpdateTaskState(taskId, TaskState.Aborted, null, null, string.Empty);
|
---|
225 | }
|
---|
226 | }
|
---|
227 | });
|
---|
228 | }
|
---|
229 |
|
---|
230 | public void PauseTask(Guid taskId) {
|
---|
231 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
232 | author.AuthorizeForTask(taskId, Permission.Full);
|
---|
233 | trans.UseTransaction(() => {
|
---|
234 | var job = dao.GetTask(taskId);
|
---|
235 | if (job.State == TaskState.Calculating || job.State == TaskState.Transferring) {
|
---|
236 | job.Command = Command.Pause;
|
---|
237 | dao.UpdateTask(job);
|
---|
238 | } else {
|
---|
239 | job = UpdateTaskState(taskId, TaskState.Paused, null, null, string.Empty);
|
---|
240 | }
|
---|
241 | });
|
---|
242 | }
|
---|
243 |
|
---|
244 | public void RestartTask(Guid taskId) {
|
---|
245 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
246 | author.AuthorizeForTask(taskId, Permission.Full);
|
---|
247 | trans.UseTransaction(() => {
|
---|
248 | Task task = dao.UpdateTaskState(taskId, DA.TaskState.Waiting, null, userManager.CurrentUserId, string.Empty);
|
---|
249 | task.Command = null;
|
---|
250 | dao.UpdateTask(task);
|
---|
251 | });
|
---|
252 | }
|
---|
253 | #endregion
|
---|
254 |
|
---|
255 | #region Job Methods
|
---|
256 | public Job GetJob(Guid id) {
|
---|
257 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
258 | author.AuthorizeForJob(id, Permission.Read);
|
---|
259 | var job = dao.GetJobs(x =>
|
---|
260 | x.JobId == id
|
---|
261 | && (x.OwnerUserId == userManager.CurrentUserId || x.JobPermissions.Count(hep => hep.Permission != DA.Permission.NotAllowed && hep.GrantedUserId == userManager.CurrentUserId) > 0)
|
---|
262 | ).FirstOrDefault();
|
---|
263 | if (job != null) {
|
---|
264 | job.Permission = DT.Convert.ToDto(dao.GetPermissionForJob(job.Id, userManager.CurrentUserId));
|
---|
265 | job.OwnerUsername = userManager.GetUserById(job.OwnerUserId).UserName;
|
---|
266 | }
|
---|
267 | return job;
|
---|
268 | }
|
---|
269 |
|
---|
270 | public IEnumerable<Job> GetJobs() {
|
---|
271 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
272 | var jobs = dao.GetJobs(x => x.OwnerUserId == userManager.CurrentUserId || x.JobPermissions.Count(hep => hep.Permission != DA.Permission.NotAllowed && hep.GrantedUserId == userManager.CurrentUserId) > 0);
|
---|
273 | foreach (var job in jobs) {
|
---|
274 | author.AuthorizeForJob(job.Id, Permission.Read);
|
---|
275 | job.Permission = DT.Convert.ToDto(dao.GetPermissionForJob(job.Id, userManager.CurrentUserId));
|
---|
276 | job.OwnerUsername = userManager.GetUserById(job.OwnerUserId).UserName;
|
---|
277 | }
|
---|
278 | return jobs;
|
---|
279 | }
|
---|
280 |
|
---|
281 | public IEnumerable<Job> GetAllJobs() {
|
---|
282 | authen.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
283 | var jobs = dao.GetJobs(x => true);
|
---|
284 | foreach (var job in jobs) { // no authorization here, since this method is admin-only! (admin is allowed to read all task)
|
---|
285 | job.Permission = DT.Convert.ToDto(dao.GetPermissionForJob(job.Id, userManager.CurrentUserId));
|
---|
286 | job.OwnerUsername = userManager.GetUserById(job.OwnerUserId).UserName;
|
---|
287 | }
|
---|
288 | return jobs;
|
---|
289 | }
|
---|
290 |
|
---|
291 | public Guid AddJob(Job jobDto) {
|
---|
292 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
293 | return trans.UseTransaction(() => {
|
---|
294 | jobDto.OwnerUserId = userManager.CurrentUserId;
|
---|
295 | jobDto.DateCreated = DateTime.Now;
|
---|
296 | return dao.AddJob(jobDto);
|
---|
297 | });
|
---|
298 | }
|
---|
299 |
|
---|
300 | public void UpdateJob(Job jobDto) {
|
---|
301 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
302 | author.AuthorizeForJob(jobDto.Id, Permission.Full);
|
---|
303 | trans.UseTransaction(() => {
|
---|
304 | dao.UpdateJob(jobDto);
|
---|
305 | });
|
---|
306 | }
|
---|
307 |
|
---|
308 | public void DeleteJob(Guid jobId) {
|
---|
309 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
310 | author.AuthorizeForJob(jobId, Permission.Full);
|
---|
311 | trans.UseTransaction(() => {
|
---|
312 | dao.DeleteJob(jobId); // child task will be deleted by db-trigger
|
---|
313 | });
|
---|
314 | }
|
---|
315 | #endregion
|
---|
316 |
|
---|
317 | #region JobPermission Methods
|
---|
318 | public void GrantPermission(Guid jobId, Guid grantedUserId, Permission permission) {
|
---|
319 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
320 | trans.UseTransaction(() => {
|
---|
321 | Job job = dao.GetJob(jobId);
|
---|
322 | if (job == null) throw new FaultException<FaultReason>(new FaultReason("Could not find task with id " + jobId));
|
---|
323 | Permission perm = DT.Convert.ToDto(dao.GetPermissionForJob(job.Id, userManager.CurrentUserId));
|
---|
324 | if (perm != Permission.Full) throw new FaultException<FaultReason>(new FaultReason("Not allowed to grant permissions for this experiment"));
|
---|
325 | dao.SetJobPermission(jobId, userManager.CurrentUserId, grantedUserId, DT.Convert.ToEntity(permission));
|
---|
326 | });
|
---|
327 | }
|
---|
328 |
|
---|
329 | public void RevokePermission(Guid jobId, Guid grantedUserId) {
|
---|
330 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
331 | trans.UseTransaction(() => {
|
---|
332 | Job job = dao.GetJob(jobId);
|
---|
333 | if (job == null) throw new FaultException<FaultReason>(new FaultReason("Could not find task with id " + jobId));
|
---|
334 | DA.Permission perm = dao.GetPermissionForJob(job.Id, userManager.CurrentUserId);
|
---|
335 | if (perm != DA.Permission.Full) throw new FaultException<FaultReason>(new FaultReason("Not allowed to grant permissions for this experiment"));
|
---|
336 | dao.SetJobPermission(jobId, userManager.CurrentUserId, grantedUserId, DA.Permission.NotAllowed);
|
---|
337 | });
|
---|
338 | }
|
---|
339 | public IEnumerable<JobPermission> GetJobPermissions(Guid jobId) {
|
---|
340 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
341 | return trans.UseTransaction(() => {
|
---|
342 | DA.Permission currentUserPermission = dao.GetPermissionForJob(jobId, userManager.CurrentUserId);
|
---|
343 | if (currentUserPermission != DA.Permission.Full) throw new FaultException<FaultReason>(new FaultReason("Not allowed to list permissions for this experiment"));
|
---|
344 | return dao.GetJobPermissions(x => x.JobId == jobId);
|
---|
345 | });
|
---|
346 | }
|
---|
347 |
|
---|
348 | public bool IsAllowedPrivileged() {
|
---|
349 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
350 | return authen.IsInRole(HiveRoles.IsAllowedPrivileged);
|
---|
351 | }
|
---|
352 | #endregion
|
---|
353 |
|
---|
354 | #region Login Methods
|
---|
355 | public void Hello(Slave slaveInfo) {
|
---|
356 | authen.AuthenticateForAnyRole(HiveRoles.Slave);
|
---|
357 | trans.UseTransaction(() => {
|
---|
358 | var slave = dao.GetSlave(slaveInfo.Id);
|
---|
359 |
|
---|
360 | if (slave == null) {
|
---|
361 | dao.AddSlave(slaveInfo);
|
---|
362 | } else {
|
---|
363 | var dbSlave = dao.GetSlave(slaveInfo.Id);
|
---|
364 |
|
---|
365 | dbSlave.Name = slaveInfo.Name;
|
---|
366 | dbSlave.Description = slaveInfo.Description;
|
---|
367 |
|
---|
368 | dbSlave.Cores = slaveInfo.Cores;
|
---|
369 | dbSlave.CpuArchitecture = slaveInfo.CpuArchitecture;
|
---|
370 | dbSlave.CpuSpeed = slaveInfo.CpuSpeed;
|
---|
371 | dbSlave.FreeCores = slaveInfo.FreeCores;
|
---|
372 | dbSlave.FreeMemory = slaveInfo.FreeMemory;
|
---|
373 | dbSlave.Memory = slaveInfo.Memory;
|
---|
374 | dbSlave.OperatingSystem = slaveInfo.OperatingSystem;
|
---|
375 |
|
---|
376 | dbSlave.LastHeartbeat = DateTime.Now;
|
---|
377 | dbSlave.SlaveState = SlaveState.Idle;
|
---|
378 |
|
---|
379 | // don't update those properties: dbSlave.IsAllowedToCalculate, dbSlave.ParentResourceId
|
---|
380 |
|
---|
381 | dao.UpdateSlave(dbSlave);
|
---|
382 | }
|
---|
383 | });
|
---|
384 | }
|
---|
385 |
|
---|
386 | public void GoodBye(Guid slaveId) {
|
---|
387 | authen.AuthenticateForAnyRole(HiveRoles.Slave);
|
---|
388 | trans.UseTransaction(() => {
|
---|
389 | var slave = dao.GetSlave(slaveId);
|
---|
390 | if (slave != null) {
|
---|
391 | slave.SlaveState = SlaveState.Offline;
|
---|
392 | dao.UpdateSlave(slave);
|
---|
393 | }
|
---|
394 | });
|
---|
395 | }
|
---|
396 | #endregion
|
---|
397 |
|
---|
398 | #region Heartbeat Methods
|
---|
399 | public List<MessageContainer> Heartbeat(Heartbeat heartbeat) {
|
---|
400 | authen.AuthenticateForAnyRole(HiveRoles.Slave);
|
---|
401 |
|
---|
402 | List<MessageContainer> result = trans.UseTransaction(() => heartbeatManager.ProcessHeartbeat(heartbeat));
|
---|
403 |
|
---|
404 | if (HeuristicLab.Services.Hive.Properties.Settings.Default.TriggerEventManagerInHeartbeat) {
|
---|
405 | TriggerEventManager(false);
|
---|
406 | }
|
---|
407 |
|
---|
408 | return result;
|
---|
409 | }
|
---|
410 | #endregion
|
---|
411 |
|
---|
412 | #region Plugin Methods
|
---|
413 | public Guid AddPlugin(Plugin plugin, List<PluginData> pluginDatas) {
|
---|
414 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
415 | return trans.UseTransaction(() => {
|
---|
416 | plugin.UserId = userManager.CurrentUserId;
|
---|
417 | plugin.DateCreated = DateTime.Now;
|
---|
418 |
|
---|
419 | var existing = dao.GetPlugins(x => x.Hash != null).Where(x => x.Hash.SequenceEqual(plugin.Hash));
|
---|
420 | if (existing.Count() > 0) {
|
---|
421 | // a plugin already exists.
|
---|
422 | throw new FaultException<PluginAlreadyExistsFault>(new PluginAlreadyExistsFault(existing.Single().Id));
|
---|
423 | }
|
---|
424 |
|
---|
425 | Guid pluginId = dao.AddPlugin(plugin);
|
---|
426 | foreach (PluginData pluginData in pluginDatas) {
|
---|
427 | pluginData.PluginId = pluginId;
|
---|
428 | dao.AddPluginData(pluginData);
|
---|
429 | }
|
---|
430 | return pluginId;
|
---|
431 | });
|
---|
432 | }
|
---|
433 |
|
---|
434 | public Plugin GetPlugin(Guid pluginId) {
|
---|
435 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
436 | return dao.GetPlugin(pluginId);
|
---|
437 | }
|
---|
438 |
|
---|
439 | public Plugin GetPluginByHash(byte[] hash) {
|
---|
440 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
441 | return dao.GetPlugins(x => x.Hash == hash).FirstOrDefault();
|
---|
442 | }
|
---|
443 |
|
---|
444 | // note: this is a possible security problem, since a client is able to download all plugins, which may contain proprietary code (which can be disassembled)
|
---|
445 | // change so that only with GetPluginByHash it is possible to download plugins
|
---|
446 | public IEnumerable<Plugin> GetPlugins() {
|
---|
447 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
448 | return dao.GetPlugins(x => x.Hash != null);
|
---|
449 | }
|
---|
450 |
|
---|
451 | public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
|
---|
452 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
453 | var pluginDatas = new List<PluginData>();
|
---|
454 | return trans.UseTransaction(() => {
|
---|
455 | foreach (Guid guid in pluginIds) {
|
---|
456 | pluginDatas.AddRange(dao.GetPluginDatas(x => x.PluginId == guid).ToList());
|
---|
457 | }
|
---|
458 | return pluginDatas;
|
---|
459 | });
|
---|
460 | }
|
---|
461 |
|
---|
462 | public void DeletePlugin(Guid pluginId) {
|
---|
463 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
|
---|
464 | dao.DeletePlugin(pluginId);
|
---|
465 | }
|
---|
466 | #endregion
|
---|
467 |
|
---|
468 | #region Slave Methods
|
---|
469 | public int GetNewHeartbeatInterval(Guid slaveId) {
|
---|
470 | authen.AuthenticateForAnyRole(HiveRoles.Slave);
|
---|
471 | Slave s = dao.GetSlave(slaveId);
|
---|
472 | if (s != null) {
|
---|
473 | return s.HbInterval;
|
---|
474 | } else {
|
---|
475 | return -1;
|
---|
476 | }
|
---|
477 | }
|
---|
478 |
|
---|
479 | public Guid AddSlave(Slave slave) {
|
---|
480 | authen.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
481 | return trans.UseTransaction(() => dao.AddSlave(slave));
|
---|
482 | }
|
---|
483 |
|
---|
484 | public Guid AddSlaveGroup(SlaveGroup slaveGroup) {
|
---|
485 | authen.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
486 | return trans.UseTransaction(() => dao.AddSlaveGroup(slaveGroup));
|
---|
487 | }
|
---|
488 |
|
---|
489 | public Slave GetSlave(Guid slaveId) {
|
---|
490 | authen.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
491 | return dao.GetSlave(slaveId);
|
---|
492 | }
|
---|
493 |
|
---|
494 | public SlaveGroup GetSlaveGroup(Guid slaveGroupId) {
|
---|
495 | authen.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
496 | return dao.GetSlaveGroup(slaveGroupId);
|
---|
497 | }
|
---|
498 |
|
---|
499 | public IEnumerable<Slave> GetSlaves() {
|
---|
500 | authen.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
501 | return dao.GetSlaves(x => true);
|
---|
502 | }
|
---|
503 |
|
---|
504 | public IEnumerable<SlaveGroup> GetSlaveGroups() {
|
---|
505 | authen.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
506 | return dao.GetSlaveGroups(x => true);
|
---|
507 | }
|
---|
508 |
|
---|
509 | public void UpdateSlave(Slave slave) {
|
---|
510 | authen.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
511 | trans.UseTransaction(() => {
|
---|
512 | dao.UpdateSlave(slave);
|
---|
513 | });
|
---|
514 | }
|
---|
515 |
|
---|
516 | public void UpdateSlaveGroup(SlaveGroup slaveGroup) {
|
---|
517 | authen.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
518 | trans.UseTransaction(() => {
|
---|
519 | dao.UpdateSlaveGroup(slaveGroup);
|
---|
520 | });
|
---|
521 | }
|
---|
522 |
|
---|
523 | public void DeleteSlave(Guid slaveId) {
|
---|
524 | authen.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
525 | trans.UseTransaction(() => {
|
---|
526 | dao.DeleteSlave(slaveId);
|
---|
527 | });
|
---|
528 | }
|
---|
529 |
|
---|
530 | public void DeleteSlaveGroup(Guid slaveGroupId) {
|
---|
531 | authen.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
532 | trans.UseTransaction(() => {
|
---|
533 | dao.DeleteSlaveGroup(slaveGroupId);
|
---|
534 | });
|
---|
535 | }
|
---|
536 |
|
---|
537 | public void AddResourceToGroup(Guid slaveGroupId, Guid resourceId) {
|
---|
538 | authen.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
539 | trans.UseTransaction(() => {
|
---|
540 | var resource = dao.GetResource(resourceId);
|
---|
541 | resource.ParentResourceId = slaveGroupId;
|
---|
542 | dao.UpdateResource(resource);
|
---|
543 | });
|
---|
544 | }
|
---|
545 |
|
---|
546 | public void RemoveResourceFromGroup(Guid slaveGroupId, Guid resourceId) {
|
---|
547 | authen.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
548 | trans.UseTransaction(() => {
|
---|
549 | var resource = dao.GetResource(resourceId);
|
---|
550 | resource.ParentResourceId = null;
|
---|
551 | dao.UpdateResource(resource);
|
---|
552 | });
|
---|
553 | }
|
---|
554 |
|
---|
555 | public Guid GetResourceId(string resourceName) {
|
---|
556 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
557 | return trans.UseTransaction(() => {
|
---|
558 | var resource = dao.GetResources(x => x.Name == resourceName).FirstOrDefault();
|
---|
559 | if (resource != null) {
|
---|
560 | return resource.Id;
|
---|
561 | } else {
|
---|
562 | return Guid.Empty;
|
---|
563 | }
|
---|
564 | });
|
---|
565 | }
|
---|
566 |
|
---|
567 | public void TriggerEventManager(bool force) {
|
---|
568 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Slave);
|
---|
569 | // use a serializable transaction here to ensure not two threads execute this simultaniously (mutex-lock would not work since IIS may use multiple AppDomains)
|
---|
570 | bool cleanup = false;
|
---|
571 | trans.UseTransaction(() => {
|
---|
572 | DateTime lastCleanup = dao.GetLastCleanup();
|
---|
573 | if (force || DateTime.Now - lastCleanup > HeuristicLab.Services.Hive.Properties.Settings.Default.CleanupInterval) {
|
---|
574 | dao.SetLastCleanup(DateTime.Now);
|
---|
575 | cleanup = true;
|
---|
576 | }
|
---|
577 | }, true);
|
---|
578 |
|
---|
579 | if (cleanup) {
|
---|
580 | eventManager.Cleanup();
|
---|
581 | }
|
---|
582 | }
|
---|
583 | #endregion
|
---|
584 |
|
---|
585 | #region Downtime Methods
|
---|
586 | public Guid AddDowntime(Downtime downtime) {
|
---|
587 | authen.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
588 | return trans.UseTransaction(() => dao.AddDowntime(downtime));
|
---|
589 | }
|
---|
590 |
|
---|
591 | public void DeleteDowntime(Guid downtimeId) {
|
---|
592 | authen.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
593 | trans.UseTransaction(() => {
|
---|
594 | dao.DeleteDowntime(downtimeId);
|
---|
595 | });
|
---|
596 | }
|
---|
597 |
|
---|
598 | public void UpdateDowntime(Downtime downtime) {
|
---|
599 | authen.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
600 | trans.UseTransaction(() => {
|
---|
601 | dao.UpdateDowntime(downtime);
|
---|
602 | });
|
---|
603 | }
|
---|
604 |
|
---|
605 | public IEnumerable<Downtime> GetDowntimesForResource(Guid resourceId) {
|
---|
606 | authen.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
607 | return trans.UseTransaction(() => dao.GetDowntimes(x => x.ResourceId == resourceId));
|
---|
608 | }
|
---|
609 | #endregion
|
---|
610 |
|
---|
611 | #region User Methods
|
---|
612 | public string GetUsernameByUserId(Guid userId) {
|
---|
613 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
614 | var user = ServiceLocator.Instance.UserManager.GetUserById(userId);
|
---|
615 | if (user != null)
|
---|
616 | return user.UserName;
|
---|
617 | else
|
---|
618 | return null;
|
---|
619 | }
|
---|
620 |
|
---|
621 | public Guid GetUserIdByUsername(string username) {
|
---|
622 | authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
|
---|
623 | var user = ServiceLocator.Instance.UserManager.GetUserByName(username);
|
---|
624 | return user != null ? (Guid)user.ProviderUserKey : Guid.Empty;
|
---|
625 | }
|
---|
626 | #endregion
|
---|
627 |
|
---|
628 | #region Helper Methods
|
---|
629 | private IEnumerable<Task> GetChildTasks(Guid? parentTaskId, bool recursive, bool includeParent) {
|
---|
630 | var tasks = new List<Task>(dao.GetTasks(x => parentTaskId == null ? !x.ParentTaskId.HasValue : x.ParentTaskId.Value == parentTaskId));
|
---|
631 |
|
---|
632 | if (recursive) {
|
---|
633 | var childs = new List<Task>();
|
---|
634 | foreach (var task in tasks) {
|
---|
635 | childs.AddRange(GetChildTasks(task.Id, recursive, false));
|
---|
636 | }
|
---|
637 | tasks.AddRange(childs);
|
---|
638 | }
|
---|
639 |
|
---|
640 | if (includeParent) tasks.Add(GetTask(parentTaskId.Value));
|
---|
641 | return tasks;
|
---|
642 | }
|
---|
643 | #endregion
|
---|
644 | }
|
---|
645 | }
|
---|