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 @ 4171

Last change on this file since 4171 was 4121, checked in by cneumuel, 15 years ago

Stabilization of Hive, Improvement HiveExperiment GUI (#1115)

File size: 2.4 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    //private ISessionFactory factory;
16
17    private static Mutex jobLock =
18      new Mutex();
19
20    #region IScheduler Members
21
22    public DefaultScheduler() {
23      //factory = ServiceLocator.GetSessionFactory();
24    }
25
26    public bool ExistsJobForClient(HeuristicLab.Hive.Contracts.BusinessObjects.HeartBeatData hbData) {
27      List<JobDto> allOfflineJobsForClient =
28        new List<JobDto>(DaoLocator.JobDao.FindFittingJobsForClient(State.Offline, hbData.FreeCores, hbData.FreeMemory, hbData.ClientId));
29      return (allOfflineJobsForClient != null && allOfflineJobsForClient.Count > 0);
30    }
31
32    public HeuristicLab.Hive.Contracts.BusinessObjects.JobDto GetNextJobForClient(Guid clientId) {
33      /// Critical section ///
34      jobLock.WaitOne();
35      JobDto jobToCalculate = null;
36      using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = ApplicationConstants.ISOLATION_LEVEL_SCOPE })) {
37        ClientDto client = DaoLocator.ClientDao.FindById(clientId);
38        LinkedList<JobDto> allOfflineJobsForClient =
39          new LinkedList<JobDto>(DaoLocator.JobDao.FindFittingJobsForClient(State.Offline, client.NrOfFreeCores, client.FreeMemory, client.Id));       
40        if (allOfflineJobsForClient != null && allOfflineJobsForClient.Count > 0) {
41          jobToCalculate = allOfflineJobsForClient.First.Value;
42          jobToCalculate.State = State.Calculating;
43          jobToCalculate.Client = client;
44          jobToCalculate.Client.State = State.Calculating;
45          jobToCalculate.DateCalculated = DateTime.Now;
46          DaoLocator.JobDao.AssignClientToJob(client.Id, jobToCalculate.Id);
47          DaoLocator.JobDao.Update(jobToCalculate);
48          DaoLocator.ClientDao.Update(jobToCalculate.Client);
49        }
50        scope.Complete();
51      }
52      jobLock.ReleaseMutex();
53      /// End Critical section ///
54
55      return jobToCalculate;
56    }
57
58    #endregion
59  }
60}
Note: See TracBrowser for help on using the repository browser.