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>(
|
---|
33 | jobAdapter.FindJobs(State.offline,
|
---|
34 | hbData.FreeCores,
|
---|
35 | hbData.FreeMemory,
|
---|
36 | hbData.ClientId));
|
---|
37 | return (allOfflineJobsForClient != null && allOfflineJobsForClient.Count > 0);
|
---|
38 | }
|
---|
39 | finally {
|
---|
40 | if (session != null)
|
---|
41 | session.EndSession();
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public HeuristicLab.Hive.Contracts.BusinessObjects.Job GetNextJobForClient(Guid clientId) {
|
---|
46 | ISession session = factory.GetSessionForCurrentThread();
|
---|
47 |
|
---|
48 | try {
|
---|
49 | IJobAdapter jobAdapter =
|
---|
50 | session.GetDataAdapter<Job, IJobAdapter>();
|
---|
51 |
|
---|
52 | IClientAdapter clientAdapter =
|
---|
53 | session.GetDataAdapter<ClientInfo, IClientAdapter>();
|
---|
54 |
|
---|
55 | /// Critical section ///
|
---|
56 | jobLock.WaitOne();
|
---|
57 |
|
---|
58 | ClientInfo client = clientAdapter.GetById(clientId);
|
---|
59 | LinkedList<Job> allOfflineJobsForClient = new LinkedList<Job>(
|
---|
60 | jobAdapter.FindJobs(State.offline,
|
---|
61 | client.NrOfFreeCores,
|
---|
62 | client.FreeMemory,
|
---|
63 | client.Id));
|
---|
64 |
|
---|
65 | Job jobToCalculate = null;
|
---|
66 | if (allOfflineJobsForClient != null && allOfflineJobsForClient.Count > 0) {
|
---|
67 | jobToCalculate = allOfflineJobsForClient.First.Value;
|
---|
68 | jobToCalculate.State = State.calculating;
|
---|
69 | jobToCalculate.Client = client;
|
---|
70 | jobToCalculate.Client.State = State.calculating;
|
---|
71 |
|
---|
72 | jobToCalculate.DateCalculated = DateTime.Now;
|
---|
73 | jobAdapter.Update(jobToCalculate);
|
---|
74 | }
|
---|
75 | jobLock.ReleaseMutex();
|
---|
76 | /// End Critical section ///
|
---|
77 |
|
---|
78 | return jobToCalculate;
|
---|
79 | }
|
---|
80 | finally {
|
---|
81 | if (session != null)
|
---|
82 | session.EndSession();
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | #endregion
|
---|
87 | }
|
---|
88 | }
|
---|