Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Server.Core/3.3/DefaultScheduler.cs @ 4264

Last change on this file since 4264 was 4264, checked in by cneumuel, 14 years ago

Split up "State" to "JobState" and "SlaveState" (#1159)

File size: 2.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Hive.Server.Core.InternalInterfaces;
6using HeuristicLab.Hive.Contracts.BusinessObjects;
7using HeuristicLab.Hive.Server.DataAccess;
8using System.Threading;
9using HeuristicLab.DataAccess.Interfaces;
10using System.Transactions;
11using HeuristicLab.Hive.Contracts;
12
13namespace 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          ClientDto slave = DaoLocator.ClientDao.FindById(slaveId);
33          LinkedList<JobDto> allOfflineJobsForSlave = new LinkedList<JobDto>(DaoLocator.JobDao.FindFittingJobsForSlave(JobState.Offline, slave.NrOfFreeCores, slave.FreeMemory, slave.Id));
34          if (allOfflineJobsForSlave != null && allOfflineJobsForSlave.Count > 0) {
35            jobToCalculate = allOfflineJobsForSlave.First.Value;
36            jobToCalculate.State = JobState.Calculating;
37            jobToCalculate.Client = slave;
38            jobToCalculate.Client.State = SlaveState.Calculating;
39            jobToCalculate.DateCalculated = DateTime.Now;
40            DaoLocator.JobDao.AssignSlaveToJob(slave.Id, jobToCalculate.Id);
41            DaoLocator.JobDao.Update(jobToCalculate);
42            DaoLocator.ClientDao.Update(jobToCalculate.Client);
43          }
44          scope.Complete();
45        }
46        /// End Critical section ///
47
48        return jobToCalculate;
49      }
50    }
51
52    #endregion
53  }
54}
Note: See TracBrowser for help on using the repository browser.