Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.Hive.Server.Core/3.2/DefaultScheduler.cs @ 4042

Last change on this file since 4042 was 4042, checked in by kgrading, 14 years ago

#828 added various improvements to the plugin cache manager, the execution engine, the transaction handling on the serverside and the server console

File size: 2.6 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,
29                                                                    hbData.ClientId));
30      return (allOfflineJobsForClient != null && allOfflineJobsForClient.Count > 0);
31    }
32
33    public HeuristicLab.Hive.Contracts.BusinessObjects.JobDto GetNextJobForClient(Guid clientId) {
34      /// Critical section ///
35      jobLock.WaitOne();
36      JobDto jobToCalculate = null;
37      using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = ApplicationConstants.ISOLATION_LEVEL_SCOPE })) {
38        ClientDto client = DaoLocator.ClientDao.FindById(clientId);
39        LinkedList<JobDto> allOfflineJobsForClient =
40          new LinkedList<JobDto>(DaoLocator.JobDao.FindFittingJobsForClient(State.offline, client.NrOfFreeCores,
41                                                                            client.FreeMemory, client.Id));       
42        if (allOfflineJobsForClient != null && allOfflineJobsForClient.Count > 0) {
43          jobToCalculate = allOfflineJobsForClient.First.Value;
44          jobToCalculate.State = State.calculating;
45          jobToCalculate.Client = client;
46          jobToCalculate.Client.State = State.calculating;
47          jobToCalculate.DateCalculated = DateTime.Now;
48          DaoLocator.JobDao.AssignClientToJob(client.Id, jobToCalculate.Id);
49          DaoLocator.JobDao.Update(jobToCalculate);
50          DaoLocator.ClientDao.Update(jobToCalculate.Client);
51        }
52        scope.Complete();
53      }
54      jobLock.ReleaseMutex();
55      /// End Critical section ///
56
57      return jobToCalculate;
58    }
59
60    #endregion
61  }
62}
Note: See TracBrowser for help on using the repository browser.