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 | //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 | } |
---|