Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implementing Lifecycle Management (#453)

File size: 5.4 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.Core.InternalInterfaces.DataAccess;
30
31namespace HeuristicLab.Hive.Server.Core {
32  class JobManager: IJobManager {
33
34    IJobAdapter jobAdapter;
35    IJobResultsAdapter jobResultAdapter;
36    ILifecycleManager lifecycleManager;
37
38    #region IJobManager Members
39
40    public JobManager() {
41      jobAdapter = ServiceLocator.GetJobAdapter();
42      jobResultAdapter = ServiceLocator.GetJobResultsAdapter();
43
44      lifecycleManager = ServiceLocator.GetLifecycleManager();
45
46      lifecycleManager.RegisterStartup(new EventHandler(lifecycleManager_OnStartup));
47      lifecycleManager.RegisterStartup(new EventHandler(lifecycleManager_OnShutdown));
48    }
49
50    private void resetJobsDependingOnResults(Job job) {
51      List<JobResult> allJobResults = new List<JobResult>(jobResultAdapter.GetAll());
52      JobResult lastJobResult = null;
53      foreach (JobResult jR in allJobResults) {
54        if (jR.Job != null && jR.Job.Id == job.Id) {
55          if (lastJobResult != null) {
56            // if lastJobResult was before the current jobResult the lastJobResult must be updated
57            if ((jR.timestamp.Subtract(lastJobResult.timestamp)).Seconds > 0)
58              lastJobResult = jR;
59          }
60        }
61      }
62      if (lastJobResult != null) {
63        job.Client = null;
64        job.Percentage = lastJobResult.Percentage;
65        job.State = State.idle;
66        job.SerializedJob = lastJobResult.Result;
67      } else {
68        job.Client = null;
69        job.Percentage = 0;
70        job.State = State.idle;
71      }
72      jobAdapter.Update(job);
73    }
74
75    void checkForDeadJobs() {
76      List<Job> allJobs = new List<Job>(jobAdapter.GetAll());
77      foreach (Job curJob in allJobs) {
78        if (curJob.State == State.calculating) {
79          resetJobsDependingOnResults(curJob);
80        }
81      }
82    }
83
84    void lifecycleManager_OnStartup(object sender, EventArgs e) {
85      checkForDeadJobs();
86    }
87
88    void lifecycleManager_OnShutdown(object sender, EventArgs e) {
89      checkForDeadJobs();
90    }
91
92    /// <summary>
93    /// returns all jobs stored in the database
94    /// </summary>
95    /// <returns></returns>
96    public ResponseList<Job> GetAllJobs() {
97      ResponseList<Job> response = new ResponseList<Job>();
98
99      response.List = new List<Job>(jobAdapter.GetAll());
100      response.Success = true;
101      response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ALL_JOBS;
102     
103      return response;
104    }
105
106    /// <summary>
107    /// Adds a new job into the database
108    /// </summary>
109    /// <param name="job"></param>
110    /// <returns></returns>
111    public ResponseObject<Job> AddNewJob(Job job) {
112      ResponseObject<Job> response = new ResponseObject<Job>();
113
114      if (job != null) {
115        if (job.State != State.offline) {
116          response.Success = false;
117          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOBSTATE_MUST_BE_OFFLINE;
118          return response;
119        }
120        if (job.Id != 0) {
121          response.Success = false;
122          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ID_MUST_NOT_BE_SET;
123          return response;
124        }
125        if (job.SerializedJob == null) {
126          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_NULL;
127          response.Success = false;
128          return response;
129        }
130
131        jobAdapter.Update(job);
132        response.Success = true;
133        response.Obj = job;
134        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_ADDED;
135      } else {
136        response.Success = false;
137        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_NULL;
138      }
139
140      return response;
141    }
142
143    /// <summary>
144    /// Removes a job from the database
145    /// </summary>
146    /// <param name="jobId"></param>
147    /// <returns></returns>
148    public Response RemoveJob(long jobId) {
149      Response response = new Response();
150
151      Job job = jobAdapter.GetById(jobId);
152      if (job == null) {
153        response.Success = false;
154        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_DOESNT_EXIST;
155        return response;
156      }
157      jobAdapter.Delete(job);
158      response.Success = false;
159      response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_REMOVED;
160
161      return response;
162    }
163
164    #endregion
165  }
166}
Note: See TracBrowser for help on using the repository browser.