Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1233

  • updated jobstates documentation
  • enhanced ganttChart
  • fixed setting of jobstates
  • added option to force lifecycle-trigger (mainly for testing purposes)
File size: 18.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.ServiceModel;
5using HeuristicLab.Services.Hive.Common;
6using HeuristicLab.Services.Hive.Common.DataTransfer;
7using HeuristicLab.Services.Hive.Common.ServiceContracts;
8
9namespace HeuristicLab.Services.Hive {
10
11  /// <summary>
12  /// Implementation of the Hive service (interface <see cref="IHiveService"/>).
13  /// We need 'IgnoreExtensionDataObject' Attribute for the slave to work.
14  /// </summary>
15  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, IgnoreExtensionDataObject = true)]
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    }
26    private ILifecycleManager lifecycleManager {
27      get { return ServiceLocator.Instance.LifecycleManager; }
28    }
29    private HeartbeatManager heartbeatManager {
30      get { return ServiceLocator.Instance.HeartbeatManager; }
31    }
32
33    #region Job Methods
34    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
35    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
36    public Guid AddJob(Job job, JobData jobData, IEnumerable<Guid> resourceIds) {
37      using (trans.OpenTransaction()) {
38        job.Id = dao.AddJob(job);
39        jobData.JobId = job.Id;
40        jobData.LastUpdate = DateTime.Now;
41        if (resourceIds != null) {
42          foreach (Guid slaveGroupId in resourceIds) {
43            dao.AssignJobToResource(job.Id, slaveGroupId);
44          }
45        } else {
46          // todo: use default group
47        }
48        dao.AddJobData(jobData);
49        dao.UpdateJobState(job.Id, JobState.Waiting, null, auth.UserId, null);
50        return jobData.JobId;
51      }
52    }
53
54    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
55    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
56    public Guid AddChildJob(Guid parentJobId, Job job, JobData jobData) {
57      using (trans.OpenTransaction()) {
58        job.ParentJobId = parentJobId;
59        return AddJob(job, jobData, dao.GetAssignedResources(parentJobId).Select(x => x.Id));
60      }
61    }
62
63    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
64    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
65    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
66    public Job GetJob(Guid jobId) {
67      return dao.GetJob(jobId);
68    }
69
70    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
71    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
72    public IEnumerable<Job> GetJobs() {
73      return dao.GetJobs(x => true);
74    }
75
76    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
77    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
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
82    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
83    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
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
88    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
89    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
90    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
91    public JobData GetJobData(Guid jobId) {
92      return dao.GetJobData(jobId);
93    }
94
95    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
96    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
97    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
98    public void UpdateJob(Job job) {
99      using (trans.OpenTransaction()) {
100        dao.UpdateJob(job);
101      }
102    }
103
104    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
105    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
106    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
107    public void UpdateJobData(Job job, JobData jobData) {
108      using (trans.OpenTransaction()) {
109        jobData.LastUpdate = DateTime.Now;
110        dao.UpdateJob(job);
111        dao.UpdateJobData(jobData);
112      }
113    }
114
115    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
116    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
117    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
118    public void DeleteJob(Guid jobId) {
119      using (trans.OpenTransaction()) {
120        dao.DeleteJob(jobId);
121      }
122    }
123
124    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
125    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
126    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
127    public void DeleteChildJobs(Guid parentJobId) {
128      using (trans.OpenTransaction()) {
129        var jobs = GetChildJobs(parentJobId, true, false);
130        foreach (var job in jobs) {
131          dao.DeleteJob(job.Id);
132          dao.DeleteJobData(job.Id);
133        };
134      }
135    }
136
137    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
138    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
139    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
140    public Job UpdateJobState(Guid jobId, JobState jobState, Guid? slaveId, Guid? userId, string exception) {
141      using (trans.OpenTransaction()) {
142        return dao.UpdateJobState(jobId, jobState, slaveId, userId, exception);
143      }
144    }
145    #endregion
146
147    #region Job Control Methods
148    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
149    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
150    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
151    public void StopJob(Guid jobId) {
152      using (trans.OpenTransaction()) {
153        throw new NotImplementedException();
154      }
155    }
156
157    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
158    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
159    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
160    public void PauseJob(Guid jobId) {
161      using (trans.OpenTransaction()) {
162        throw new NotImplementedException();
163      }
164    }
165    #endregion
166
167    #region HiveExperiment Methods
168    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
169    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
170    public HiveExperiment GetHiveExperiment(Guid id) {
171      return dao.GetHiveExperiments(x =>
172             x.HiveExperimentId == id
173          && (x.OwnerUserId == auth.UserId || x.HiveExperimentPermissions.Count(hep => hep.Permission != Permission.NotAllowed && hep.GrantedUserId == auth.UserId) > 0)
174          ).FirstOrDefault();
175    }
176
177    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
178    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
179    public IEnumerable<HiveExperiment> GetHiveExperiments() {
180      return dao.GetHiveExperiments(x => x.OwnerUserId == auth.UserId || x.HiveExperimentPermissions.Count(hep => hep.Permission != Permission.NotAllowed && hep.GrantedUserId == auth.UserId) > 0);
181    }
182
183    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]   
184    public IEnumerable<HiveExperiment> GetAllHiveExperiments() {
185      return dao.GetHiveExperiments(x => true);
186    }
187
188    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
189    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
190    public Guid AddHiveExperiment(HiveExperiment hiveExperimentDto) {
191      using (trans.OpenTransaction()) {
192        hiveExperimentDto.OwnerUserId = auth.UserId;
193        hiveExperimentDto.DateCreated = DateTime.Now;
194        return dao.AddHiveExperiment(hiveExperimentDto);
195      }
196    }
197
198    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
199    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
200    public void UpdateHiveExperiment(HiveExperiment hiveExperimentDto) {
201      using (trans.OpenTransaction()) {
202        dao.UpdateHiveExperiment(hiveExperimentDto);
203      }
204    }
205
206    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
207    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
208    public void DeleteHiveExperiment(Guid hiveExperimentId) {
209      using (trans.OpenTransaction()) {
210        HiveExperiment he = dao.GetHiveExperiment(hiveExperimentId);
211        dao.DeleteHiveExperiment(hiveExperimentId); // child jobs will be deleted by db-trigger
212      }
213    }
214    #endregion
215
216    #region Login Methods
217    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
218    public void Hello(Slave slaveInfo) {
219      using (trans.OpenTransaction()) {
220        var slave = dao.GetSlave(slaveInfo.Id);
221
222        if (slave == null) {
223          dao.AddSlave(slaveInfo);
224        } else {
225          dao.UpdateSlave(slaveInfo);
226        }
227      }
228    }
229
230    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
231    public void GoodBye(Guid slaveId) {
232      using (trans.OpenTransaction()) {
233        var slave = dao.GetSlave(slaveId);
234        if (slave != null) {
235          slave.SlaveState = SlaveState.Offline;
236          dao.UpdateSlave(slave);
237        }
238      }
239    }
240    #endregion
241
242    #region Heartbeat Methods
243    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
244    public List<MessageContainer> Heartbeat(Heartbeat heartbeat) {
245      TriggerLifecycle(false);
246
247      using (trans.OpenTransaction()) {
248        return heartbeatManager.ProcessHeartbeat(heartbeat);
249      }
250    }
251    #endregion
252
253    #region Plugin Methods
254    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
255    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
256    public Guid AddPlugin(Plugin plugin, List<PluginData> pluginDatas) {
257      using (trans.OpenTransaction()) {
258        plugin.UserId = auth.UserId;
259        plugin.DateCreated = DateTime.Now;
260        Guid pluginId = dao.AddPlugin(plugin);
261        foreach (PluginData pluginData in pluginDatas) {
262          pluginData.PluginId = pluginId;
263          dao.AddPluginData(pluginData);
264        }
265        return pluginId;
266      }
267    }
268
269    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
270    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
271    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
272    public IEnumerable<Plugin> GetPlugins() {
273      return dao.GetPlugins(x => x.IsLocal == false);
274    }
275
276    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
277    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
278    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
279    public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
280      List<PluginData> pluginDatas = new List<PluginData>();
281
282      using (trans.OpenTransaction()) {
283        foreach (Guid guid in pluginIds) {
284          List<PluginData> pluginData = dao.GetPluginDatas(x => x.PluginId == guid).ToList();
285          if (pluginData != null) {
286            pluginDatas.AddRange(pluginData);
287          } else {
288            //ignore ?
289          }
290        }
291        return pluginDatas;
292      }
293    }
294
295    #endregion
296
297    #region Slave Methods
298    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
299    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
300    public Guid AddSlave(Slave slave) {
301      using (trans.OpenTransaction()) {
302        return dao.AddSlave(slave);
303      }
304    }
305
306    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
307    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
308    public Guid AddSlaveGroup(SlaveGroup slaveGroup) {
309      using (trans.OpenTransaction()) {
310        return dao.AddSlaveGroup(slaveGroup);
311      }
312    }
313
314    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
315    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
316    public Slave GetSlave(Guid slaveId) {
317      return dao.GetSlave(slaveId);
318    }
319
320    public SlaveGroup GetSlaveGroup(Guid slaveGroupId) {
321      return dao.GetSlaveGroup(slaveGroupId);
322    }
323
324    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
325    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
326    public IEnumerable<Slave> GetSlaves() {
327      return dao.GetSlaves(x => true);
328    }
329
330    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
331    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
332    public IEnumerable<SlaveGroup> GetSlaveGroups() {
333      return dao.GetSlaveGroups(x => true);
334    }
335
336    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
337    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
338    public void UpdateSlave(Slave slave) {
339      using (trans.OpenTransaction()) {
340        dao.UpdateSlave(slave);
341      }
342    }
343
344    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
345    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
346    public void UpdateSlaveGroup(SlaveGroup slaveGroup) {
347      using (trans.OpenTransaction()) {
348        dao.UpdateSlaveGroup(slaveGroup);
349      }
350    }
351
352    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
353    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
354    public void DeleteSlave(Guid slaveId) {
355      using (trans.OpenTransaction()) {
356        dao.DeleteSlave(slaveId);
357      }
358    }
359
360    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
361    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
362    public void DeleteSlaveGroup(Guid slaveGroupId) {
363      using (trans.OpenTransaction()) {
364        dao.DeleteSlaveGroup(slaveGroupId);
365      }
366    }
367
368    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
369    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
370    public void AddResourceToGroup(Guid slaveGroupId, Guid resourceId) {
371      using (trans.OpenTransaction()) {
372        var resource = dao.GetResource(resourceId);
373        resource.ParentResourceId = slaveGroupId;
374        dao.UpdateResource(resource);
375      }
376    }
377
378    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
379    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
380    public void RemoveResourceFromGroup(Guid slaveGroupId, Guid resourceId) {
381      using (trans.OpenTransaction()) {
382        var resource = dao.GetResource(resourceId);
383        resource.ParentResourceId = null;
384        dao.UpdateResource(resource);
385      }
386    }
387
388    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
389    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
390    public Guid GetResourceId(string resourceName) {
391      using (trans.OpenTransaction()) {
392        var resource = dao.GetResources(x => x.Name == resourceName).FirstOrDefault();
393        if (resource != null) {
394          return resource.Id;
395        } else {
396          return Guid.Empty;
397        }
398      }
399    }
400
401    public void TriggerLifecycle(bool force) {
402      using (trans.OpenTransaction()) {
403        DateTime lastCleanup = dao.GetLastCleanup();
404        if (force || DateTime.Now - lastCleanup > TimeSpan.FromSeconds(59)) {
405          dao.SetLastCleanup(DateTime.Now);
406          lifecycleManager.Cleanup();
407        }
408      }
409    }
410    #endregion
411
412    #region Helper Methods
413    private IEnumerable<Job> GetChildJobs(Guid? parentJobId, bool recursive, bool includeParent) {
414      var jobs = new List<Job>(dao.GetJobs(x => parentJobId == null ? !x.ParentJobId.HasValue : x.ParentJobId.Value == parentJobId));
415
416      if (recursive) {
417        var childs = new List<Job>();
418        foreach (var job in jobs) {
419          childs.AddRange(GetChildJobs(job.Id, recursive, false));
420        }
421        jobs.AddRange(childs);
422      }
423
424      if (includeParent) jobs.Add(GetJob(parentJobId.Value));
425
426      return jobs;
427    }
428
429    #endregion
430
431    #region Appointment Methods
432
433    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
434    public Guid AddAppointment(Appointment appointment) {
435      using (trans.OpenTransaction()) {
436        return dao.AddAppointment(appointment);
437      }
438    }
439
440    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
441    public void DeleteAppointment(Guid appointmentId) {
442      using (trans.OpenTransaction()) {
443        dao.DeleteAppointment(appointmentId);
444      }
445    }
446
447    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
448    public void UpdateAppointment(Appointment appointment) {
449      using (trans.OpenTransaction()) {
450        dao.UpdateAppointment(appointment);
451      }
452    }
453
454    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
455    public IEnumerable<Appointment> GetScheduleForResource(Guid resourceId) {
456      using (trans.OpenTransaction()) {
457        return dao.GetAppointments(x => x.ResourceId == resourceId);
458      }
459    }
460    #endregion
461  }
462}
Note: See TracBrowser for help on using the repository browser.