[3011] | 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 {
|
---|
[3018] | 12 | internal class DefaultScheduler : IScheduler {
|
---|
[3011] | 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) {
|
---|
[3018] | 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 | }
|
---|
[3011] | 30 |
|
---|
| 31 | public HeuristicLab.Hive.Contracts.BusinessObjects.JobDto GetNextJobForClient(Guid clientId) {
|
---|
[3018] | 32 | /// Critical section ///
|
---|
| 33 | jobLock.WaitOne();
|
---|
[3011] | 34 |
|
---|
[3018] | 35 | ClientDto client = DaoLocator.ClientDao.FindById(clientId);
|
---|
[3011] | 36 | LinkedList<JobDto> allOfflineJobsForClient =
|
---|
| 37 | new LinkedList<JobDto>(DaoLocator.JobDao.FindFittingJobsForClient(State.offline, client.NrOfFreeCores,
|
---|
[3018] | 38 | client.FreeMemory, client.Id));
|
---|
[3011] | 39 |
|
---|
[3018] | 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);
|
---|
[3011] | 50 | }
|
---|
[3018] | 51 | jobLock.ReleaseMutex();
|
---|
| 52 | /// End Critical section ///
|
---|
[3011] | 53 |
|
---|
[3018] | 54 | return jobToCalculate;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[3011] | 57 | #endregion
|
---|
| 58 | }
|
---|
[3018] | 59 | } |
---|