Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3578 was 3578, checked in by kgrading, 14 years ago

Removed References to HiveLogging and updated the default logging mechanism (#991)

File size: 12.9 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;
32using System.Data;
33using System.IO;
34using HeuristicLab.Tracing;
35using System.Transactions;
36using HeuristicLab.Hive.Server.LINQDataAccess;
37using IsolationLevel=System.Transactions.IsolationLevel;
38
39namespace HeuristicLab.Hive.Server.Core {
40  class JobManager: IJobManager, IInternalJobManager {
41
42    //ISessionFactory factory;
43    ILifecycleManager lifecycleManager;
44
45    #region IJobManager Members
46
47    public JobManager() {
48      //factory = ServiceLocator.GetSessionFactory();
49      lifecycleManager = ServiceLocator.GetLifecycleManager();
50
51      lifecycleManager.RegisterStartup(new EventHandler(lifecycleManager_OnStartup));
52      lifecycleManager.RegisterStartup(new EventHandler(lifecycleManager_OnShutdown));
53    }
54
55    private JobDto GetLastJobResult(Guid jobId) {     
56      return DaoLocator.JobDao.FindById(jobId);
57    }
58
59    public void ResetJobsDependingOnResults(JobDto job) {
60      Logger.Info("Setting job " + job.Id + " offline");
61      if (job != null) {
62        DaoLocator.JobDao.SetJobOffline(job);
63      }
64    }
65         
66
67    void checkForDeadJobs() {
68      Logger.Info("Searching for dead Jobs");
69      using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = ApplicationConstants.ISOLATION_LEVEL_SCOPE })) {
70        List<JobDto> allJobs = new List<JobDto>(DaoLocator.JobDao.FindAll());
71        foreach (JobDto curJob in allJobs) {
72          if (curJob.State != State.calculating) {
73            ResetJobsDependingOnResults(curJob);
74          }
75        }
76        scope.Complete();
77      }
78      DaoLocator.DestroyContext();
79    }
80
81    void lifecycleManager_OnStartup(object sender, EventArgs e) {
82      checkForDeadJobs();
83    }
84
85    void lifecycleManager_OnShutdown(object sender, EventArgs e) {
86      checkForDeadJobs();
87    }
88
89    /// <summary>
90    /// returns all jobs stored in the database
91    /// </summary>
92    /// <returns></returns>
93    public ResponseList<JobDto> GetAllJobs() {
94         ResponseList<JobDto> response = new ResponseList<JobDto>();
95
96         response.List = new List<JobDto>(DaoLocator.JobDao.FindAll());
97         response.Success = true;
98         response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ALL_JOBS;
99
100         return response;
101    }
102
103    public ResponseList<JobDto> GetAllJobsWithFilter(State jobState, int offset, int count) {
104      ResponseList<JobDto> response = new ResponseList<JobDto>();
105      response.List = new List<JobDto>(DaoLocator.JobDao.FindWithLimitations(jobState, offset, count));
106      return response;
107    } 
108
109    /// <summary>
110    /// Gets the streamed job
111    /// </summary>
112    /// <param name="jobId"></param>
113    /// <returns></returns>
114    public Stream GetJobStreamById(Guid jobId) {           
115      return DaoLocator.JobDao.GetSerializedJobStream(jobId);
116     
117    }
118
119    /// <summary>
120    /// returns the job with the specified id
121    /// </summary>
122    /// <returns></returns>
123    public ResponseObject<JobDto> GetJobById(Guid jobId) {
124        ResponseObject<JobDto> response = new ResponseObject<JobDto>();
125
126      response.Obj = DaoLocator.JobDao.FindById(jobId);
127        if (response.Obj != null) {
128          response.Success = true;
129          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_GET_JOB_BY_ID;
130        } else {
131          response.Success = false;
132          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_DOESNT_EXIST;
133        }
134
135        return response;
136      }
137
138    public ResponseObject<JobDto> GetJobByIdWithDetails(Guid jobId) {
139      ResponseObject<JobDto> job = new ResponseObject<JobDto>();
140      job.Obj = DaoLocator.JobDao.FindById(jobId);
141      if (job.Obj != null) {
142        job.Success = true;
143        job.StatusMessage = ApplicationConstants.RESPONSE_JOB_GET_JOB_BY_ID;
144
145        job.Obj.Client = DaoLocator.ClientDao.GetClientForJob(jobId);
146      } else {
147        job.Success = false;
148        job.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_DOESNT_EXIST;
149      }     
150      return job;
151    }
152
153    public ResponseObject<JobDto> AddJobWithGroupStrings(SerializedJob job, IEnumerable<string> resources) {
154      IClientGroupDao cgd = DaoLocator.ClientGroupDao;
155      foreach (string res in resources) {
156        foreach(ClientGroupDto cg in cgd.FindByName(res)) {
157          job.JobInfo.AssignedResourceIds.Add(cg.Id);
158        }
159      }
160      return AddNewJob(job);
161    }
162
163    /// <summary>
164    /// Adds a new job into the database
165    /// </summary>
166    /// <param name="job"></param>
167    /// <returns></returns>
168    public ResponseObject<JobDto> AddNewJob(SerializedJob job) {
169        ResponseObject<JobDto> response = new ResponseObject<JobDto>();
170
171        if (job != null && job.JobInfo != null) {
172          if (job.JobInfo.State != State.offline) {
173            response.Success = false;
174            response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOBSTATE_MUST_BE_OFFLINE;
175            return response;
176          }
177          if (job.JobInfo.Id != Guid.Empty) {
178            response.Success = false;
179            response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ID_MUST_NOT_BE_SET;
180            return response;
181          }
182          if (job.SerializedJobData == null) {
183            response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_NULL;
184            response.Success = false;
185            return response;
186          }
187
188          job.JobInfo.DateCreated = DateTime.Now;
189          DaoLocator.JobDao.InsertWithAttachedJob(job);
190          DaoLocator.PluginInfoDao.InsertPluginDependenciesForJob(job.JobInfo);
191         
192          response.Success = true;
193          response.Obj = job.JobInfo;
194          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_ADDED;
195        } else {
196          response.Success = false;
197          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_NULL;
198        }
199
200        return response;
201      }
202    /*  finally {
203        if (session != null)
204          session.EndSession();
205      }
206    } */
207
208    /// <summary>
209    /// Removes a job from the database
210    /// </summary>
211    /// <param name="jobId"></param>
212    /// <returns></returns>
213    public Response RemoveJob(Guid jobId) {
214        Response response = new Response();
215
216      JobDto job = DaoLocator.JobDao.FindById(jobId);
217        if (job == null) {
218          response.Success = false;
219          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_DOESNT_EXIST;
220          return response;
221        }
222        DaoLocator.JobDao.Delete(job);
223        response.Success = false;
224        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_REMOVED;
225
226        return response;
227      }
228
229    public ResponseObject<JobDto> GetLastJobResultOf(Guid jobId) {
230      ResponseObject<JobDto> result =
231        new ResponseObject<JobDto>();
232
233       result.Obj =
234         GetLastJobResult(jobId);
235       result.Success =
236         result.Obj != null;
237
238       return result;
239    }
240
241    public ResponseObject<SerializedJob>
242      GetLastSerializedJobResultOf(Guid jobId, bool requested) {
243
244        ResponseObject<SerializedJob> response =
245          new ResponseObject<SerializedJob>();
246
247      JobDto job = DaoLocator.JobDao.FindById(jobId);
248        if (requested && (job.State == State.requestSnapshot || job.State == State.requestSnapshotSent)) {
249          response.Success = true;
250          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_RESULT_NOT_YET_HERE;
251         
252          //tx.Commit();
253         
254          return response;
255        }
256
257        /*JobResult lastResult =
258          jobResultsAdapter.GetLastResultOf(job.Id);*/
259
260        //if (lastResult != null) {
261          response.Success = true;
262          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_RESULT_SENT;
263          response.Obj = new SerializedJob();
264          response.Obj.JobInfo = job;
265          response.Obj.SerializedJobData =
266            DaoLocator.JobDao.GetBinaryJobFile(jobId);
267        //} else {
268        //  response.Success = false;
269        //}
270
271        //tx.Commit();
272        return response;
273      }
274      /*catch (Exception ex) {
275        if (tx != null)
276          tx.Rollback();
277        throw ex;
278      }
279      finally {
280        if (session != null)
281          session.EndSession();
282      }
283    }    */
284
285
286    public Response RequestSnapshot(Guid jobId) {
287     // ISession session = factory.GetSessionForCurrentThread();
288      Response response = new Response();
289     
290     /* try {
291        IJobAdapter jobAdapter = session.GetDataAdapter<JobDto, IJobAdapter>();*/
292
293        JobDto job = DaoLocator.JobDao.FindById(jobId);
294        if (job.State == State.requestSnapshot || job.State == State.requestSnapshotSent) {
295          response.Success = true;
296          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_REQUEST_ALLREADY_SET;
297          return response; // no commit needed
298        }
299        if (job.State != State.calculating) {
300          response.Success = false;
301          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_IS_NOT_BEEING_CALCULATED;
302          return response; // no commit needed
303        }
304        // job is in correct state
305        job.State = State.requestSnapshot;
306        DaoLocator.JobDao.Update(job);
307
308        response.Success = true;
309        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_REQUEST_SET;
310
311        return response;
312      }
313    /*  finally {
314        if (session != null)
315          session.EndSession();
316      }
317    } */
318
319    public Response AbortJob(Guid jobId) {
320      //ISession session = factory.GetSessionForCurrentThread();
321      Response response = new Response();
322
323    /*  try {
324        IJobAdapter jobAdapter = session.GetDataAdapter<JobDto, IJobAdapter>();*/
325
326//        JobDto job = jobAdapter.GetById(jobId);
327      JobDto job = DaoLocator.JobDao.FindById(jobId);
328        if (job == null) {
329          response.Success = false;
330          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_DOESNT_EXIST;
331          return response; // no commit needed
332        }
333        if (job.State == State.abort) {
334          response.Success = true;
335          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ABORT_REQUEST_ALLREADY_SET;
336          return response; // no commit needed
337        }
338        if (job.State != State.calculating && job.State != State.requestSnapshot && job.State != State.requestSnapshotSent) {
339          response.Success = false;
340          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_IS_NOT_BEEING_CALCULATED;
341          return response; // no commit needed
342        }
343        // job is in correct state
344        job.State = State.abort;
345      DaoLocator.JobDao.Update(job);
346
347        response.Success = true;
348        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ABORT_REQUEST_SET;
349
350        return response;
351      }
352      /*finally {
353        if (session != null)
354          session.EndSession();
355      }
356    }   */
357
358    public ResponseList<JobResult> GetAllJobResults(Guid jobId) {
359      return new ResponseList<JobResult>();
360    }
361
362    public ResponseList<ProjectDto> GetAllProjects() {
363      return null;
364    }
365
366    private Response createUpdateProject(ProjectDto project) {
367      return null;
368    }
369
370    public Response CreateProject(ProjectDto project) {
371      return createUpdateProject(project);
372    }
373
374    public Response ChangeProject(ProjectDto project) {
375      return createUpdateProject(project);
376    }
377
378    public Response DeleteProject(Guid projectId) {
379      return null;
380    }
381
382    public ResponseList<JobDto> GetJobsByProject(Guid projectId) {
383      return null;     
384    }
385    #endregion
386  }
387}
Note: See TracBrowser for help on using the repository browser.