Free cookie consent management tool by TermsFeed Policy Generator

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

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

corrected job state handling (#466)

File size: 11.6 KB
RevLine 
[907]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;
[800]23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Hive.Contracts.Interfaces;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
[902]28using HeuristicLab.Hive.Contracts;
[1377]29using HeuristicLab.Hive.Server.DataAccess;
[1141]30using HeuristicLab.Hive.Server.Core.InternalInterfaces;
[1468]31using HeuristicLab.DataAccess.Interfaces;
[800]32
33namespace HeuristicLab.Hive.Server.Core {
[1141]34  class JobManager: IJobManager, IInternalJobManager {
[820]35
[1468]36    ISessionFactory factory;
[1133]37    ILifecycleManager lifecycleManager;
[820]38
[800]39    #region IJobManager Members
40
[820]41    public JobManager() {
[1468]42      factory = ServiceLocator.GetSessionFactory();
[1133]43      lifecycleManager = ServiceLocator.GetLifecycleManager();
44
45      lifecycleManager.RegisterStartup(new EventHandler(lifecycleManager_OnStartup));
46      lifecycleManager.RegisterStartup(new EventHandler(lifecycleManager_OnShutdown));
[820]47    }
48
[1170]49    private JobResult GetLastJobResult(Job job) {
[1468]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 ||
[1626]61              (jR.Timestamp > lastJobResult.Timestamp))
[1468]62            lastJobResult = jR;
63        }
64        return lastJobResult;
[1139]65      }
[1468]66      finally {
67        if (session != null)
68          session.EndSession();
69      }
[1170]70    }
71
72    public void ResetJobsDependingOnResults(Job job) {
[1468]73      ISession session = factory.GetSessionForCurrentThread();
[1160]74
[1468]75      try {
76        IJobAdapter jobAdapter =
77            session.GetDataAdapter<Job, IJobAdapter>();
[1160]78
[1468]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      }
[1139]96    }
97
[1133]98    void checkForDeadJobs() {
[1468]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       }
[1133]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
[1121]126    /// <summary>
127    /// returns all jobs stored in the database
128    /// </summary>
129    /// <returns></returns>
[902]130    public ResponseList<Job> GetAllJobs() {
[1468]131       ISession session = factory.GetSessionForCurrentThread();
[967]132
[1468]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       }
[800]149    }
150
[1121]151    /// <summary>
152    /// Adds a new job into the database
153    /// </summary>
154    /// <param name="job"></param>
155    /// <returns></returns>
[967]156    public ResponseObject<Job> AddNewJob(Job job) {
[1468]157      ISession session = factory.GetSessionForCurrentThread();
[967]158
[1468]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 {
[1024]188          response.Success = false;
[1120]189          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_NULL;
190        }
191
[1468]192        return response;
[967]193      }
[1468]194      finally {
195        if (session != null)
196          session.EndSession();
197      }
[967]198    }
199
[1121]200    /// <summary>
201    /// Removes a job from the database
202    /// </summary>
203    /// <param name="jobId"></param>
204    /// <returns></returns>
[1449]205    public Response RemoveJob(Guid jobId) {
[1468]206      ISession session = factory.GetSessionForCurrentThread();
[967]207
[1468]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);
[967]220        response.Success = false;
[1468]221        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_REMOVED;
222
[967]223        return response;
224      }
[1468]225      finally {
226        if (session != null)
227          session.EndSession();
228      }
[967]229    }
230
[1509]231    public ResponseObject<JobResult> GetLastJobResultOf(Guid jobId, bool requested) {
[1468]232      ISession session = factory.GetSessionForCurrentThread();
[1170]233
[1468]234      try {
235        IJobAdapter jobAdapter =
236            session.GetDataAdapter<Job, IJobAdapter>();
237
238        ResponseObject<JobResult> response = new ResponseObject<JobResult>();
[1770]239
240        Job job = jobAdapter.GetById(jobId);
[1772]241        if (requested && (job.State == State.requestSnapshot || job.State == State.requestSnapshotSent)) {
[1770]242          response.Success = true;
243          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_RESULT_NOT_YET_HERE;
244          return response;
245        }
246         
[1468]247        response.Success = true;
248        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_RESULT_SENT;
[1770]249        response.Obj = GetLastJobResult(job);
[1468]250
251        return response;
252      }
253      finally {
254        if(session != null)
255          session.EndSession();
256      }
[1170]257    }
258
[1509]259    public Response RequestSnapshot(Guid jobId) {
260      ISession session = factory.GetSessionForCurrentThread();
261      Response response = new Response();
262     
263      try {
[1577]264        IJobAdapter jobAdapter = session.GetDataAdapter<Job, IJobAdapter>();
[1509]265
[1577]266        Job job = jobAdapter.GetById(jobId);
[1831]267        if (job.State == State.requestSnapshot || job.State == State.requestSnapshotSent) {
[1577]268          response.Success = true;
269          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_REQUEST_ALLREADY_SET;
270          return response; // no commit needed
271        }
272        if (job.State != State.calculating) {
273          response.Success = false;
274          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_IS_NOT_BEEING_CALCULATED;
275          return response; // no commit needed
276        }
277        // job is in correct state
278        job.State = State.requestSnapshot;
279        jobAdapter.Update(job);
280
281        response.Success = true;
282        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_REQUEST_SET;
283
[1509]284        return response;
285      }
286      finally {
287        if (session != null)
288          session.EndSession();
289      }
290    }
291
292    public Response AbortJob(Guid jobId) {
[1577]293      ISession session = factory.GetSessionForCurrentThread();
294      Response response = new Response();
295
296      try {
297        IJobAdapter jobAdapter = session.GetDataAdapter<Job, IJobAdapter>();
298
299        Job job = jobAdapter.GetById(jobId);
[1762]300        if (job == null) {
301          response.Success = false;
302          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_DOESNT_EXIST;
303          return response; // no commit needed
304        }
[1577]305        if (job.State == State.abort) {
306          response.Success = true;
307          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ABORT_REQUEST_ALLREADY_SET;
308          return response; // no commit needed
309        }
[1811]310        if (job.State != State.calculating && job.State != State.requestSnapshot && job.State != State.requestSnapshotSent) {
[1577]311          response.Success = false;
312          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_IS_NOT_BEEING_CALCULATED;
313          return response; // no commit needed
314        }
315        // job is in correct state
316        job.State = State.abort;
317        jobAdapter.Update(job);
318
319        response.Success = true;
320        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ABORT_REQUEST_SET;
321
322        return response;
323      }
324      finally {
325        if (session != null)
326          session.EndSession();
327      }
[1509]328    }
329
[1799]330    public ResponseList<JobResult> GetAllJobResults(Guid jobId) {
[1627]331      ISession session = factory.GetSessionForCurrentThread();
[1799]332      ResponseList<JobResult> response = new ResponseList<JobResult>();
[1627]333
334      try {
335        IJobResultsAdapter jobResultAdapter =
336            session.GetDataAdapter<JobResult, IJobResultsAdapter>();
337        IJobAdapter jobAdapter = session.GetDataAdapter<Job, IJobAdapter>();
338
339        Job job = jobAdapter.GetById(jobId);
340        if (job == null) {
341          response.Success = false;
342          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_DOESNT_EXIST;
343          return response;
344        }
[1799]345        response.List = new List<JobResult>(jobResultAdapter.GetResultsOf(job));
[1627]346        response.Success = true;
347        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_RESULT_SENT;
348
349        return response;
350      }
351      finally {
352        if(session != null)
353          session.EndSession();
354      }
355    }
356     
[800]357    #endregion
358  }
359}
Note: See TracBrowser for help on using the repository browser.