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 |
|
---|
11 | namespace HeuristicLab.Hive.Server.Core {
|
---|
12 | internal class DefaultScheduler : IScheduler {
|
---|
13 | //private ISessionFactory factory;
|
---|
14 |
|
---|
15 | private static Mutex jobLock =
|
---|
16 | new Mutex();
|
---|
17 |
|
---|
18 | #region IScheduler Members
|
---|
19 |
|
---|
20 | public DefaultScheduler() {
|
---|
21 | //factory = ServiceLocator.GetSessionFactory();
|
---|
22 | }
|
---|
23 |
|
---|
24 | public bool ExistsJobForClient(HeuristicLab.Hive.Contracts.BusinessObjects.HeartBeatData hbData) {
|
---|
25 | List<JobDto> allOfflineJobsForClient =
|
---|
26 | new List<JobDto>(DaoLocator.JobDao.FindFittingJobsForClient(State.offline, hbData.FreeCores, hbData.FreeMemory,
|
---|
27 | hbData.ClientId));
|
---|
28 | return (allOfflineJobsForClient != null && allOfflineJobsForClient.Count > 0);
|
---|
29 | }
|
---|
30 |
|
---|
31 | public HeuristicLab.Hive.Contracts.BusinessObjects.JobDto GetNextJobForClient(Guid clientId) {
|
---|
32 | /// Critical section ///
|
---|
33 | jobLock.WaitOne();
|
---|
34 |
|
---|
35 | ClientDto client = DaoLocator.ClientDao.FindById(clientId);
|
---|
36 | LinkedList<JobDto> allOfflineJobsForClient =
|
---|
37 | new LinkedList<JobDto>(DaoLocator.JobDao.FindFittingJobsForClient(State.offline, client.NrOfFreeCores,
|
---|
38 | client.FreeMemory, client.Id));
|
---|
39 |
|
---|
40 | JobDto jobToCalculate = null;
|
---|
41 | if (allOfflineJobsForClient != null && allOfflineJobsForClient.Count > 0) {
|
---|
42 | jobToCalculate = allOfflineJobsForClient.First.Value;
|
---|
43 | jobToCalculate.State = State.calculating;
|
---|
44 | jobToCalculate.Client = client;
|
---|
45 | jobToCalculate.Client.State = State.calculating;
|
---|
46 | jobToCalculate.DateCalculated = DateTime.Now;
|
---|
47 | DaoLocator.JobDao.AssignClientToJob(client.Id, jobToCalculate.Id);
|
---|
48 | DaoLocator.JobDao.Update(jobToCalculate);
|
---|
49 | DaoLocator.ClientDao.Update(jobToCalculate.Client);
|
---|
50 | }
|
---|
51 | jobLock.ReleaseMutex();
|
---|
52 | /// End Critical section ///
|
---|
53 |
|
---|
54 | return jobToCalculate;
|
---|
55 | }
|
---|
56 |
|
---|
57 | #endregion
|
---|
58 | }
|
---|
59 | } |
---|