Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implementing Lifecycle Management (#453)

File size: 4.5 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    ILifecycleManager lifecycleManager;
36
37    #region IJobManager Members
38
39    public JobManager() {
40      jobAdapter = ServiceLocator.GetJobAdapter();
41
42      lifecycleManager = ServiceLocator.GetLifecycleManager();
43
44      lifecycleManager.RegisterStartup(new EventHandler(lifecycleManager_OnStartup));
45      lifecycleManager.RegisterStartup(new EventHandler(lifecycleManager_OnShutdown));
46    }
47
48    void checkForDeadJobs() {
49      List<Job> allJobs = new List<Job>(jobAdapter.GetAll());
50      foreach (Job curJob in allJobs) {
51        if (curJob.State == State.calculating) {
52          // TODO check for job results
53          curJob.State = State.idle;
54          curJob.Percentage = 0;
55          curJob.Client = null;
56        }
57      }
58    }
59
60    void lifecycleManager_OnStartup(object sender, EventArgs e) {
61      checkForDeadJobs();
62    }
63
64    void lifecycleManager_OnShutdown(object sender, EventArgs e) {
65      checkForDeadJobs();
66    }
67
68    /// <summary>
69    /// returns all jobs stored in the database
70    /// </summary>
71    /// <returns></returns>
72    public ResponseList<Job> GetAllJobs() {
73      ResponseList<Job> response = new ResponseList<Job>();
74
75      response.List = new List<Job>(jobAdapter.GetAll());
76      response.Success = true;
77      response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ALL_JOBS;
78     
79      return response;
80    }
81
82    /// <summary>
83    /// Adds a new job into the database
84    /// </summary>
85    /// <param name="job"></param>
86    /// <returns></returns>
87    public ResponseObject<Job> AddNewJob(Job job) {
88      ResponseObject<Job> response = new ResponseObject<Job>();
89
90      if (job != null) {
91        if (job.State != State.offline) {
92          response.Success = false;
93          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOBSTATE_MUST_BE_OFFLINE;
94          return response;
95        }
96        if (job.Id != 0) {
97          response.Success = false;
98          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_ID_MUST_NOT_BE_SET;
99          return response;
100        }
101        if (job.SerializedJob == null) {
102          response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_NULL;
103          response.Success = false;
104          return response;
105        }
106
107        jobAdapter.Update(job);
108        response.Success = true;
109        response.Obj = job;
110        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_ADDED;
111      } else {
112        response.Success = false;
113        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_NULL;
114      }
115
116      return response;
117    }
118
119    /// <summary>
120    /// Removes a job from the database
121    /// </summary>
122    /// <param name="jobId"></param>
123    /// <returns></returns>
124    public Response RemoveJob(long jobId) {
125      Response response = new Response();
126
127      Job job = jobAdapter.GetById(jobId);
128      if (job == null) {
129        response.Success = false;
130        response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_DOESNT_EXIST;
131        return response;
132      }
133      jobAdapter.Delete(job);
134      response.Success = false;
135      response.StatusMessage = ApplicationConstants.RESPONSE_JOB_JOB_REMOVED;
136
137      return response;
138    }
139
140    #endregion
141  }
142}
Note: See TracBrowser for help on using the repository browser.