Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added transaction management (#527)

File size: 2.3 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> allOfflineJobs = new List<Job>(jobAdapter.GetJobsByState(State.offline));
33        return (allOfflineJobs.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        LinkedList<Job> allOfflineJobs = new LinkedList<Job>(jobAdapter.GetJobsByState(State.offline));
55
56        Job job2Calculate = null;
57        if (allOfflineJobs != null && allOfflineJobs.Count > 0) {
58          job2Calculate = allOfflineJobs.First.Value;
59          job2Calculate.State = State.calculating;
60          job2Calculate.Client = clientAdapter.GetById(clientId);
61          job2Calculate.Client.State = State.calculating;
62
63          job2Calculate.DateCalculated = DateTime.Now;
64          jobAdapter.Update(job2Calculate);
65        }
66        jobLock.ReleaseMutex();
67        /// End Critical section ///
68
69        return job2Calculate;
70      }
71      finally {
72        if (session != null)
73          session.EndSession();
74      }
75    }
76
77    #endregion
78  }
79}
Note: See TracBrowser for help on using the repository browser.