1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Hive.Server.Core.InternalInterfaces;
|
---|
6 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
7 | using HeuristicLab.Hive.Server.DataAccess;
|
---|
8 | using System.Threading;
|
---|
9 | using HeuristicLab.DataAccess.Interfaces;
|
---|
10 | using System.Transactions;
|
---|
11 | using HeuristicLab.Hive.Contracts;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.Hive.Server.Core {
|
---|
14 | internal class DefaultScheduler : IScheduler {
|
---|
15 |
|
---|
16 | private static object jobLock = new object();
|
---|
17 |
|
---|
18 | #region IScheduler Members
|
---|
19 |
|
---|
20 | public DefaultScheduler() { }
|
---|
21 |
|
---|
22 | public bool ExistsJobForSlave(HeartBeatData hbData) {
|
---|
23 | List<JobDto> allOfflineJobsForSlave = new List<JobDto>(DaoLocator.JobDao.FindFittingJobsForSlave(JobState.Offline, hbData.FreeCores, hbData.FreeMemory, hbData.SlaveId));
|
---|
24 | return (allOfflineJobsForSlave != null && allOfflineJobsForSlave.Count > 0);
|
---|
25 | }
|
---|
26 |
|
---|
27 | public JobDto GetNextJobForSlave(Guid slaveId) {
|
---|
28 | lock (jobLock) {
|
---|
29 | /// Critical section ///
|
---|
30 | JobDto jobToCalculate = null;
|
---|
31 | using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = ApplicationConstants.ISOLATION_LEVEL_SCOPE })) {
|
---|
32 | SlaveDto slave = DaoLocator.SlaveDao.FindById(slaveId);
|
---|
33 | if (slave != null) {
|
---|
34 | LinkedList<JobDto> allOfflineJobsForSlave = new LinkedList<JobDto>(DaoLocator.JobDao.FindFittingJobsForSlave(JobState.Offline, slave.NrOfFreeCores, slave.FreeMemory, slave.Id));
|
---|
35 | if (allOfflineJobsForSlave != null && allOfflineJobsForSlave.Count > 0) {
|
---|
36 | jobToCalculate = allOfflineJobsForSlave.First.Value;
|
---|
37 | jobToCalculate.State = JobState.Calculating;
|
---|
38 | jobToCalculate.Slave = slave;
|
---|
39 | jobToCalculate.Slave.State = SlaveState.Calculating;
|
---|
40 | jobToCalculate.DateCalculated = DateTime.Now;
|
---|
41 | DaoLocator.JobDao.AssignSlaveToJob(slave.Id, jobToCalculate.Id);
|
---|
42 | DaoLocator.JobDao.Update(jobToCalculate);
|
---|
43 | DaoLocator.SlaveDao.Update(jobToCalculate.Slave);
|
---|
44 | }
|
---|
45 | }
|
---|
46 | scope.Complete();
|
---|
47 | }
|
---|
48 | /// End Critical section ///
|
---|
49 |
|
---|
50 | return jobToCalculate;
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | #endregion
|
---|
55 | }
|
---|
56 | } |
---|