Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.LINQDataAccess/3.2/JobDao.cs @ 3094

Last change on this file since 3094 was 3018, checked in by kgrading, 15 years ago

added Priority and resource restricted scheduling (#907)

File size: 6.1 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 byte[] GetBinaryJobFile(Guid jobId) {
28      return (from job in Context.Jobs
29              where job.JobId.Equals(jobId)
30              select job.SerializedJob).SingleOrDefault().ToArray();
31    }
32
33    public JobDto Insert(JobDto bObj) {
34      Job j = DtoToEntity(bObj, null);
35      Context.Jobs.InsertOnSubmit(j);
36      Context.SubmitChanges();
37      bObj.Id = j.JobId;
38      return bObj;
39    }
40
41    public SerializedJob InsertWithAttachedJob(SerializedJob job) {
42      Job j = DtoToEntity(job.JobInfo, null);
43      j.SerializedJob = job.SerializedJobData;
44      foreach (Guid assignRessourceId in job.JobInfo.AssignedResourceIds)
45        j.AssignedResources.Add(new AssignedResource { ResourceId = assignRessourceId});
46      Context.Jobs.InsertOnSubmit(j);
47      Context.SubmitChanges();
48      job.JobInfo.Id = j.JobId;
49      return job;
50
51    }
52
53    public void Delete(JobDto bObj) {
54      Job job = Context.Jobs.SingleOrDefault(j => j.JobId.Equals(bObj.Id));
55      Context.Jobs.DeleteOnSubmit(job);
56      Context.SubmitChanges();
57    }
58
59    public void Update(JobDto bObj) {
60      Job job = Context.Jobs.SingleOrDefault(j => j.JobId.Equals(bObj.Id));
61      DtoToEntity(bObj, job);
62      Context.SubmitChanges();
63    }
64
65    public IEnumerable<JobDto> FindActiveJobsOfClient(ClientDto client) {
66      return (from j in Context.Jobs
67              where (j.JobState == Enum.GetName(typeof (State), State.calculating) ||
68                     j.JobState == Enum.GetName(typeof (State), State.abort) ||
69                     j.JobState == Enum.GetName(typeof (State), State.requestSnapshot) ||
70                     j.JobState == Enum.GetName(typeof (State), State.requestSnapshotSent)) &&
71                    (j.ResourceId.Equals(client.Id))
72              select EntityToDto(j, null)).ToList();
73    }
74    public IEnumerable<JobDto> FindFittingJobsForClient(State state, int freeCores, int freeMemory, Guid clientId) {
75      ClientGroupDao cgd = new ClientGroupDao();
76     
77      List<Guid> idList = new List<Guid>(cgd.FindAllGroupAndParentGroupIdsForClient(clientId));
78      //Add myself too - enables jobs for one specific host!
79      idList.Add(clientId);
80     
81      var q = (from ar in Context.AssignedResources               
82               where ar.Job.JobState == Enum.GetName(typeof (State), State.offline) &&
83                     ar.Job.CoresNeeded <= freeCores &&
84                     ar.Job.MemoryNeeded <= freeMemory &&
85                     idList.Contains(ar.ResourceId)
86               orderby ar.Job.Priority descending                 
87               select EntityToDto(ar.Job, null));
88      return q.ToList();
89     
90    }
91
92    public IEnumerable<JobDto> GetJobsByState(State state) {
93      return (from j in Context.Jobs
94              where (j.JobState == Enum.GetName(typeof (State), state))
95              select EntityToDto(j, null)).ToList();
96    }
97
98    public void AssignClientToJob(Guid clientId, Guid jobId) {
99      Client c = Context.Clients.SingleOrDefault(client => client.ResourceId.Equals(clientId));
100      Job j = Context.Jobs.SingleOrDefault(job => job.JobId.Equals(jobId));
101      c.Jobs.Add(j);
102      j.Client = c;
103      Context.SubmitChanges();     
104    }
105
106    public void SetJobOffline(JobDto job) {
107      Job j = Context.Jobs.SingleOrDefault(jq => jq.JobId.Equals(job.Id));
108      j.Client = null;
109      j.JobState = Enum.GetName(typeof(State), State.offline);
110      Context.SubmitChanges();
111    }
112
113    public Stream GetSerializedJobStream(Guid jobId) {
114      HiveDataContext hdc = new HiveDataContext();
115      String ConnStr = hdc.Connection.ConnectionString;
116      SqlConnection connection = new SqlConnection(hdc.Connection.ConnectionString);     
117      VarBinarySource source =
118        new VarBinarySource(
119          connection, null,
120          "Job", "SerializedJob", "JobId", jobId);
121
122      return new VarBinaryStream(source);
123    }
124
125    #endregion
126
127    public override Job DtoToEntity(JobDto source, Job target) {
128      if (source == null)
129        return null;
130      if (target == null)
131        target = new Job();
132
133      target.CoresNeeded = source.CoresNeeded;
134      target.MemoryNeeded = source.MemoryNeeded;
135
136      target.DateCalculated = source.DateCalculated;
137      target.DateCreated = source.DateCreated;
138      target.JobId = source.Id;
139
140      target.Percentage = source.Percentage;
141
142      target.Priority = source.Priority;
143      target.JobState = Enum.GetName(typeof(State), source.State);
144      return target;
145    }
146
147    //Assigned ressources are not used atm!
148    //Client is not used ATM - not sure when we stopped using those...
149    public override JobDto EntityToDto(Job source, JobDto target) {
150      if (source == null)
151        return null;
152      if(target == null)
153        target = new JobDto();
154   
155      //target.ParentJob = null;
156      //target.PluginsNeeded = null;
157      //target.Client = null;
158      //target.Project = null;
159     
160      target.CoresNeeded = source.CoresNeeded;
161      target.MemoryNeeded = source.MemoryNeeded;
162
163
164
165      target.DateCalculated = source.DateCalculated;
166      target.DateCreated = source.DateCreated;
167      target.Id = source.JobId;
168     
169         
170      target.Percentage = source.Percentage;
171     
172      target.Priority = source.Priority;
173      target.State = (State) Enum.Parse(typeof (State), source.JobState);
174      return target;
175    }
176  }
177}
Note: See TracBrowser for help on using the repository browser.