Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive/3.4/HiveService.cs @ 6486

Last change on this file since 6486 was 6479, checked in by cneumuel, 13 years ago

#1233

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