[3011] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
| 6 | using System.Data.Linq;
|
---|
| 7 | using HeuristicLab.Hive.Server.DataAccess;
|
---|
| 8 | using System.IO;
|
---|
| 9 | using System.Data.SqlClient;
|
---|
| 10 |
|
---|
| 11 | namespace HeuristicLab.Hive.Server.LINQDataAccess {
|
---|
[4267] | 12 | public class JobDao : BaseDao<JobDto, Job>, IJobDao {
|
---|
| 13 |
|
---|
[3011] | 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 |
|
---|
[4264] | 27 | public IEnumerable<JobDto> FindWithLimitations(JobState jobState, int offset, int count) {
|
---|
[3578] | 28 | IQueryable<JobDto> query = null;
|
---|
[4264] | 29 | if (jobState == JobState.Finished) {
|
---|
[4267] | 30 | query = from job in Context.Jobs
|
---|
| 31 | where job.JobState == Enum.GetName(typeof(JobState), jobState)
|
---|
| 32 | orderby job.DateFinished
|
---|
| 33 | select EntityToDto(job, null);
|
---|
[4264] | 34 | } else if (jobState == JobState.Calculating || jobState == JobState.SnapshotRequested || jobState == JobState.SnapshotSent) {
|
---|
[3578] | 35 | query = from job in Context.Jobs
|
---|
[4267] | 36 | where job.JobState == Enum.GetName(typeof(JobState), jobState)
|
---|
| 37 | orderby job.DateCalculated
|
---|
| 38 | select EntityToDto(job, null);
|
---|
[3578] | 39 | } else {
|
---|
| 40 | query = from job in Context.Jobs
|
---|
[4267] | 41 | where job.JobState == Enum.GetName(typeof(JobState), jobState)
|
---|
| 42 | orderby job.DateCreated
|
---|
| 43 | select EntityToDto(job, null);
|
---|
[3578] | 44 | }
|
---|
| 45 |
|
---|
| 46 | return query.Skip(offset).Take(count).ToList();
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 |
|
---|
[3011] | 50 | public byte[] GetBinaryJobFile(Guid jobId) {
|
---|
| 51 | return (from job in Context.Jobs
|
---|
| 52 | where job.JobId.Equals(jobId)
|
---|
| 53 | select job.SerializedJob).SingleOrDefault().ToArray();
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | public JobDto Insert(JobDto bObj) {
|
---|
| 57 | Job j = DtoToEntity(bObj, null);
|
---|
| 58 | Context.Jobs.InsertOnSubmit(j);
|
---|
[3578] | 59 | CommitChanges();
|
---|
[3011] | 60 | bObj.Id = j.JobId;
|
---|
| 61 | return bObj;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[3931] | 64 | public void SetBinaryJobFile(Guid jobId, byte[] data) {
|
---|
| 65 | Job j = (from job in Context.Jobs
|
---|
| 66 | where job.JobId.Equals(jobId)
|
---|
| 67 | select job).SingleOrDefault();
|
---|
| 68 | j.SerializedJob = data;
|
---|
| 69 | CommitChanges();
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[3011] | 72 | public SerializedJob InsertWithAttachedJob(SerializedJob job) {
|
---|
| 73 | Job j = DtoToEntity(job.JobInfo, null);
|
---|
| 74 | j.SerializedJob = job.SerializedJobData;
|
---|
[3018] | 75 | foreach (Guid assignRessourceId in job.JobInfo.AssignedResourceIds)
|
---|
[4267] | 76 | j.AssignedResources.Add(new AssignedResource { ResourceId = assignRessourceId });
|
---|
[3011] | 77 | Context.Jobs.InsertOnSubmit(j);
|
---|
[3578] | 78 | CommitChanges();
|
---|
[3011] | 79 | job.JobInfo.Id = j.JobId;
|
---|
| 80 | return job;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | public void Delete(JobDto bObj) {
|
---|
| 84 | Job job = Context.Jobs.SingleOrDefault(j => j.JobId.Equals(bObj.Id));
|
---|
| 85 | Context.Jobs.DeleteOnSubmit(job);
|
---|
[3578] | 86 | CommitChanges();
|
---|
[3011] | 87 | }
|
---|
| 88 |
|
---|
| 89 | public void Update(JobDto bObj) {
|
---|
| 90 | Job job = Context.Jobs.SingleOrDefault(j => j.JobId.Equals(bObj.Id));
|
---|
[4267] | 91 | DtoToEntity(bObj, job);
|
---|
| 92 | CommitChanges();
|
---|
[3011] | 93 | }
|
---|
| 94 |
|
---|
[4267] | 95 | public IEnumerable<JobDto> FindActiveJobsOfSlave(SlaveDto slave) {
|
---|
[3011] | 96 | return (from j in Context.Jobs
|
---|
[4267] | 97 | where (j.JobState == Enum.GetName(typeof(JobState), JobState.Calculating) ||
|
---|
| 98 | j.JobState == Enum.GetName(typeof(JobState), JobState.Aborted) ||
|
---|
| 99 | j.JobState == Enum.GetName(typeof(JobState), JobState.SnapshotRequested) ||
|
---|
| 100 | j.JobState == Enum.GetName(typeof(JobState), JobState.SnapshotSent)) &&
|
---|
| 101 | (j.ResourceId.Equals(slave.Id))
|
---|
[3011] | 102 | select EntityToDto(j, null)).ToList();
|
---|
| 103 | }
|
---|
[4119] | 104 |
|
---|
[4267] | 105 | public IEnumerable<JobDto> FindFittingJobsForSlave(JobState state, int freeCores, int freeMemory, Guid slaveId) {
|
---|
| 106 | SlaveGroupDao cgd = new SlaveGroupDao();
|
---|
| 107 |
|
---|
| 108 | List<Guid> idList = new List<Guid>(cgd.FindAllGroupAndParentGroupIdsForSlave(slaveId));
|
---|
[3018] | 109 | //Add myself too - enables jobs for one specific host!
|
---|
[4267] | 110 | idList.Add(slaveId);
|
---|
| 111 |
|
---|
| 112 | var q = (from ar in Context.AssignedResources
|
---|
| 113 | where ar.Job.JobState == Enum.GetName(typeof(JobState), JobState.Offline) &&
|
---|
[3018] | 114 | ar.Job.CoresNeeded <= freeCores &&
|
---|
| 115 | ar.Job.MemoryNeeded <= freeMemory &&
|
---|
| 116 | idList.Contains(ar.ResourceId)
|
---|
[4267] | 117 | orderby ar.Job.Priority descending
|
---|
[3018] | 118 | select EntityToDto(ar.Job, null));
|
---|
| 119 | return q.ToList();
|
---|
[3011] | 120 | }
|
---|
| 121 |
|
---|
[4264] | 122 | public IEnumerable<JobDto> GetJobsByState(JobState state) {
|
---|
[3011] | 123 | return (from j in Context.Jobs
|
---|
[4267] | 124 | where (j.JobState == Enum.GetName(typeof(JobState), state))
|
---|
[3011] | 125 | select EntityToDto(j, null)).ToList();
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[4267] | 128 | public void AssignSlaveToJob(Guid slaveId, Guid jobId) {
|
---|
| 129 | Slave c = Context.Resources.OfType<Slave>().SingleOrDefault(slave => slave.ResourceId.Equals(slaveId));
|
---|
[3011] | 130 | Job j = Context.Jobs.SingleOrDefault(job => job.JobId.Equals(jobId));
|
---|
| 131 | c.Jobs.Add(j);
|
---|
[4267] | 132 | j.Slave = c;
|
---|
| 133 | CommitChanges();
|
---|
[3011] | 134 | }
|
---|
| 135 |
|
---|
| 136 | public void SetJobOffline(JobDto job) {
|
---|
| 137 | Job j = Context.Jobs.SingleOrDefault(jq => jq.JobId.Equals(job.Id));
|
---|
[4267] | 138 | j.Slave = null;
|
---|
| 139 | j.JobState = Enum.GetName(typeof(JobState), JobState.Offline);
|
---|
[3578] | 140 | CommitChanges();
|
---|
[3011] | 141 | }
|
---|
| 142 |
|
---|
| 143 | public Stream GetSerializedJobStream(Guid jobId) {
|
---|
[4267] | 144 | VarBinarySource source = new VarBinarySource((SqlConnection)Context.Connection, null, "Job", "SerializedJob", "JobId", jobId);
|
---|
[3011] | 145 | return new VarBinaryStream(source);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[4170] | 148 | public IEnumerable<JobDto> FindJobsById(IEnumerable<Guid> jobIds) {
|
---|
| 149 | IQueryable<JobDto> jobs = from job in Context.Jobs
|
---|
[4267] | 150 | where jobIds.Contains(job.JobId)
|
---|
| 151 | select EntityToDto(job, null);
|
---|
[4170] | 152 |
|
---|
| 153 | return jobs.ToList();
|
---|
| 154 | }
|
---|
[3011] | 155 | #endregion
|
---|
| 156 |
|
---|
| 157 | public override Job DtoToEntity(JobDto source, Job target) {
|
---|
| 158 | if (source == null)
|
---|
| 159 | return null;
|
---|
| 160 | if (target == null)
|
---|
| 161 | target = new Job();
|
---|
| 162 |
|
---|
| 163 | target.CoresNeeded = source.CoresNeeded;
|
---|
| 164 | target.MemoryNeeded = source.MemoryNeeded;
|
---|
| 165 |
|
---|
| 166 | target.DateCalculated = source.DateCalculated;
|
---|
| 167 | target.DateCreated = source.DateCreated;
|
---|
[3578] | 168 | target.DateFinished = source.DateFinished;
|
---|
[3011] | 169 | target.JobId = source.Id;
|
---|
| 170 |
|
---|
| 171 | target.Percentage = source.Percentage;
|
---|
[4141] | 172 | target.Exception = source.Exception;
|
---|
[3011] | 173 |
|
---|
| 174 | target.Priority = source.Priority;
|
---|
[4264] | 175 | target.JobState = Enum.GetName(typeof(JobState), source.State);
|
---|
[3011] | 176 | return target;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | //Assigned ressources are not used atm!
|
---|
[4267] | 180 | //Slave is not used ATM - not sure when we stopped using those...
|
---|
[3011] | 181 | public override JobDto EntityToDto(Job source, JobDto target) {
|
---|
| 182 | if (source == null)
|
---|
| 183 | return null;
|
---|
[4267] | 184 | if (target == null)
|
---|
[3011] | 185 | target = new JobDto();
|
---|
[4267] | 186 |
|
---|
[3011] | 187 | //target.ParentJob = null;
|
---|
| 188 | //target.PluginsNeeded = null;
|
---|
[4267] | 189 | //target.Slave = null;
|
---|
[3011] | 190 | //target.Project = null;
|
---|
[4267] | 191 |
|
---|
[3011] | 192 | target.CoresNeeded = source.CoresNeeded;
|
---|
| 193 | target.MemoryNeeded = source.MemoryNeeded;
|
---|
| 194 |
|
---|
| 195 | target.DateCalculated = source.DateCalculated;
|
---|
| 196 | target.DateCreated = source.DateCreated;
|
---|
[3578] | 197 | target.DateFinished = source.DateFinished;
|
---|
[3011] | 198 | target.Id = source.JobId;
|
---|
[4141] | 199 |
|
---|
| 200 | target.Exception = source.Exception;
|
---|
[3011] | 201 | target.Percentage = source.Percentage;
|
---|
[4267] | 202 |
|
---|
[3011] | 203 | target.Priority = source.Priority;
|
---|
[4267] | 204 | target.State = (JobState)Enum.Parse(typeof(JobState), source.JobState, true);
|
---|
[3011] | 205 | return target;
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 | }
|
---|