Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CEDMA.Server/RunScheduler.cs @ 390

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

unified runs and agents (are actually the same with the minor difference that agents can create new agents (runs)) (ticket #188)

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.CEDMA.DB;
27using HeuristicLab.CEDMA.DB.Interfaces;
28using HeuristicLab.Core;
29using System.Threading;
30using HeuristicLab.CEDMA.Core;
31using HeuristicLab.Grid;
32using System.Diagnostics;
33
34namespace HeuristicLab.CEDMA.Server {
35  public class RunScheduler {
36    private class Job {
37      public long AgentId;
38      public WaitHandle WaitHandle;
39      public AtomicOperation Operation;
40    }
41    private Database database;
42    private JobManager jobManager;
43    private const int RELEASE_INTERVAL = 5;
44    private object remoteCommLock = new object();
45    private object queueLock = new object();
46    private Queue<Job> jobQueue;
47
48    public RunScheduler(Database database, JobManager jobManager) {
49      this.database = database;
50      this.jobManager = jobManager;
51      jobQueue = new Queue<Job>();
52      Thread resultsGatheringThread = new Thread(GatherResults);
53      resultsGatheringThread.Start();
54    }
55    public void Run() {
56      while(true) {
57        ReleaseWaitingRuns();
58        Thread.Sleep(TimeSpan.FromSeconds(RELEASE_INTERVAL));
59      }
60    }
61    private void ReleaseWaitingRuns() {
62      IEnumerable<AgentEntry> agents;
63      lock(remoteCommLock) {
64        agents = database.GetAgents(ProcessStatus.Waiting).Where(a=>!a.ControllerAgent);
65      }
66      foreach(AgentEntry entry in agents) {
67        IOperatorGraph opGraph = (IOperatorGraph)DbPersistenceManager.Restore(entry.RawData);
68        AtomicOperation op = new AtomicOperation(opGraph.InitialOperator, new Scope());
69        WaitHandle wHandle;
70        lock(remoteCommLock) {
71          wHandle = jobManager.BeginExecuteOperation(op.Scope, op);
72          database.UpdateAgent(entry.Id, ProcessStatus.Active);
73        }
74
75        Job job = new Job();
76        job.AgentId = entry.Id;
77        job.Operation = op;
78        job.WaitHandle = wHandle;
79
80        lock(queueLock) {
81          jobQueue.Enqueue(job);
82        }
83      }
84    }
85
86    private void GatherResults() {
87      try {
88        while(true) {
89          int runningJobs;
90          lock(queueLock) runningJobs = jobQueue.Count;
91          if(runningJobs==0) Thread.Sleep(1000); // TASK: replace with waithandle
92          else {
93            Job job;
94            lock(queueLock) {
95              job = jobQueue.Dequeue();
96            }
97            job.WaitHandle.WaitOne();
98            job.WaitHandle.Close();
99            lock(remoteCommLock) {
100              jobManager.EndExecuteOperation(job.Operation);
101              database.UpdateAgent(job.AgentId, ProcessStatus.Finished);
102            }
103          }
104        }
105      } finally {
106        Debug.Assert(false); // make sure we are notified when this thread is killed while debugging
107      }
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.