Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.3-Hive/sources/HeuristicLab.Hive/HeuristicLab.Hive.Server.LINQDataAccess/3.3/JobDao.cs @ 4501

Last change on this file since 4501 was 4424, checked in by cneumuel, 14 years ago
  • Added and updated License Information in every file
  • Sort and remove usings in every file
  • Deleted obsolete DataAccess.ADOHelper
  • Deleted some obsolete files
File size: 11.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Data.SqlClient;
25using System.IO;
26using System.Linq;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
28using HeuristicLab.Hive.Server.DataAccess;
29
30namespace HeuristicLab.Hive.Server.LINQDataAccess {
31  public class JobDao : BaseDao<JobDto, Job>, IJobDao {
32
33    #region IGenericDao<JobDto,Job> Members
34
35    public JobDto FindById(Guid id) {
36      return (from job in Context.Jobs
37              where job.JobId.Equals(id)
38              select EntityToDto(job, null)).SingleOrDefault();
39    }
40
41    public IEnumerable<JobDto> FindAll() {
42      return (from job in Context.Jobs
43              select EntityToDto(job, null)).ToList();
44    }
45
46    public IEnumerable<JobDto> FindWithLimitations(JobState jobState, int offset, int count) {
47      IQueryable<JobDto> query = null;
48      if (jobState == JobState.Finished) {
49        query = from job in Context.Jobs
50                where job.JobState == Enum.GetName(typeof(JobState), jobState)
51                orderby job.DateFinished
52                select EntityToDto(job, null);
53      } else if (jobState == JobState.Calculating || jobState == JobState.SnapshotRequested || jobState == JobState.SnapshotSent) {
54        query = from job in Context.Jobs
55                where job.JobState == Enum.GetName(typeof(JobState), jobState)
56                orderby job.DateCalculated
57                select EntityToDto(job, null);
58      } else {
59        query = from job in Context.Jobs
60                where job.JobState == Enum.GetName(typeof(JobState), jobState)
61                orderby job.DateCreated
62                select EntityToDto(job, null);
63      }
64
65      return query.Skip(offset).Take(count).ToList();
66    }
67
68
69    public byte[] GetBinaryJobFile(Guid jobId) {
70      return (from job in Context.Jobs
71              where job.JobId.Equals(jobId)
72              select job.SerializedJob).SingleOrDefault().ToArray();
73    }
74
75    public JobDto Insert(JobDto bObj) {
76      Job j = DtoToEntity(bObj, null);
77      Context.Jobs.InsertOnSubmit(j);
78      CommitChanges();
79      bObj.Id = j.JobId;
80      return bObj;
81    }
82
83    public void SetBinaryJobFile(Guid jobId, byte[] data) {
84      Job j = (from job in Context.Jobs
85               where job.JobId.Equals(jobId)
86               select job).SingleOrDefault();
87      j.SerializedJob = data;
88      CommitChanges();
89    }
90
91    public SerializedJob InsertWithAttachedJob(SerializedJob job) {
92      Job j = DtoToEntity(job.JobInfo, null);
93      j.SerializedJob = job.SerializedJobData;
94      //foreach (Guid assignRessourceId in job.JobInfo.AssignedResourceIds)
95      //  j.AssignedResources.Add(new AssignedResource { ResourceId = assignRessourceId });
96      Context.Jobs.InsertOnSubmit(j);
97      CommitChanges();
98      job.JobInfo.Id = j.JobId;
99      return job;
100    }
101
102    public void Delete(Guid id) {
103      Job job = Context.Jobs.SingleOrDefault(j => j.JobId.Equals(id));
104      Context.Jobs.DeleteOnSubmit(job);
105      CommitChanges();
106    }
107
108    public void Update(JobDto bObj) {
109      Job job = Context.Jobs.SingleOrDefault(j => j.JobId.Equals(bObj.Id));
110      DtoToEntity(bObj, job);
111      CommitChanges();
112    }
113
114    public IEnumerable<JobDto> FindActiveJobsOfSlave(SlaveDto slave) {
115      return (from j in Context.Jobs
116              where (j.JobState == Enum.GetName(typeof(JobState), JobState.Calculating) ||
117                     j.JobState == Enum.GetName(typeof(JobState), JobState.Aborted) ||
118                     j.JobState == Enum.GetName(typeof(JobState), JobState.SnapshotRequested) ||
119                     j.JobState == Enum.GetName(typeof(JobState), JobState.SnapshotSent) ||
120                     j.JobState == Enum.GetName(typeof(JobState), JobState.WaitForChildJobs)) &&
121                    (j.ResourceId.Equals(slave.Id))
122              select EntityToDto(j, null)).ToList();
123    }
124
125    public IEnumerable<JobDto> FindFittingJobs(JobState state, int freeCores, int freeMemory, Guid slaveId) {
126      SlaveGroupDao cgd = new SlaveGroupDao();
127
128      List<Guid> idList = new List<Guid>(cgd.FindAllGroupAndParentGroupIdsForSlave(slaveId));
129      //Add myself too - enables jobs for one specific host!
130      idList.Add(slaveId);
131
132      var q = (from ar in Context.AssignedResources
133               where ar.Job.JobState == Enum.GetName(typeof(JobState), state) &&
134                     ar.Job.CoresNeeded <= freeCores &&
135                     ar.Job.MemoryNeeded <= freeMemory &&
136                     idList.Contains(ar.ResourceId)
137               orderby ar.Job.Priority descending
138               select EntityToDto(ar.Job, null));
139      return q.ToList();
140    }
141
142    public IEnumerable<JobDto> FindJobsWithFinishedChilds(Guid slaveId) {
143      SlaveGroupDao cgd = new SlaveGroupDao();
144     
145      List<Guid> idList = new List<Guid>(cgd.FindAllGroupAndParentGroupIdsForSlave(slaveId));
146      //Add myself too - enables jobs for one specific host!
147      idList.Add(slaveId);
148
149      var query = from ar in Context.AssignedResources
150                  where ar.Job.JobState == Enum.GetName(typeof(JobState), JobState.WaitForChildJobs) &&
151                    (from child in Context.Jobs
152                     where child.ParentJobId == ar.Job.JobId
153                     select child.JobState == Enum.GetName(typeof(JobState), JobState.Finished)).All(x => x) &&
154                    (from child in Context.Jobs // avoid returning WaitForChildJobs jobs where no child-jobs exist (yet)
155                     where child.ParentJobId == ar.Job.JobId
156                     select child).Count() > 0
157                  orderby ar.Job.Priority descending
158                  select EntityToDto(ar.Job, null);
159      var list = query.ToList();
160      return list;
161    }
162
163    public IEnumerable<JobDto> GetJobsByState(JobState state) {
164      return (from j in Context.Jobs
165              where (j.JobState == Enum.GetName(typeof(JobState), state))
166              select EntityToDto(j, null)).ToList();
167    }
168
169    public void AssignSlaveToJob(Guid slaveId, Guid jobId) {
170      Slave s = Context.Resources.OfType<Slave>().SingleOrDefault(slave => slave.ResourceId.Equals(slaveId));
171      Job j = Context.Jobs.SingleOrDefault(job => job.JobId.Equals(jobId));
172      s.Jobs.Add(j);
173      j.Slave = s;
174      CommitChanges();
175    }
176
177    public void UnAssignSlaveToJob(Guid jobId) {
178      Job j = Context.Jobs.SingleOrDefault(job => job.JobId.Equals(jobId));
179      j.Slave = null;
180      CommitChanges();
181    }
182
183    public void SetJobOffline(JobDto job) {
184      Job j = Context.Jobs.SingleOrDefault(jq => jq.JobId.Equals(job.Id));
185      j.Slave = null;
186      j.JobState = Enum.GetName(typeof(JobState), JobState.Offline);
187      CommitChanges();
188    }
189
190    public Stream GetSerializedJobStream(Guid jobId) {
191      VarBinarySource source = new VarBinarySource((SqlConnection)Context.Connection, null, "Job", "SerializedJob", "JobId", jobId);
192      return new VarBinaryStream(source);
193    }
194
195    public IEnumerable<JobDto> FindJobsById(IEnumerable<Guid> jobIds) {
196      IQueryable<JobDto> jobs = from job in Context.Jobs
197                                where jobIds.Contains(job.JobId)
198                                select EntityToDto(job, null);
199      return jobs.ToList();
200    }
201
202    public bool IsUserAuthorizedForJobs(Guid userId, params Guid[] jobIds) {
203      var jobs = from job in Context.Jobs
204                 where jobIds.Contains(job.JobId)
205                 select job;
206      return jobs.All(job => job.UserId == userId);
207    }
208
209    public IEnumerable<JobDto> FindJobsByParentId(Guid? parentJobId, bool recursive) {
210      IQueryable<JobDto> query = from job in Context.Jobs
211                                 where parentJobId == null ? !job.ParentJobId.HasValue : job.ParentJobId.Value == parentJobId
212                                 select EntityToDto(job, null);
213      List<JobDto> jobs = query.ToList();
214      if (recursive) {
215        List<JobDto> childs = new List<JobDto>();
216        foreach (JobDto job in jobs) {
217          childs.AddRange(FindJobsByParentId(job.Id, recursive));
218        }
219        jobs.AddRange(childs);
220      }
221      return jobs;
222    }
223
224    #endregion
225
226    public override Job DtoToEntity(JobDto source, Job target) {
227      if (source == null)
228        return null;
229      if (target == null)
230        target = new Job();
231
232      target.CoresNeeded = source.CoresNeeded;
233      target.MemoryNeeded = source.MemoryNeeded;
234
235      target.DateCalculated = source.DateCalculated;
236      target.DateCreated = source.DateCreated;
237      target.DateFinished = source.DateFinished;
238      target.JobId = source.Id;
239
240      target.ExecutionTime = source.ExecutionTime;
241      target.Exception = source.Exception;
242
243      target.Priority = source.Priority;
244      target.JobState = Enum.GetName(typeof(JobState), source.State);
245      target.UserId = source.UserId;
246      target.ParentJobId = source.ParentJobId;
247     
248      foreach (Guid assignRessourceId in source.AssignedResourceIds) {
249        if (!target.AssignedResources.Select(x => x.ResourceId).Contains(assignRessourceId)) {
250          target.AssignedResources.Add(new AssignedResource { ResourceId = assignRessourceId });
251        }
252      }
253
254      return target;
255    }
256
257    //Slave is not used ATM - not sure when we stopped using those...
258    public override JobDto EntityToDto(Job source, JobDto target) {
259      if (source == null)
260        return null;
261      if (target == null)
262        target = new JobDto();
263
264      target.CoresNeeded = source.CoresNeeded;
265      target.MemoryNeeded = source.MemoryNeeded;
266
267      target.DateCalculated = source.DateCalculated;
268      target.DateCreated = source.DateCreated;
269      target.DateFinished = source.DateFinished;
270      target.Id = source.JobId;
271
272      target.Exception = source.Exception;
273      target.ExecutionTime = source.ExecutionTime.HasValue ? source.ExecutionTime.Value : TimeSpan.Zero;
274
275      target.Priority = source.Priority;
276      target.State = (JobState)Enum.Parse(typeof(JobState), source.JobState, true);
277      target.UserId = source.UserId;
278      target.ParentJobId = source.ParentJobId;
279     
280      target.AssignedResourceIds = source.AssignedResources.Select(x => x.ResourceId).ToList();
281
282      return target;
283    }
284  }
285}
Note: See TracBrowser for help on using the repository browser.