Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented job assignment to resources (#507)

File size: 2.6 KB
RevLine 
[1284]1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Hive.Server.Core.InternalInterfaces;
[1334]6using HeuristicLab.Hive.Contracts.BusinessObjects;
[1377]7using HeuristicLab.Hive.Server.DataAccess;
[1334]8using System.Threading;
[1468]9using HeuristicLab.DataAccess.Interfaces;
[1284]10
11namespace 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
[2066]32        List<Job> allOfflineJobsForClient = new List<Job>(
33          jobAdapter.FindJobs(State.offline,
34          hbData.FreeCores,
35          hbData.FreeMemory,
36          hbData.ClientId));
[1500]37        return (allOfflineJobsForClient != null && allOfflineJobsForClient.Count > 0);
[1468]38      }
39      finally {
40        if (session != null)
41          session.EndSession();
42      }
[1284]43    }
44
45    public HeuristicLab.Hive.Contracts.BusinessObjects.Job GetNextJobForClient(Guid clientId) {
[1468]46      ISession session = factory.GetSessionForCurrentThread();
[1334]47
[1468]48      try {
49        IJobAdapter jobAdapter =
50          session.GetDataAdapter<Job, IJobAdapter>();
[1334]51
[1468]52        IClientAdapter clientAdapter =
53          session.GetDataAdapter<ClientInfo, IClientAdapter>();
[1334]54
[1468]55        /// Critical section ///
56        jobLock.WaitOne();
[1334]57
[1500]58        ClientInfo client = clientAdapter.GetById(clientId);
[2066]59        LinkedList<Job> allOfflineJobsForClient = new LinkedList<Job>(
60          jobAdapter.FindJobs(State.offline,
61          client.NrOfFreeCores,
62          client.FreeMemory,
63          client.Id));
[1468]64
[1500]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;
[1468]71
[1500]72          jobToCalculate.DateCalculated = DateTime.Now;
73          jobAdapter.Update(jobToCalculate);
[1468]74        }
75        jobLock.ReleaseMutex();
76        /// End Critical section ///
77
[1500]78        return jobToCalculate;
[1334]79      }
[1468]80      finally {
81        if (session != null)
82          session.EndSession();
83      }
[1284]84    }
85
86    #endregion
87  }
88}
Note: See TracBrowser for help on using the repository browser.