Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.LINQDataAccess/3.2/JobDao.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: 7.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Hive.Contracts.BusinessObjects;
6using System.Data.Linq;
7using HeuristicLab.Hive.Server.DataAccess;
8using System.IO;
9using System.Data.SqlClient;
10
11namespace HeuristicLab.Hive.Server.LINQDataAccess {
12  public class JobDao: BaseDao<JobDto, Job>, IJobDao {
13   
14    #region IGenericDao<JobDto,Job> Members
15
16    public JobDto FindById(Guid id) {
17      return (from job in Context.Jobs
18              where job.JobId.Equals(id)
19              select EntityToDto(job, null)).SingleOrDefault();
20    }
21
22    public IEnumerable<JobDto> FindAll() {
23      return (from job in Context.Jobs
24              select EntityToDto(job, null)).ToList();
25    }
26
27    public IEnumerable<JobDto> FindWithLimitations(State jobState, int offset, int count) {
28
29      IQueryable<JobDto> query = null;
30      if (jobState == State.finished) {
31         query = from job in Context.Jobs
32                 where job.JobState == Enum.GetName(typeof (State), jobState)
33                 orderby job.DateFinished
34                 select EntityToDto(job, null);
35      } else if (jobState == State.calculating || jobState == State.requestSnapshot || jobState == State.requestSnapshotSent) {
36        query = from job in Context.Jobs
37                    where job.JobState == Enum.GetName(typeof(State), jobState)
38                    orderby job.DateCalculated
39                    select EntityToDto(job, null);
40      } else {
41        query = from job in Context.Jobs
42                    where job.JobState == Enum.GetName(typeof(State), jobState)
43                    orderby job.DateCreated
44                    select EntityToDto(job, null);
45      }
46
47      return query.Skip(offset).Take(count).ToList();
48    }
49
50
51    public byte[] GetBinaryJobFile(Guid jobId) {
52      return (from job in Context.Jobs
53              where job.JobId.Equals(jobId)
54              select job.SerializedJob).SingleOrDefault().ToArray();
55    }
56
57    public JobDto Insert(JobDto bObj) {
58      Job j = DtoToEntity(bObj, null);
59      Context.Jobs.InsertOnSubmit(j);
60      CommitChanges();
61      bObj.Id = j.JobId;
62      return bObj;
63    }
64
65    public SerializedJob InsertWithAttachedJob(SerializedJob job) {
66      Job j = DtoToEntity(job.JobInfo, null);
67      j.SerializedJob = job.SerializedJobData;
68      foreach (Guid assignRessourceId in job.JobInfo.AssignedResourceIds)
69        j.AssignedResources.Add(new AssignedResource { ResourceId = assignRessourceId});
70      Context.Jobs.InsertOnSubmit(j);
71      CommitChanges();
72      job.JobInfo.Id = j.JobId;
73      return job;
74
75    }
76
77    public void Delete(JobDto bObj) {
78      Job job = Context.Jobs.SingleOrDefault(j => j.JobId.Equals(bObj.Id));
79      Context.Jobs.DeleteOnSubmit(job);
80      CommitChanges();
81    }
82
83    public void Update(JobDto bObj) {
84      Job job = Context.Jobs.SingleOrDefault(j => j.JobId.Equals(bObj.Id));
85      DtoToEntity(bObj, job);   
86      CommitChanges();
87    }
88
89    public IEnumerable<JobDto> FindActiveJobsOfClient(ClientDto client) {
90      return (from j in Context.Jobs
91              where (j.JobState == Enum.GetName(typeof (State), State.calculating) ||
92                     j.JobState == Enum.GetName(typeof (State), State.abort) ||
93                     j.JobState == Enum.GetName(typeof (State), State.requestSnapshot) ||
94                     j.JobState == Enum.GetName(typeof (State), State.requestSnapshotSent)) &&
95                    (j.ResourceId.Equals(client.Id))
96              select EntityToDto(j, null)).ToList();
97    }
98    public IEnumerable<JobDto> FindFittingJobsForClient(State state, int freeCores, int freeMemory, Guid clientId) {
99      ClientGroupDao cgd = new ClientGroupDao();
100     
101      List<Guid> idList = new List<Guid>(cgd.FindAllGroupAndParentGroupIdsForClient(clientId));
102      //Add myself too - enables jobs for one specific host!
103      idList.Add(clientId);
104     
105      var q = (from ar in Context.AssignedResources               
106               where ar.Job.JobState == Enum.GetName(typeof (State), State.offline) &&
107                     ar.Job.CoresNeeded <= freeCores &&
108                     ar.Job.MemoryNeeded <= freeMemory &&
109                     idList.Contains(ar.ResourceId)
110               orderby ar.Job.Priority descending                 
111               select EntityToDto(ar.Job, null));
112      return q.ToList();
113     
114    }
115
116    public IEnumerable<JobDto> GetJobsByState(State state) {
117      return (from j in Context.Jobs
118              where (j.JobState == Enum.GetName(typeof (State), state))
119              select EntityToDto(j, null)).ToList();
120    }
121
122    public void AssignClientToJob(Guid clientId, Guid jobId) {
123      Client c = Context.Clients.SingleOrDefault(client => client.ResourceId.Equals(clientId));
124      Job j = Context.Jobs.SingleOrDefault(job => job.JobId.Equals(jobId));
125      c.Jobs.Add(j);
126      j.Client = c;
127      CommitChanges();     
128    }
129
130    public void SetJobOffline(JobDto job) {
131      Job j = Context.Jobs.SingleOrDefault(jq => jq.JobId.Equals(job.Id));
132      j.Client = null;
133      j.JobState = Enum.GetName(typeof(State), State.offline);
134      CommitChanges();
135    }
136
137    public Stream GetSerializedJobStream(Guid jobId) {
138      VarBinarySource source =
139        new VarBinarySource(
140          (SqlConnection)Context.Connection, null,
141          "Job", "SerializedJob", "JobId", jobId);
142
143      return new VarBinaryStream(source);
144    }
145
146    #endregion
147
148    public override Job DtoToEntity(JobDto source, Job target) {
149      if (source == null)
150        return null;
151      if (target == null)
152        target = new Job();
153
154      target.CoresNeeded = source.CoresNeeded;
155      target.MemoryNeeded = source.MemoryNeeded;
156
157      target.DateCalculated = source.DateCalculated;
158      target.DateCreated = source.DateCreated;
159      target.DateFinished = source.DateFinished;
160      target.JobId = source.Id;
161
162      target.Percentage = source.Percentage;
163
164      target.Priority = source.Priority;
165      target.JobState = Enum.GetName(typeof(State), source.State);
166      return target;
167    }
168
169    //Assigned ressources are not used atm!
170    //Client is not used ATM - not sure when we stopped using those...
171    public override JobDto EntityToDto(Job source, JobDto target) {
172      if (source == null)
173        return null;
174      if(target == null)
175        target = new JobDto();
176   
177      //target.ParentJob = null;
178      //target.PluginsNeeded = null;
179      //target.Client = null;
180      //target.Project = null;
181     
182      target.CoresNeeded = source.CoresNeeded;
183      target.MemoryNeeded = source.MemoryNeeded;
184
185      target.DateCalculated = source.DateCalculated;
186      target.DateCreated = source.DateCreated;
187      target.DateFinished = source.DateFinished;
188      target.Id = source.JobId;
189       
190      target.Percentage = source.Percentage;
191     
192      target.Priority = source.Priority;
193      target.State = (State) Enum.Parse(typeof (State), source.JobState);
194      return target;
195    }
196  }
197}
Note: See TracBrowser for help on using the repository browser.