Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.Scheduler/3.2/DefaultScheduler.cs @ 2519

Last change on this file since 2519 was 2066, checked in by svonolfe, 15 years ago

Implemented job assignment to resources (#507)

File size: 2.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Hive.Server.Core.InternalInterfaces;
6using HeuristicLab.Hive.Contracts.BusinessObjects;
7using HeuristicLab.Hive.Server.DataAccess;
8using System.Threading;
9using HeuristicLab.DataAccess.Interfaces;
10
11namespace 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}
Note: See TracBrowser for help on using the repository browser.