Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1233

  • minor changes
File size: 9.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.ServiceModel;
6using HeuristicLab.Services.Hive.Common.ServiceContracts;
7using HeuristicLab.Services.Hive.Common.DataTransfer;
8using System.IO;
9using System.Security.Permissions;
10using System.Data.Linq;
11using HeuristicLab.Services.Hive.Common;
12using System.Transactions;
13
14namespace HeuristicLab.Services.Hive {
15
16  /// <summary>
17  /// Implementation of the Hive service (interface <see cref="IHiveService"/>).
18  /// </summary>
19  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
20  public class HiveService : IHiveService {
21    private DataAccess.IHiveDao dao {
22      get { return ServiceLocator.Instance.HiveDao; }
23    }
24    private HeuristicLab.Services.Hive.DataAccess.TransactionManager trans {
25      get { return ServiceLocator.Instance.TransactionManager; }
26    }
27    private IAuthorizationManager auth {
28      get { return ServiceLocator.Instance.AuthorizationManager; }
29    }
30    private ILifecycleManager lifecycleManager {
31      get { return ServiceLocator.Instance.LifecycleManager; }
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> slaveGroupIds) {
38      using (trans.OpenTransaction()) {
39        job.UserId = auth.UserId;
40        job.DateCreated = DateTime.Now;
41        job.JobState = JobState.Waiting;
42        job.Id = dao.AddJob(job);
43        jobData.JobId = job.Id;
44        jobData.LastUpdate = DateTime.Now;
45        if (slaveGroupIds != null) {
46          foreach (Guid slaveGroupId in slaveGroupIds) {
47            dao.AssignJobToResource(job.Id, slaveGroupId);
48          }
49        } else {
50          // todo: use default group
51        }
52        dao.AddJobData(jobData);
53        return jobData.JobId;
54      }
55    }
56
57    public Guid AddChildJob(Guid parentJobId, Job job, JobData jobData) {
58      using (trans.OpenTransaction()) {
59        job.ParentJobId = parentJobId;
60        return AddJob(job, jobData, dao.GetAssignedResources(parentJobId).Select(x => x.Id));
61      }
62    }
63
64    public Job GetJob(Guid jobId) {
65      return dao.GetJob(jobId);
66    }
67
68    public IEnumerable<Job> GetJobs() {
69      return dao.GetJobs(x => true);
70    }
71
72    public IEnumerable<LightweightJob> GetLightweightJobs(IEnumerable<Guid> jobIds) {
73      return dao.GetJobs(x => jobIds.Contains(x.JobId)).Select(x => new LightweightJob(x)).ToArray();
74    }
75
76    public IEnumerable<LightweightJob> GetLightweightChildJobs(Guid? parentJobId, bool recursive, bool includeParent) {
77      return GetChildJobs(parentJobId, recursive, includeParent).Select(x => new LightweightJob(x)).ToArray();
78    }
79
80    public JobData GetJobData(Guid jobId) {
81      return dao.GetJobData(jobId);
82    }
83
84    public void UpdateJob(Job job, JobData jobData) {
85      using (trans.OpenTransaction()) {
86        jobData.LastUpdate = DateTime.Now;
87        dao.UpdateJob(job);
88        dao.UpdateJobData(jobData);
89      }
90    }
91
92    public void DeleteJob(Guid jobId) {
93      using (trans.OpenTransaction()) {
94        dao.DeleteJob(jobId);
95      }
96    }
97
98    public void DeleteChildJobs(Guid parentJobId) {
99      using (trans.OpenTransaction()) {
100        var jobs = GetChildJobs(parentJobId, true, false);
101        foreach (var job in jobs) {
102          dao.DeleteJob(job.Id);
103          dao.DeleteJobData(job.Id);
104        };
105      }
106    }
107
108    public Job AquireJob(Guid slaveId) {
109      using (trans.OpenTransaction()) {
110        var slave = dao.GetSlave(slaveId);
111        var availableJobs = dao.GetWaitingJobs(slave);
112        var job = availableJobs.FirstOrDefault();
113
114        if (job != null) {
115          job.SlaveId = slaveId;
116          job.JobState = JobState.Calculating;
117        }
118        return job;
119      }
120    }
121
122    public PluginData GetConfigurationFile() {
123      throw new NotImplementedException();
124    }
125
126    #endregion
127
128    #region Job Control Methods
129    public void StopJob(Guid jobId) {
130      using (trans.OpenTransaction()) {
131        throw new NotImplementedException();
132      }
133    }
134    public void PauseJob(Guid jobId) {
135      using (trans.OpenTransaction()) {
136        throw new NotImplementedException();
137      }
138    }
139    #endregion
140
141    #region HiveExperiment Methods
142
143    public HiveExperiment GetHiveExperiment(Guid id) {
144      return dao.GetHiveExperiments(x => x.UserId == auth.UserId && x.HiveExperimentId == id).FirstOrDefault();
145    }
146
147    public IEnumerable<HiveExperiment> GetHiveExperiments() {
148      return dao.GetHiveExperiments(x => x.UserId == auth.UserId);
149    }
150
151    public Guid AddHiveExperiment(HiveExperiment hiveExperimentDto) {
152      using (trans.OpenTransaction()) {
153        hiveExperimentDto.UserId = auth.UserId;
154        hiveExperimentDto.DateCreated = DateTime.Now;
155        return dao.AddHiveExperiment(hiveExperimentDto);
156      }
157    }
158
159    public void UpdateHiveExperiment(HiveExperiment hiveExperimentDto) {
160      using (trans.OpenTransaction()) {
161        dao.UpdateHiveExperiment(hiveExperimentDto);
162      }
163    }
164
165    public void DeleteHiveExperiment(Guid hiveExperimentId) {
166      using (trans.OpenTransaction()) {
167        HiveExperiment he = dao.GetHiveExperiment(hiveExperimentId);
168        dao.DeleteHiveExperiment(hiveExperimentId); // child jobs will be deleted by db-trigger
169      }
170    }
171    #endregion
172
173    #region Login Methods
174    public void Hello(Guid slaveId, string name, int cores, int memory) {
175      throw new NotImplementedException();
176    }
177
178    public void GoodBye() {
179      throw new NotImplementedException();
180    }
181    #endregion
182
183    #region Heartbeat Methods
184    public List<MessageContainer> Heartbeat(Heartbeat heartbeat) {
185      using (trans.OpenTransaction()) {
186        return lifecycleManager.ProcessHeartbeat(heartbeat);
187      }
188    }
189    #endregion
190
191    #region Plugin Methods
192    public Guid AddPlugin(Plugin plugin, List<PluginData> pluginDatas) {
193      using (trans.OpenTransaction()) {
194        Guid pluginId = dao.AddPlugin(plugin);
195        foreach (PluginData pluginData in pluginDatas) {
196          pluginData.PluginId = pluginId;
197          dao.AddPluginData(pluginData);
198        }
199        return pluginId;
200      }
201    }
202    public IEnumerable<Plugin> GetPlugins() {
203      return dao.GetPlugins(x => true);
204    }
205    public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
206      throw new NotImplementedException();
207    }
208    public Stream GetStreamedPluginDatas(List<Guid> pluginIds) {
209      throw new NotImplementedException();
210    }
211    #endregion
212
213    #region Slave Methods
214    public Guid AddSlave(Slave slave) {
215      using (trans.OpenTransaction()) {
216        return dao.AddSlave(slave);
217      }
218    }
219
220    public Guid AddSlaveGroup(SlaveGroup slaveGroup) {
221      using (trans.OpenTransaction()) {
222        return dao.AddSlaveGroup(slaveGroup);
223      }
224    }
225
226    public Slave GetSlave(Guid slaveId) {
227      return dao.GetSlave(slaveId);
228    }
229
230    public SlaveGroup GetSlaveGroup(Guid slaveGroupId) {
231      return dao.GetSlaveGroup(slaveGroupId);
232    }
233
234    public IEnumerable<Slave> GetSlaves() {
235      return dao.GetSlaves(x => true);
236    }
237
238    public IEnumerable<SlaveGroup> GetSlaveGroups() {
239      return dao.GetSlaveGroups(x => true);
240    }
241
242    public void UpdateSlave(Slave slave) {
243      using (trans.OpenTransaction()) {
244        dao.UpdateSlave(slave);
245      }
246    }
247
248    public void UpdateSlaveGroup(SlaveGroup slaveGroup) {
249      using (trans.OpenTransaction()) {
250        dao.UpdateSlaveGroup(slaveGroup);
251      }
252    }
253
254    public void DeleteSlave(Guid slaveId) {
255      using (trans.OpenTransaction()) {
256        dao.DeleteSlave(slaveId);
257      }
258    }
259
260    public void DeleteSlaveGroup(Guid slaveGroupId) {
261      using (trans.OpenTransaction()) {
262        dao.DeleteSlaveGroup(slaveGroupId);
263      }
264    }
265
266    public void AddResourceToGroup(Guid slaveGroupId, Guid resourceId) {
267      using (trans.OpenTransaction()) {
268        var resource = dao.GetResource(resourceId);
269        resource.ParentResourceId = slaveGroupId;
270        dao.UpdateResource(resource);
271      }
272    }
273
274    public void RemoveResourceFromGroup(Guid slaveGroupId, Guid resourceId) {
275      using (trans.OpenTransaction()) {
276        var resource = dao.GetResource(resourceId);
277        resource.ParentResourceId = null;
278        dao.UpdateResource(resource);
279      }
280    }
281
282    #endregion
283
284    #region Helper Methods
285    private IEnumerable<Job> GetChildJobs(Guid? parentJobId, bool recursive, bool includeParent) {
286      var jobs = new List<Job>(dao.GetJobs(x => parentJobId == null ? !x.ParentJobId.HasValue : x.ParentJobId.Value == parentJobId));
287
288      if (includeParent) {
289        jobs.Add(GetJob(parentJobId.Value));
290      }
291
292      if (recursive) {
293        var childs = new List<Job>();
294        foreach (var job in jobs) {
295          childs.AddRange(GetChildJobs(job.Id, recursive, false));
296        }
297        jobs.AddRange(childs);
298      }
299      return jobs;
300    }
301
302    #endregion
303 
304  }
305}
Note: See TracBrowser for help on using the repository browser.