[1284] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using HeuristicLab.Hive.Server.Core.InternalInterfaces;
|
---|
[1334] | 6 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
[1377] | 7 | using HeuristicLab.Hive.Server.DataAccess;
|
---|
[1334] | 8 | using System.Threading;
|
---|
[1468] | 9 | using HeuristicLab.DataAccess.Interfaces;
|
---|
[1284] | 10 |
|
---|
| 11 | namespace HeuristicLab.Hive.Server.Scheduler {
|
---|
[1334] | 12 | class DefaultScheduler: IScheduler {
|
---|
| 13 |
|
---|
[1468] | 14 | private ISessionFactory factory;
|
---|
[1334] | 15 |
|
---|
| 16 | private static Mutex jobLock =
|
---|
| 17 | new Mutex();
|
---|
| 18 |
|
---|
[1284] | 19 | #region IScheduler Members
|
---|
| 20 |
|
---|
[1334] | 21 | public DefaultScheduler() {
|
---|
[1468] | 22 | factory = ServiceLocator.GetSessionFactory();
|
---|
[1334] | 23 | }
|
---|
| 24 |
|
---|
[1284] | 25 | public bool ExistsJobForClient(HeuristicLab.Hive.Contracts.BusinessObjects.HeartBeatData hbData) {
|
---|
[1468] | 26 | ISession session = factory.GetSessionForCurrentThread();
|
---|
| 27 |
|
---|
| 28 | try {
|
---|
| 29 | IJobAdapter jobAdapter =
|
---|
| 30 | session.GetDataAdapter<Job, IJobAdapter>();
|
---|
| 31 |
|
---|
[1500] | 32 | List<Job> allOfflineJobsForClient = new List<Job>(jobAdapter.FindJobs(State.offline, hbData.FreeCores, hbData.FreeMemory));
|
---|
| 33 | return (allOfflineJobsForClient != null && allOfflineJobsForClient.Count > 0);
|
---|
[1468] | 34 | }
|
---|
| 35 | finally {
|
---|
| 36 | if (session != null)
|
---|
| 37 | session.EndSession();
|
---|
| 38 | }
|
---|
[1284] | 39 | }
|
---|
| 40 |
|
---|
| 41 | public HeuristicLab.Hive.Contracts.BusinessObjects.Job GetNextJobForClient(Guid clientId) {
|
---|
[1468] | 42 | ISession session = factory.GetSessionForCurrentThread();
|
---|
[1334] | 43 |
|
---|
[1468] | 44 | try {
|
---|
| 45 | IJobAdapter jobAdapter =
|
---|
| 46 | session.GetDataAdapter<Job, IJobAdapter>();
|
---|
[1334] | 47 |
|
---|
[1468] | 48 | IClientAdapter clientAdapter =
|
---|
| 49 | session.GetDataAdapter<ClientInfo, IClientAdapter>();
|
---|
[1334] | 50 |
|
---|
[1468] | 51 | /// Critical section ///
|
---|
| 52 | jobLock.WaitOne();
|
---|
[1334] | 53 |
|
---|
[1500] | 54 | ClientInfo client = clientAdapter.GetById(clientId);
|
---|
| 55 | LinkedList<Job> allOfflineJobsForClient = new LinkedList<Job>(jobAdapter.FindJobs(State.offline, client.NrOfFreeCores, client.FreeMemory ));
|
---|
[1468] | 56 |
|
---|
[1500] | 57 | Job jobToCalculate = null;
|
---|
| 58 | if (allOfflineJobsForClient != null && allOfflineJobsForClient.Count > 0) {
|
---|
| 59 | jobToCalculate = allOfflineJobsForClient.First.Value;
|
---|
| 60 | jobToCalculate.State = State.calculating;
|
---|
| 61 | jobToCalculate.Client = client;
|
---|
| 62 | jobToCalculate.Client.State = State.calculating;
|
---|
[1468] | 63 |
|
---|
[1500] | 64 | jobToCalculate.DateCalculated = DateTime.Now;
|
---|
| 65 | jobAdapter.Update(jobToCalculate);
|
---|
[1468] | 66 | }
|
---|
| 67 | jobLock.ReleaseMutex();
|
---|
| 68 | /// End Critical section ///
|
---|
| 69 |
|
---|
[1500] | 70 | return jobToCalculate;
|
---|
[1334] | 71 | }
|
---|
[1468] | 72 | finally {
|
---|
| 73 | if (session != null)
|
---|
| 74 | session.EndSession();
|
---|
| 75 | }
|
---|
[1284] | 76 | }
|
---|
| 77 |
|
---|
| 78 | #endregion
|
---|
| 79 | }
|
---|
| 80 | }
|
---|