Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.Core/3.2/JobManager.cs @ 1627

Last change on this file since 1627 was 1627, checked in by msteinbi, 15 years ago

Implemented method GetAllJobResults in jobManager (#596)

File size: 11.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Hive.Contracts.Interfaces;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
28using HeuristicLab.Hive.Contracts;
29using HeuristicLab.Hive.Server.DataAccess;
30using HeuristicLab.Hive.Server.Core.InternalInterfaces;
31using HeuristicLab.DataAccess.Interfaces;
32
33namespace HeuristicLab.Hive.Server.Core {
34  class JobManager: IJobManager, IInternalJobManager {
35
36    ISessionFactory factory;
37    ILifecycleManager lifecycleManager;
38
39    #region IJobManager Members
40
41    public JobManager() {
42      factory = ServiceLocator.GetSessionFactory();
43      lifecycleManager = ServiceLocator.GetLifecycleManager();
44
45      lifecycleManager.RegisterStartup(new EventHandler(lifecycleManager_OnStartup));
46      lifecycleManager.RegisterStartup(new EventHandler(lifecycleManager_OnShutdown));
47    }
48
49    private JobResult GetLastJobResult(Job job) {
50      ISession session = factory.GetSessionForCurrentThread();
51
52      try {
53        IJobResultsAdapter jobResultAdapter =
54            session.GetDataAdapter<JobResult, IJobResultsAdapter>();
55
56        List<JobResult> allJobResults = new List<JobResult>(jobResultAdapter.GetResultsOf(job));
57        JobResult lastJobResult = null;
58        foreach (JobResult jR in allJobResults) {
59          // if lastJobResult was before the current jobResult the lastJobResult must be updated
60          if (lastJobResult == null ||
61              (jR.Timestamp > lastJobResult.Timestamp))
62            lastJobResult = jR;
63        }
64        return lastJobResult;
65      }
66      finally {
67        if (session != null)
68          session.EndSession();
69      }
70    }
71
72    public void ResetJobsDependingOnResults(Job job) {
73      ISession session = factory.GetSessionForCurrentThread();
74
75      try {
76        IJobAdapter jobAdapter =
77            session.GetDataAdapter<Job, IJobAdapter>();
78
79        JobResult lastJobResult = GetLastJobResult(job);
80        if (lastJobResult != null) {
81          job.Percentage = lastJobResult.Percentage;
82          job.SerializedJob = lastJobResult.Result;
83        } else {
84          job.Percentage = 0;
85        }
86
87        job.Client = null;
88        job.State = State.offline;
89
90        jobAdapter.Update(job);
91      }
92      finally {
93        if (session != null)
94          session.EndSession();
95      }
96    }
97
98    void checkForDeadJobs() {
99       ISession session = factory.GetSessionForCurrentThread();
100
101       try {
102         IJobAdapter jobAdapter =
103             session.GetDataAdapter<Job, IJobAdapter>();
104
105         List<Job> allJobs = new List<Job>(jobAdapter.GetAll());
106         foreach (Job curJob in allJobs) {
107           if (curJob.State == State.calculating) {
108             ResetJobsDependingOnResults(curJob);
109           }
110         }
111       }
112       finally {
113         if (session != null)
114           session.EndSession();
115       }
116    }
117
118    void lifecycleManager_OnStartup(object sender, EventArgs e) {
119      checkForDeadJobs();
120    }
121
122    void lifecycleManager_OnShutdown(object sender, EventArgs e) {
123      checkForDeadJobs();
124    }
125
126    /// <summary>
127    /// returns all jobs stored in the database
128    /// </summary>
129    /// <returns></returns>
130    public ResponseList<Job> GetAllJobs() {
131       ISession session = factory.GetSessionForCurrentThread();
132
133       try {
134         IJobAdapter jobAdapter =
135             session.GetDataAdapter<Job, IJobAdapter>();
136
137         ResponseList<Job> response = new ResponseList<Job>();
138
139         response.List = new List<Job>(jobAdapter.GetAll());
140         response.Success = true;
141         response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ALL_JOBS;
142
143         return response;
144       }
145       finally {
146         if (session != null)
147           session.EndSession();
148       }
149    }
150
151    /// <summary>
152    /// Adds a new job into the database
153    /// </summary>
154    /// <param name="job"></param>
155    /// <returns></returns>
156    public ResponseObject<Job> AddNewJob(Job job) {
157      ISession session = factory.GetSessionForCurrentThread();
158
159      try {
160        IJobAdapter jobAdapter =
161            session.GetDataAdapter<Job, IJobAdapter>();
162
163        ResponseObject<Job> response = new ResponseObject<Job>();
164
165        if (job != null) {
166          if (job.State != State.offline) {
167            response.Success = false;
168            response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOBSTATE_MUST_BE_OFFLINE;
169            return response;
170          }
171          if (job.Id != Guid.Empty) {
172            response.Success = false;
173            response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ID_MUST_NOT_BE_SET;
174            return response;
175          }
176          if (job.SerializedJob == null) {
177            response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_NULL;
178            response.Success = false;
179            return response;
180          }
181
182          job.DateCreated = DateTime.Now;
183          jobAdapter.Update(job);
184          response.Success = true;
185          response.Obj = job;
186          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_ADDED;
187        } else {
188          response.Success = false;
189          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_NULL;
190        }
191
192        return response;
193      }
194      finally {
195        if (session != null)
196          session.EndSession();
197      }
198    }
199
200    /// <summary>
201    /// Removes a job from the database
202    /// </summary>
203    /// <param name="jobId"></param>
204    /// <returns></returns>
205    public Response RemoveJob(Guid jobId) {
206      ISession session = factory.GetSessionForCurrentThread();
207
208      try {
209        IJobAdapter jobAdapter =
210            session.GetDataAdapter<Job, IJobAdapter>();
211        Response response = new Response();
212
213        Job job = jobAdapter.GetById(jobId);
214        if (job == null) {
215          response.Success = false;
216          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_DOESNT_EXIST;
217          return response;
218        }
219        jobAdapter.Delete(job);
220        response.Success = false;
221        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_REMOVED;
222
223        return response;
224      }
225      finally {
226        if (session != null)
227          session.EndSession();
228      }
229    }
230
231    public ResponseObject<JobResult> GetLastJobResultOf(Guid jobId, bool requested) {
232      ISession session = factory.GetSessionForCurrentThread();
233
234      try {
235        IJobAdapter jobAdapter =
236            session.GetDataAdapter<Job, IJobAdapter>();
237
238        ResponseObject<JobResult> response = new ResponseObject<JobResult>();
239        response.Success = true;
240        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_RESULT_SENT;
241        response.Obj = GetLastJobResult(jobAdapter.GetById(jobId));
242
243        return response;
244      }
245      finally {
246        if(session != null)
247          session.EndSession();
248      }
249    }
250
251    public Response RequestSnapshot(Guid jobId) {
252      ISession session = factory.GetSessionForCurrentThread();
253      Response response = new Response();
254     
255      try {
256        IJobAdapter jobAdapter = session.GetDataAdapter<Job, IJobAdapter>();
257
258        Job job = jobAdapter.GetById(jobId);
259        if (job.State == State.requestSnapshot) {
260          response.Success = true;
261          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_REQUEST_ALLREADY_SET;
262          return response; // no commit needed
263        }
264        if (job.State != State.calculating) {
265          response.Success = false;
266          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_IS_NOT_BEEING_CALCULATED;
267          return response; // no commit needed
268        }
269        // job is in correct state
270        job.State = State.requestSnapshot;
271        jobAdapter.Update(job);
272
273        response.Success = true;
274        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_REQUEST_SET;
275
276        return response;
277      }
278      finally {
279        if (session != null)
280          session.EndSession();
281      }
282    }
283
284    public Response AbortJob(Guid jobId) {
285      ISession session = factory.GetSessionForCurrentThread();
286      Response response = new Response();
287
288      try {
289        IJobAdapter jobAdapter = session.GetDataAdapter<Job, IJobAdapter>();
290
291        Job job = jobAdapter.GetById(jobId);
292        if (job.State == State.abort) {
293          response.Success = true;
294          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ABORT_REQUEST_ALLREADY_SET;
295          return response; // no commit needed
296        }
297        if (job.State != State.calculating) {
298          response.Success = false;
299          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_IS_NOT_BEEING_CALCULATED;
300          return response; // no commit needed
301        }
302        // job is in correct state
303        job.State = State.abort;
304        jobAdapter.Update(job);
305
306        response.Success = true;
307        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ABORT_REQUEST_SET;
308
309        return response;
310      }
311      finally {
312        if (session != null)
313          session.EndSession();
314      }
315    }
316
317    public ResponseObject<List<JobResult>> GetAllJobResults(Guid jobId) {
318      ISession session = factory.GetSessionForCurrentThread();
319      ResponseObject<List<JobResult>> response = new ResponseObject<List<JobResult>>();
320
321      try {
322        IJobResultsAdapter jobResultAdapter =
323            session.GetDataAdapter<JobResult, IJobResultsAdapter>();
324        IJobAdapter jobAdapter = session.GetDataAdapter<Job, IJobAdapter>();
325
326        Job job = jobAdapter.GetById(jobId);
327        if (job == null) {
328          response.Success = false;
329          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_DOESNT_EXIST;
330          return response;
331        }
332        response.Obj = new List<JobResult>(jobResultAdapter.GetResultsOf(job));
333        response.Success = true;
334        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_RESULT_SENT;
335
336        return response;
337      }
338      finally {
339        if(session != null)
340          session.EndSession();
341      }
342    }
343     
344    #endregion
345  }
346}
Note: See TracBrowser for help on using the repository browser.