Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6347 was 6267, checked in by cneumuel, 14 years ago

#1233

  • extended statistics recording:
    • execution times of users are captured
    • execution times and start-to-finish time of finished jobs is captured (to computer hive overhead)
    • data of deleted jobs is automatically captured in DeletedJobStatistics
  • changed ExecutionTime type in database from string to float (milliseconds are stored instead of TimeSpan.ToString())
  • added IsPrivileged field to job to indicate if it should be executed in a privileged sandbox
  • added CpuUtilization field to slave to be able to report cpu utilization
  • added GetJobsByResourceId to retrieve all jobs which are currently beeing calculated in a slave(-group)
  • TransactionManager now allows to use serializable tranactions (used for lifecycle trigger)
File size: 22.3 KB
RevLine 
[5028]1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.ServiceModel;
[5375]5using HeuristicLab.Services.Hive.Common;
6using HeuristicLab.Services.Hive.Common.DataTransfer;
[5028]7using HeuristicLab.Services.Hive.Common.ServiceContracts;
8
9namespace HeuristicLab.Services.Hive {
10
11  /// <summary>
12  /// Implementation of the Hive service (interface <see cref="IHiveService"/>).
[5599]13  /// We need 'IgnoreExtensionDataObject' Attribute for the slave to work.
[5028]14  /// </summary>
[5599]15  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, IgnoreExtensionDataObject = true)]
[5028]16  public class HiveService : IHiveService {
17    private DataAccess.IHiveDao dao {
18      get { return ServiceLocator.Instance.HiveDao; }
19    }
20    private HeuristicLab.Services.Hive.DataAccess.TransactionManager trans {
21      get { return ServiceLocator.Instance.TransactionManager; }
22    }
23    private IAuthorizationManager auth {
24      get { return ServiceLocator.Instance.AuthorizationManager; }
25    }
[5095]26    private ILifecycleManager lifecycleManager {
27      get { return ServiceLocator.Instance.LifecycleManager; }
[5028]28    }
[5405]29    private HeartbeatManager heartbeatManager {
30      get { return ServiceLocator.Instance.HeartbeatManager; }
31    }
[5028]32
33    #region Job Methods
[6006]34    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
35    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
[5511]36    public Guid AddJob(Job job, JobData jobData, IEnumerable<Guid> resourceIds) {
[5708]37      return trans.UseTransaction(() => {
[5155]38        job.Id = dao.AddJob(job);
39        jobData.JobId = job.Id;
[5106]40        jobData.LastUpdate = DateTime.Now;
[5511]41        if (resourceIds != null) {
42          foreach (Guid slaveGroupId in resourceIds) {
[5155]43            dao.AssignJobToResource(job.Id, slaveGroupId);
44          }
45        } else {
46          // todo: use default group
47        }
[5028]48        dao.AddJobData(jobData);
[5636]49        dao.UpdateJobState(job.Id, JobState.Waiting, null, auth.UserId, null);
[5028]50        return jobData.JobId;
[5708]51      });
[5028]52    }
53
[6006]54    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
55    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
[5028]56    public Guid AddChildJob(Guid parentJobId, Job job, JobData jobData) {
[5708]57      return trans.UseTransaction(() => {
[5028]58        job.ParentJobId = parentJobId;
[5155]59        return AddJob(job, jobData, dao.GetAssignedResources(parentJobId).Select(x => x.Id));
[5708]60      });
[5028]61    }
62
[6006]63    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
64    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
65    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
[5028]66    public Job GetJob(Guid jobId) {
67      return dao.GetJob(jobId);
68    }
69
[6006]70    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
71    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
[5028]72    public IEnumerable<Job> GetJobs() {
73      return dao.GetJobs(x => true);
74    }
75
[6006]76    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
77    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
[5028]78    public IEnumerable<LightweightJob> GetLightweightJobs(IEnumerable<Guid> jobIds) {
79      return dao.GetJobs(x => jobIds.Contains(x.JobId)).Select(x => new LightweightJob(x)).ToArray();
80    }
81
[6006]82    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
83    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
[5028]84    public IEnumerable<LightweightJob> GetLightweightChildJobs(Guid? parentJobId, bool recursive, bool includeParent) {
85      return GetChildJobs(parentJobId, recursive, includeParent).Select(x => new LightweightJob(x)).ToArray();
86    }
87
[6006]88    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
89    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
90    public IEnumerable<LightweightJob> GetLightweightExperimentJobs(Guid experimentId) {
91      return dao.GetJobs(x => x.HiveExperimentId == experimentId).Select(x => new LightweightJob(x)).ToArray();
92    }
93
94    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
95    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
96    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
[5028]97    public JobData GetJobData(Guid jobId) {
98      return dao.GetJobData(jobId);
99    }
100
[6006]101    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
102    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
103    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
[5511]104    public void UpdateJob(Job job) {
[5708]105      trans.UseTransaction(() => {
[5511]106        dao.UpdateJob(job);
[5708]107      });
[5511]108    }
109
[6006]110    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
111    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
112    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
[5511]113    public void UpdateJobData(Job job, JobData jobData) {
[5708]114      trans.UseTransaction(() => {
[5106]115        jobData.LastUpdate = DateTime.Now;
116        dao.UpdateJob(job);
117        dao.UpdateJobData(jobData);
[5708]118      });
[5028]119    }
120
[6006]121    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
122    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
123    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
[5106]124    public void DeleteJob(Guid jobId) {
[5708]125      trans.UseTransaction(() => {
[5106]126        dao.DeleteJob(jobId);
[5708]127      });
[5028]128    }
129
[6006]130    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
131    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
132    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
[5106]133    public void DeleteChildJobs(Guid parentJobId) {
[5708]134      trans.UseTransaction(() => {
[5106]135        var jobs = GetChildJobs(parentJobId, true, false);
[5028]136        foreach (var job in jobs) {
137          dao.DeleteJob(job.Id);
138          dao.DeleteJobData(job.Id);
139        };
[5708]140      });
[5028]141    }
[5102]142
[6006]143    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
144    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
145    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
[5636]146    public Job UpdateJobState(Guid jobId, JobState jobState, Guid? slaveId, Guid? userId, string exception) {
[5779]147      return trans.UseTransaction(() => {
148        Job job = dao.UpdateJobState(jobId, jobState, slaveId, userId, exception);
[6267]149
[5779]150        if (job.Command.HasValue && job.Command.Value == Command.Pause && job.State == JobState.Paused) {
151          job.Command = null;
152        } else if (job.Command.HasValue && job.Command.Value == Command.Abort && job.State == JobState.Aborted) {
153          job.Command = null;
154        } else if (job.Command.HasValue && job.Command.Value == Command.Stop && job.State == JobState.Aborted) {
155          job.Command = null;
[6110]156        } else if (jobState == JobState.Paused && !job.Command.HasValue) {
157          // job was paused and uploaded by slave without the user-command to pause it -> set waiting
158          job = dao.UpdateJobState(jobId, JobState.Waiting, slaveId, userId, exception);
[5779]159        }
[6110]160
[5779]161        dao.UpdateJob(job);
162        return job;
163      });
[5636]164    }
[5028]165    #endregion
166
167    #region Job Control Methods
[6006]168    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
169    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
170    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
[5053]171    public void StopJob(Guid jobId) {
[5708]172      trans.UseTransaction(() => {
[5779]173        var job = dao.GetJob(jobId);
[6168]174        if (job.State == JobState.Calculating || job.State == JobState.Transferring) {
175          job.Command = Command.Stop;
176          dao.UpdateJob(job);
177        } else {
178          if (job.State != JobState.Aborted && job.State != JobState.Finished && job.State != JobState.Failed) {
179            job = UpdateJobState(jobId, JobState.Aborted, null, null, string.Empty);
180          }
181        }
[5708]182      });
[5028]183    }
[5526]184
[6006]185    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
186    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
187    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
[5062]188    public void PauseJob(Guid jobId) {
[5708]189      trans.UseTransaction(() => {
[5779]190        var job = dao.GetJob(jobId);
[6168]191        if (job.State == JobState.Calculating || job.State == JobState.Transferring) {
192          job.Command = Command.Pause;
193          dao.UpdateJob(job);
194        } else {
195          job = UpdateJobState(jobId, JobState.Paused, null, null, string.Empty);
196        }
[5708]197      });
[5028]198    }
[5779]199
[6006]200    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
201    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
202    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
[5779]203    public void RestartJob(Guid jobId) {
204      trans.UseTransaction(() => {
205        Job job = dao.UpdateJobState(jobId, JobState.Waiting, null, auth.UserId, string.Empty);
206        job.Command = null;
207        dao.UpdateJob(job);
208      });
209    }
[5028]210    #endregion
211
212    #region HiveExperiment Methods
[6006]213    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
214    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
[5028]215    public HiveExperiment GetHiveExperiment(Guid id) {
[5511]216      return dao.GetHiveExperiments(x =>
217             x.HiveExperimentId == id
218          && (x.OwnerUserId == auth.UserId || x.HiveExperimentPermissions.Count(hep => hep.Permission != Permission.NotAllowed && hep.GrantedUserId == auth.UserId) > 0)
219          ).FirstOrDefault();
[5028]220    }
221
[6006]222    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
223    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
[5028]224    public IEnumerable<HiveExperiment> GetHiveExperiments() {
[5511]225      return dao.GetHiveExperiments(x => x.OwnerUserId == auth.UserId || x.HiveExperimentPermissions.Count(hep => hep.Permission != Permission.NotAllowed && hep.GrantedUserId == auth.UserId) > 0);
[5028]226    }
227
[6006]228    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]   
[5602]229    public IEnumerable<HiveExperiment> GetAllHiveExperiments() {
230      return dao.GetHiveExperiments(x => true);
231    }
232
[6006]233    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
234    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
[5028]235    public Guid AddHiveExperiment(HiveExperiment hiveExperimentDto) {
[5708]236      return trans.UseTransaction(() => {
[5511]237        hiveExperimentDto.OwnerUserId = auth.UserId;
[5106]238        hiveExperimentDto.DateCreated = DateTime.Now;
[5028]239        return dao.AddHiveExperiment(hiveExperimentDto);
[5708]240      });
[5028]241    }
242
[6006]243    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
244    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
[5028]245    public void UpdateHiveExperiment(HiveExperiment hiveExperimentDto) {
[5708]246      trans.UseTransaction(() => {
[5028]247        dao.UpdateHiveExperiment(hiveExperimentDto);
[5708]248      });
[5028]249    }
250
[6006]251    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
252    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
[5028]253    public void DeleteHiveExperiment(Guid hiveExperimentId) {
[5708]254      trans.UseTransaction(() => {
[5028]255        HiveExperiment he = dao.GetHiveExperiment(hiveExperimentId);
[5106]256        dao.DeleteHiveExperiment(hiveExperimentId); // child jobs will be deleted by db-trigger
[5708]257      });
[5028]258    }
259    #endregion
[5106]260
[5028]261    #region Login Methods
[6006]262    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
[5405]263    public void Hello(Slave slaveInfo) {
[5708]264      trans.UseTransaction(() => {
[5405]265        var slave = dao.GetSlave(slaveInfo.Id);
[5375]266
267        if (slave == null) {
[5405]268          dao.AddSlave(slaveInfo);
[5375]269        } else {
[6000]270          var dbSlave = dao.GetSlave(slaveInfo.Id);
271
272          dbSlave.Name = slaveInfo.Name;
273          dbSlave.Description = slaveInfo.Description;
274
275          dbSlave.Cores = slaveInfo.Cores;
276          dbSlave.CpuArchitecture = slaveInfo.CpuArchitecture;
277          dbSlave.CpuSpeed = slaveInfo.CpuSpeed;
278          dbSlave.FreeCores = slaveInfo.FreeCores;
279          dbSlave.FreeMemory = slaveInfo.FreeMemory;
280          dbSlave.Memory = slaveInfo.Memory;
281          dbSlave.OperatingSystem = slaveInfo.OperatingSystem;
[6267]282
283          dbSlave.LastHeartbeat = DateTime.Now;
[6000]284          dbSlave.SlaveState = SlaveState.Idle;
285
286          // don't update those properties:
287          // dbSlave.IsAllowedToCalculate = slaveInfo.IsAllowedToCalculate;
288          // dbSlave.ParentResourceId = slaveInfo.ParentResourceId;
289
290          dao.UpdateSlave(dbSlave);
[5375]291        }
[5708]292      });
[5053]293    }
[5028]294
[6006]295    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
[5375]296    public void GoodBye(Guid slaveId) {
[5708]297      trans.UseTransaction(() => {
[5375]298        var slave = dao.GetSlave(slaveId);
299        if (slave != null) {
300          slave.SlaveState = SlaveState.Offline;
301          dao.UpdateSlave(slave);
302        }
[5708]303      });
[5053]304    }
[5028]305    #endregion
306
307    #region Heartbeat Methods
[6006]308    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
[5053]309    public List<MessageContainer> Heartbeat(Heartbeat heartbeat) {
[5636]310      TriggerLifecycle(false);
[5708]311      return trans.UseTransaction(() => heartbeatManager.ProcessHeartbeat(heartbeat));
[5028]312    }
313    #endregion
314
315    #region Plugin Methods
[6006]316    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
317    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
[5028]318    public Guid AddPlugin(Plugin plugin, List<PluginData> pluginDatas) {
[5708]319      return trans.UseTransaction(() => {
[5402]320        plugin.UserId = auth.UserId;
321        plugin.DateCreated = DateTime.Now;
[6000]322        if (!plugin.IsLocal) {
323          var existing = dao.GetPlugins(x => x.Name == plugin.Name && x.Version == plugin.Version.ToString() && !x.IsLocal);
324          if (existing.Count() > 0) {
325            // a plugin with the same name and version already exists.
326            throw new FaultException<PluginAlreadyExistsFault>(new PluginAlreadyExistsFault(existing.Single().Id));
327          }
328        }
[5028]329        Guid pluginId = dao.AddPlugin(plugin);
330        foreach (PluginData pluginData in pluginDatas) {
331          pluginData.PluginId = pluginId;
332          dao.AddPluginData(pluginData);
333        }
334        return pluginId;
[5708]335      });
[5028]336    }
[5526]337
[6006]338    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
339    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
340    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
[6000]341    public Plugin GetPlugin(Guid pluginId) {
342      return dao.GetPlugin(pluginId);
343    }
344
[6006]345    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
346    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
347    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
[5053]348    public IEnumerable<Plugin> GetPlugins() {
[5593]349      return dao.GetPlugins(x => x.IsLocal == false);
[5028]350    }
[5375]351
[6006]352    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
353    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
354    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
[5053]355    public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
[5375]356      List<PluginData> pluginDatas = new List<PluginData>();
357
[5708]358      return trans.UseTransaction(() => {
[5375]359        foreach (Guid guid in pluginIds) {
360          List<PluginData> pluginData = dao.GetPluginDatas(x => x.PluginId == guid).ToList();
361          if (pluginData != null) {
362            pluginDatas.AddRange(pluginData);
363          } else {
364            //ignore ?
365          }
366        }
367        return pluginDatas;
[5708]368      });
[5028]369    }
[5375]370
[5028]371    #endregion
[5106]372
[5028]373    #region Slave Methods
[6006]374    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
375    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
[5028]376    public Guid AddSlave(Slave slave) {
[5708]377      return trans.UseTransaction(() => dao.AddSlave(slave));
[5028]378    }
379
[6006]380    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
381    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
[5028]382    public Guid AddSlaveGroup(SlaveGroup slaveGroup) {
[5708]383      return trans.UseTransaction(() => dao.AddSlaveGroup(slaveGroup));
[5028]384    }
385
[6006]386    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
387    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
[5106]388    public Slave GetSlave(Guid slaveId) {
389      return dao.GetSlave(slaveId);
390    }
391
392    public SlaveGroup GetSlaveGroup(Guid slaveGroupId) {
393      return dao.GetSlaveGroup(slaveGroupId);
394    }
395
[6006]396    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
397    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
[5028]398    public IEnumerable<Slave> GetSlaves() {
399      return dao.GetSlaves(x => true);
400    }
401
[6006]402    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
403    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
[5028]404    public IEnumerable<SlaveGroup> GetSlaveGroups() {
405      return dao.GetSlaveGroups(x => true);
406    }
407
[6006]408    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
409    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
[5106]410    public void UpdateSlave(Slave slave) {
[5708]411      trans.UseTransaction(() => {
[5106]412        dao.UpdateSlave(slave);
[5708]413      });
[5028]414    }
415
[6006]416    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
417    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
[5106]418    public void UpdateSlaveGroup(SlaveGroup slaveGroup) {
[5708]419      trans.UseTransaction(() => {
[5106]420        dao.UpdateSlaveGroup(slaveGroup);
[5708]421      });
[5028]422    }
423
[6006]424    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
425    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
[5106]426    public void DeleteSlave(Guid slaveId) {
[5708]427      trans.UseTransaction(() => {
[5106]428        dao.DeleteSlave(slaveId);
[5708]429      });
[5028]430    }
431
[6006]432    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
433    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
[5106]434    public void DeleteSlaveGroup(Guid slaveGroupId) {
[5708]435      trans.UseTransaction(() => {
[5106]436        dao.DeleteSlaveGroup(slaveGroupId);
[5708]437      });
[5028]438    }
[5106]439
[6006]440    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
441    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
[5106]442    public void AddResourceToGroup(Guid slaveGroupId, Guid resourceId) {
[5708]443      trans.UseTransaction(() => {
[5106]444        var resource = dao.GetResource(resourceId);
445        resource.ParentResourceId = slaveGroupId;
446        dao.UpdateResource(resource);
[5708]447      });
[5106]448    }
449
[6006]450    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
451    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
[5106]452    public void RemoveResourceFromGroup(Guid slaveGroupId, Guid resourceId) {
[5708]453      trans.UseTransaction(() => {
[5106]454        var resource = dao.GetResource(resourceId);
455        resource.ParentResourceId = null;
456        dao.UpdateResource(resource);
[5708]457      });
[5106]458    }
459
[6006]460    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
461    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
[5458]462    public Guid GetResourceId(string resourceName) {
[5708]463      return trans.UseTransaction(() => {
[5458]464        var resource = dao.GetResources(x => x.Name == resourceName).FirstOrDefault();
465        if (resource != null) {
466          return resource.Id;
467        } else {
468          return Guid.Empty;
469        }
[5708]470      });
[5458]471    }
[5593]472
[5636]473    public void TriggerLifecycle(bool force) {
[6267]474      // use a serializable transaction here to ensure not two threads execute this simultaniously (locking would not work since IIS may use multiple AppDomains)
[5708]475      trans.UseTransaction(() => {
[5593]476        DateTime lastCleanup = dao.GetLastCleanup();
[5636]477        if (force || DateTime.Now - lastCleanup > TimeSpan.FromSeconds(59)) {
[5593]478          dao.SetLastCleanup(DateTime.Now);
479          lifecycleManager.Cleanup();
480        }
[6267]481      }, true);
[5593]482    }
[5028]483    #endregion
484
485    #region Helper Methods
486    private IEnumerable<Job> GetChildJobs(Guid? parentJobId, bool recursive, bool includeParent) {
487      var jobs = new List<Job>(dao.GetJobs(x => parentJobId == null ? !x.ParentJobId.HasValue : x.ParentJobId.Value == parentJobId));
488
489      if (recursive) {
490        var childs = new List<Job>();
491        foreach (var job in jobs) {
492          childs.AddRange(GetChildJobs(job.Id, recursive, false));
493        }
494        jobs.AddRange(childs);
495      }
[5535]496
497      if (includeParent) jobs.Add(GetJob(parentJobId.Value));
498
[5028]499      return jobs;
500    }
501
502    #endregion
[5633]503
504    #region Appointment Methods
505
[6006]506    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
[5633]507    public Guid AddAppointment(Appointment appointment) {
[5708]508      return trans.UseTransaction(() => dao.AddAppointment(appointment));
[5633]509    }
510
[6006]511    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
[5633]512    public void DeleteAppointment(Guid appointmentId) {
[5708]513      trans.UseTransaction(() => {
[5633]514        dao.DeleteAppointment(appointmentId);
[5708]515      });
[5633]516    }
517
[6006]518    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
[5633]519    public void UpdateAppointment(Appointment appointment) {
[5708]520      trans.UseTransaction(() => {
[5633]521        dao.UpdateAppointment(appointment);
[5708]522      });
[5633]523    }
524
[6006]525    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
[5633]526    public IEnumerable<Appointment> GetScheduleForResource(Guid resourceId) {
[5708]527      return trans.UseTransaction(() => dao.GetAppointments(x => x.ResourceId == resourceId));
[5633]528    }
[6267]529
530    public IEnumerable<Job> GetJobsByResourceId(Guid resourceId) {
531      return trans.UseTransaction(() => dao.GetJobsByResourceId(resourceId));
532    }
[5633]533    #endregion
[5028]534  }
535}
Note: See TracBrowser for help on using the repository browser.