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

Last change on this file since 6465 was 6465, checked in by cneumuel, 8 years ago

#1233

  • show owner of experimens in listview
File size: 24.8 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    #endregion
331
332    #region Login Methods
333    public void Hello(Slave slaveInfo) {
334      authen.AuthenticateForAnyRole(HiveRoles.Slave);
335      trans.UseTransaction(() => {
336        var slave = dao.GetSlave(slaveInfo.Id);
337
338        if (slave == null) {
339          dao.AddSlave(slaveInfo);
340        } else {
341          var dbSlave = dao.GetSlave(slaveInfo.Id);
342
343          dbSlave.Name = slaveInfo.Name;
344          dbSlave.Description = slaveInfo.Description;
345
346          dbSlave.Cores = slaveInfo.Cores;
347          dbSlave.CpuArchitecture = slaveInfo.CpuArchitecture;
348          dbSlave.CpuSpeed = slaveInfo.CpuSpeed;
349          dbSlave.FreeCores = slaveInfo.FreeCores;
350          dbSlave.FreeMemory = slaveInfo.FreeMemory;
351          dbSlave.Memory = slaveInfo.Memory;
352          dbSlave.OperatingSystem = slaveInfo.OperatingSystem;
353
354          dbSlave.LastHeartbeat = DateTime.Now;
355          dbSlave.SlaveState = SlaveState.Idle;
356
357          // don't update those properties: dbSlave.IsAllowedToCalculate, dbSlave.ParentResourceId
358
359          dao.UpdateSlave(dbSlave);
360        }
361      });
362    }
363
364    public void GoodBye(Guid slaveId) {
365      authen.AuthenticateForAnyRole(HiveRoles.Slave);
366      trans.UseTransaction(() => {
367        var slave = dao.GetSlave(slaveId);
368        if (slave != null) {
369          slave.SlaveState = SlaveState.Offline;
370          dao.UpdateSlave(slave);
371        }
372      });
373    }
374    #endregion
375
376    #region Heartbeat Methods
377    public List<MessageContainer> Heartbeat(Heartbeat heartbeat) {
378      authen.AuthenticateForAnyRole(HiveRoles.Slave);
379      TriggerLifecycle(false);
380      return trans.UseTransaction(() => heartbeatManager.ProcessHeartbeat(heartbeat));
381    }
382    #endregion
383
384    #region Plugin Methods
385    public Guid AddPlugin(Plugin plugin, List<PluginData> pluginDatas) {
386      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
387      return trans.UseTransaction(() => {
388        plugin.UserId = userManager.CurrentUserId;
389        plugin.DateCreated = DateTime.Now;
390
391        var existing = dao.GetPlugins(x => x.Hash != null).Where(x => x.Hash.SequenceEqual(plugin.Hash));
392        if (existing.Count() > 0) {
393          // a plugin already exists.
394          throw new FaultException<PluginAlreadyExistsFault>(new PluginAlreadyExistsFault(existing.Single().Id));
395        }
396
397        Guid pluginId = dao.AddPlugin(plugin);
398        foreach (PluginData pluginData in pluginDatas) {
399          pluginData.PluginId = pluginId;
400          dao.AddPluginData(pluginData);
401        }
402        return pluginId;
403      });
404    }
405
406    public Plugin GetPlugin(Guid pluginId) {
407      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
408      return dao.GetPlugin(pluginId);
409    }
410
411    public Plugin GetPluginByHash(byte[] hash) {
412      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
413      return dao.GetPlugins(x => x.Hash == hash).FirstOrDefault();
414    }
415
416    // 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)
417    //       change so that only with GetPluginByHash it is possible to download plugins
418    public IEnumerable<Plugin> GetPlugins() {
419      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
420      return dao.GetPlugins(x => x.Hash != null);
421    }
422
423    public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
424      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
425      var pluginDatas = new List<PluginData>();
426      return trans.UseTransaction(() => {
427        foreach (Guid guid in pluginIds) {
428          pluginDatas.AddRange(dao.GetPluginDatas(x => x.PluginId == guid).ToList());
429        }
430        return pluginDatas;
431      });
432    }
433
434    public void DeletePlugin(Guid pluginId) {
435      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
436      dao.DeletePlugin(pluginId);
437    }
438    #endregion
439
440    #region Slave Methods
441    public Guid AddSlave(Slave slave) {
442      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
443      return trans.UseTransaction(() => dao.AddSlave(slave));
444    }
445
446    public Guid AddSlaveGroup(SlaveGroup slaveGroup) {
447      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
448      return trans.UseTransaction(() => dao.AddSlaveGroup(slaveGroup));
449    }
450
451    public Slave GetSlave(Guid slaveId) {
452      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
453      return dao.GetSlave(slaveId);
454    }
455
456    public SlaveGroup GetSlaveGroup(Guid slaveGroupId) {
457      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
458      return dao.GetSlaveGroup(slaveGroupId);
459    }
460
461    public IEnumerable<Slave> GetSlaves() {
462      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
463      return dao.GetSlaves(x => true);
464    }
465
466    public IEnumerable<SlaveGroup> GetSlaveGroups() {
467      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
468      return dao.GetSlaveGroups(x => true);
469    }
470
471    public void UpdateSlave(Slave slave) {
472      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
473      trans.UseTransaction(() => {
474        dao.UpdateSlave(slave);
475      });
476    }
477
478    public void UpdateSlaveGroup(SlaveGroup slaveGroup) {
479      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
480      trans.UseTransaction(() => {
481        dao.UpdateSlaveGroup(slaveGroup);
482      });
483    }
484
485    public void DeleteSlave(Guid slaveId) {
486      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
487      trans.UseTransaction(() => {
488        dao.DeleteSlave(slaveId);
489      });
490    }
491
492    public void DeleteSlaveGroup(Guid slaveGroupId) {
493      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
494      trans.UseTransaction(() => {
495        dao.DeleteSlaveGroup(slaveGroupId);
496      });
497    }
498
499    public void AddResourceToGroup(Guid slaveGroupId, Guid resourceId) {
500      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
501      trans.UseTransaction(() => {
502        var resource = dao.GetResource(resourceId);
503        resource.ParentResourceId = slaveGroupId;
504        dao.UpdateResource(resource);
505      });
506    }
507
508    public void RemoveResourceFromGroup(Guid slaveGroupId, Guid resourceId) {
509      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
510      trans.UseTransaction(() => {
511        var resource = dao.GetResource(resourceId);
512        resource.ParentResourceId = null;
513        dao.UpdateResource(resource);
514      });
515    }
516
517    public Guid GetResourceId(string resourceName) {
518      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
519      return trans.UseTransaction(() => {
520        var resource = dao.GetResources(x => x.Name == resourceName).FirstOrDefault();
521        if (resource != null) {
522          return resource.Id;
523        } else {
524          return Guid.Empty;
525        }
526      });
527    }
528
529    public void TriggerLifecycle(bool force) {
530      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Slave);
531      // use a serializable transaction here to ensure not two threads execute this simultaniously (mutex-lock would not work since IIS may use multiple AppDomains)
532      trans.UseTransaction(() => {
533        DateTime lastCleanup = dao.GetLastCleanup();
534        if (force || DateTime.Now - lastCleanup > TimeSpan.FromSeconds(59)) {
535          dao.SetLastCleanup(DateTime.Now);
536          lifecycleManager.Cleanup();
537        }
538      }, true);
539    }
540    #endregion
541
542    #region Downtime Methods
543    public Guid AddDowntime(Downtime downtime) {
544      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
545      return trans.UseTransaction(() => dao.AddDowntime(downtime));
546    }
547
548    public void DeleteDowntime(Guid downtimeId) {
549      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
550      trans.UseTransaction(() => {
551        dao.DeleteDowntime(downtimeId);
552      });
553    }
554
555    public void UpdateDowntime(Downtime downtime) {
556      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
557      trans.UseTransaction(() => {
558        dao.UpdateDowntime(downtime);
559      });
560    }
561
562    public IEnumerable<Downtime> GetDowntimesForResource(Guid resourceId) {
563      authen.AuthenticateForAnyRole(HiveRoles.Administrator);
564      return trans.UseTransaction(() => dao.GetDowntimes(x => x.ResourceId == resourceId));
565    }
566    #endregion
567
568    #region User Methods
569    public string GetUsernameByUserId(Guid userId) {
570      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
571      var user = ServiceLocator.Instance.UserManager.GetUserById(userId);
572      if (user != null)
573        return user.UserName;
574      else
575        return null;
576    }
577
578    public Guid GetUserIdByUsername(string username) {
579      authen.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
580      var user = ServiceLocator.Instance.UserManager.GetUserByName(username);
581      return user != null ? (Guid)user.ProviderUserKey : Guid.Empty;
582    }
583    #endregion
584
585    #region Helper Methods
586    private IEnumerable<Job> GetChildJobs(Guid? parentJobId, bool recursive, bool includeParent) {
587      var jobs = new List<Job>(dao.GetJobs(x => parentJobId == null ? !x.ParentJobId.HasValue : x.ParentJobId.Value == parentJobId));
588
589      if (recursive) {
590        var childs = new List<Job>();
591        foreach (var job in jobs) {
592          childs.AddRange(GetChildJobs(job.Id, recursive, false));
593        }
594        jobs.AddRange(childs);
595      }
596
597      if (includeParent) jobs.Add(GetJob(parentJobId.Value));
598      return jobs;
599    }
600    #endregion
601  }
602}
Note: See TracBrowser for help on using the repository browser.