Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5602 was 5602, checked in by ascheibe, 12 years ago

#1233

  • added partial classes for dtos
  • added About dialog for Slave tray icon
  • added GetAllHiveExperiments method to service
File size: 16.7 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.SetState(JobState.Waiting, auth.UserId);
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        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    #endregion
138
139    #region Job Control Methods
140    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
141    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
142    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
143    public void StopJob(Guid jobId) {
144      using (trans.OpenTransaction()) {
145        throw new NotImplementedException();
146      }
147    }
148
149    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
150    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
151    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
152    public void PauseJob(Guid jobId) {
153      using (trans.OpenTransaction()) {
154        throw new NotImplementedException();
155      }
156    }
157    #endregion
158
159    #region HiveExperiment Methods
160    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
161    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
162    public HiveExperiment GetHiveExperiment(Guid id) {
163      return dao.GetHiveExperiments(x =>
164             x.HiveExperimentId == id
165          && (x.OwnerUserId == auth.UserId || x.HiveExperimentPermissions.Count(hep => hep.Permission != Permission.NotAllowed && hep.GrantedUserId == auth.UserId) > 0)
166          ).FirstOrDefault();
167    }
168
169    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
170    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
171    public IEnumerable<HiveExperiment> GetHiveExperiments() {
172      return dao.GetHiveExperiments(x => x.OwnerUserId == auth.UserId || x.HiveExperimentPermissions.Count(hep => hep.Permission != Permission.NotAllowed && hep.GrantedUserId == auth.UserId) > 0);
173    }
174
175    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]   
176    public IEnumerable<HiveExperiment> GetAllHiveExperiments() {
177      return dao.GetHiveExperiments(x => true);
178    }
179
180    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
181    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
182    public Guid AddHiveExperiment(HiveExperiment hiveExperimentDto) {
183      using (trans.OpenTransaction()) {
184        hiveExperimentDto.OwnerUserId = auth.UserId;
185        hiveExperimentDto.DateCreated = DateTime.Now;
186        return dao.AddHiveExperiment(hiveExperimentDto);
187      }
188    }
189
190    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
191    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
192    public void UpdateHiveExperiment(HiveExperiment hiveExperimentDto) {
193      using (trans.OpenTransaction()) {
194        dao.UpdateHiveExperiment(hiveExperimentDto);
195      }
196    }
197
198    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
199    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
200    public void DeleteHiveExperiment(Guid hiveExperimentId) {
201      using (trans.OpenTransaction()) {
202        HiveExperiment he = dao.GetHiveExperiment(hiveExperimentId);
203        dao.DeleteHiveExperiment(hiveExperimentId); // child jobs will be deleted by db-trigger
204      }
205    }
206    #endregion
207
208    #region Login Methods
209    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
210    public void Hello(Slave slaveInfo) {
211      using (trans.OpenTransaction()) {
212        var slave = dao.GetSlave(slaveInfo.Id);
213
214        if (slave == null) {
215          dao.AddSlave(slaveInfo);
216        } else {
217          dao.UpdateSlave(slaveInfo);
218        }
219      }
220    }
221
222    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
223    public void GoodBye(Guid slaveId) {
224      using (trans.OpenTransaction()) {
225        var slave = dao.GetSlave(slaveId);
226        if (slave != null) {
227          slave.SlaveState = SlaveState.Offline;
228          dao.UpdateSlave(slave);
229        }
230      }
231    }
232    #endregion
233
234    #region Heartbeat Methods
235    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
236    public List<MessageContainer> Heartbeat(Heartbeat heartbeat) {
237      TriggerLifecycle();
238
239      using (trans.OpenTransaction()) {
240        return heartbeatManager.ProcessHeartbeat(heartbeat);
241      }
242    }
243    #endregion
244
245    #region Plugin Methods
246    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
247    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
248    public Guid AddPlugin(Plugin plugin, List<PluginData> pluginDatas) {
249      using (trans.OpenTransaction()) {
250        plugin.UserId = auth.UserId;
251        plugin.DateCreated = DateTime.Now;
252        Guid pluginId = dao.AddPlugin(plugin);
253        foreach (PluginData pluginData in pluginDatas) {
254          pluginData.PluginId = pluginId;
255          dao.AddPluginData(pluginData);
256        }
257        return pluginId;
258      }
259    }
260
261    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
262    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
263    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
264    public IEnumerable<Plugin> GetPlugins() {
265      return dao.GetPlugins(x => x.IsLocal == false);
266    }
267
268    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
269    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
270    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
271    public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
272      List<PluginData> pluginDatas = new List<PluginData>();
273
274      using (trans.OpenTransaction()) {
275        foreach (Guid guid in pluginIds) {
276          List<PluginData> pluginData = dao.GetPluginDatas(x => x.PluginId == guid).ToList();
277          if (pluginData != null) {
278            pluginDatas.AddRange(pluginData);
279          } else {
280            //ignore ?
281          }
282        }
283        return pluginDatas;
284      }
285    }
286
287    #endregion
288
289    #region Slave Methods
290    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
291    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
292    public Guid AddSlave(Slave slave) {
293      using (trans.OpenTransaction()) {
294        return dao.AddSlave(slave);
295      }
296    }
297
298    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
299    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
300    public Guid AddSlaveGroup(SlaveGroup slaveGroup) {
301      using (trans.OpenTransaction()) {
302        return dao.AddSlaveGroup(slaveGroup);
303      }
304    }
305
306    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
307    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
308    public Slave GetSlave(Guid slaveId) {
309      return dao.GetSlave(slaveId);
310    }
311
312    public SlaveGroup GetSlaveGroup(Guid slaveGroupId) {
313      return dao.GetSlaveGroup(slaveGroupId);
314    }
315
316    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
317    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
318    public IEnumerable<Slave> GetSlaves() {
319      return dao.GetSlaves(x => true);
320    }
321
322    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
323    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
324    public IEnumerable<SlaveGroup> GetSlaveGroups() {
325      return dao.GetSlaveGroups(x => true);
326    }
327
328    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
329    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
330    public void UpdateSlave(Slave slave) {
331      using (trans.OpenTransaction()) {
332        dao.UpdateSlave(slave);
333      }
334    }
335
336    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
337    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
338    public void UpdateSlaveGroup(SlaveGroup slaveGroup) {
339      using (trans.OpenTransaction()) {
340        dao.UpdateSlaveGroup(slaveGroup);
341      }
342    }
343
344    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
345    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
346    public void DeleteSlave(Guid slaveId) {
347      using (trans.OpenTransaction()) {
348        dao.DeleteSlave(slaveId);
349      }
350    }
351
352    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
353    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
354    public void DeleteSlaveGroup(Guid slaveGroupId) {
355      using (trans.OpenTransaction()) {
356        dao.DeleteSlaveGroup(slaveGroupId);
357      }
358    }
359
360    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
361    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
362    public void AddResourceToGroup(Guid slaveGroupId, Guid resourceId) {
363      using (trans.OpenTransaction()) {
364        var resource = dao.GetResource(resourceId);
365        resource.ParentResourceId = slaveGroupId;
366        dao.UpdateResource(resource);
367      }
368    }
369
370    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
371    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
372    public void RemoveResourceFromGroup(Guid slaveGroupId, Guid resourceId) {
373      using (trans.OpenTransaction()) {
374        var resource = dao.GetResource(resourceId);
375        resource.ParentResourceId = null;
376        dao.UpdateResource(resource);
377      }
378    }
379
380    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
381    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
382    public Guid GetResourceId(string resourceName) {
383      using (trans.OpenTransaction()) {
384        var resource = dao.GetResources(x => x.Name == resourceName).FirstOrDefault();
385        if (resource != null) {
386          return resource.Id;
387        } else {
388          return Guid.Empty;
389        }
390      }
391    }
392
393    public void TriggerLifecycle() {
394      using (trans.OpenTransaction()) {
395        DateTime lastCleanup = dao.GetLastCleanup();
396        if (DateTime.Now - lastCleanup > TimeSpan.FromSeconds(59)) {
397          dao.SetLastCleanup(DateTime.Now);
398          lifecycleManager.Cleanup();
399        }
400      }
401    }
402    #endregion
403
404    #region Helper Methods
405    private IEnumerable<Job> GetChildJobs(Guid? parentJobId, bool recursive, bool includeParent) {
406      var jobs = new List<Job>(dao.GetJobs(x => parentJobId == null ? !x.ParentJobId.HasValue : x.ParentJobId.Value == parentJobId));
407
408      if (recursive) {
409        var childs = new List<Job>();
410        foreach (var job in jobs) {
411          childs.AddRange(GetChildJobs(job.Id, recursive, false));
412        }
413        jobs.AddRange(childs);
414      }
415
416      if (includeParent) jobs.Add(GetJob(parentJobId.Value));
417
418      return jobs;
419    }
420
421    #endregion
422  }
423}
Note: See TracBrowser for help on using the repository browser.