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.Scheduler {
|
---|
12 | class DefaultScheduler: IScheduler {
|
---|
13 |
|
---|
14 | private ISessionFactory factory;
|
---|
15 |
|
---|
16 | private static Mutex jobLock =
|
---|
17 | new Mutex();
|
---|
18 |
|
---|
19 | #region IScheduler Members
|
---|
20 |
|
---|
21 | public DefaultScheduler() {
|
---|
22 | factory = ServiceLocator.GetSessionFactory();
|
---|
23 | }
|
---|
24 |
|
---|
25 | public bool ExistsJobForClient(HeuristicLab.Hive.Contracts.BusinessObjects.HeartBeatData hbData) {
|
---|
26 | ISession session = factory.GetSessionForCurrentThread();
|
---|
27 |
|
---|
28 | try {
|
---|
29 | IJobAdapter jobAdapter =
|
---|
30 | session.GetDataAdapter<Job, IJobAdapter>();
|
---|
31 |
|
---|
32 | List<Job> allOfflineJobsForClient = new List<Job>(jobAdapter.FindJobs(State.offline, hbData.FreeCores, hbData.FreeMemory));
|
---|
33 | return (allOfflineJobsForClient != null && allOfflineJobsForClient.Count > 0);
|
---|
34 | }
|
---|
35 | finally {
|
---|
36 | if (session != null)
|
---|
37 | session.EndSession();
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | public HeuristicLab.Hive.Contracts.BusinessObjects.Job GetNextJobForClient(Guid clientId) {
|
---|
42 | ISession session = factory.GetSessionForCurrentThread();
|
---|
43 |
|
---|
44 | try {
|
---|
45 | IJobAdapter jobAdapter =
|
---|
46 | session.GetDataAdapter<Job, IJobAdapter>();
|
---|
47 |
|
---|
48 | IClientAdapter clientAdapter =
|
---|
49 | session.GetDataAdapter<ClientInfo, IClientAdapter>();
|
---|
50 |
|
---|
51 | /// Critical section ///
|
---|
52 | jobLock.WaitOne();
|
---|
53 |
|
---|
54 | ClientInfo client = clientAdapter.GetById(clientId);
|
---|
55 | LinkedList<Job> allOfflineJobsForClient = new LinkedList<Job>(jobAdapter.FindJobs(State.offline, client.NrOfFreeCores, client.FreeMemory ));
|
---|
56 |
|
---|
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;
|
---|
63 |
|
---|
64 | jobToCalculate.DateCalculated = DateTime.Now;
|
---|
65 | jobAdapter.Update(jobToCalculate);
|
---|
66 | }
|
---|
67 | jobLock.ReleaseMutex();
|
---|
68 | /// End Critical section ///
|
---|
69 |
|
---|
70 | return jobToCalculate;
|
---|
71 | }
|
---|
72 | finally {
|
---|
73 | if (session != null)
|
---|
74 | session.EndSession();
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | #endregion
|
---|
79 | }
|
---|
80 | }
|
---|