Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1584 was 1530, checked in by gkronber, 16 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

File size: 2.5 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
[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}
Note: See TracBrowser for help on using the repository browser.