Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1233

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