Free cookie consent management tool by TermsFeed Policy Generator

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

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

removed the distinction between controller agents and normal agents. Also removed the AgentScheduler because it is not needed anymore. (ticket #204)

File size: 4.1 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;
33using HeuristicLab.Data;
34
35namespace HeuristicLab.CEDMA.Server {
36  public class RunScheduler {
37    private class Job {
38      public long AgentId;
39      public WaitHandle WaitHandle;
40      public AtomicOperation Operation;
41    }
42    private string serverUri;
43    private Database database;
44    private JobManager jobManager;
45    private const int RELEASE_INTERVAL = 5;
46    private object remoteCommLock = new object();
47    private object queueLock = new object();
48    private Queue<Job> jobQueue;
49    private AutoResetEvent runningJobs = new AutoResetEvent(false);
50
51    public RunScheduler(Database database, JobManager jobManager, string serverUri) {
52      this.database = database;
53      this.jobManager = jobManager;
54      this.serverUri = serverUri;
55      jobQueue = new Queue<Job>();
56      Thread resultsGatheringThread = new Thread(GatherResults);
57      resultsGatheringThread.Start();
58    }
59    public void Run() {
60      while(true) {
61        ReleaseWaitingRuns();
62        Thread.Sleep(TimeSpan.FromSeconds(RELEASE_INTERVAL));
63      }
64    }
65    private void ReleaseWaitingRuns() {
66      ICollection<AgentEntry> agents;
67      lock(remoteCommLock) {
68        agents = database.GetAgents(ProcessStatus.Waiting);
69      }
70      foreach(AgentEntry entry in agents) {
71        Agent agent = (Agent)DbPersistenceManager.Restore(entry.RawData);
72        IOperatorGraph opGraph = agent.OperatorGraph;
73        Scope scope = new Scope();
74        // initialize CEDMA variables for the execution of the agent
75        scope.AddVariable(new Variable("AgentId", new IntData((int)entry.Id)));
76        scope.AddVariable(new Variable("CedmaServerUri", new StringData(serverUri)));
77        AtomicOperation op = new AtomicOperation(opGraph.InitialOperator, scope);
78        WaitHandle wHandle;
79        lock(remoteCommLock) {
80          wHandle = jobManager.BeginExecuteOperation(op.Scope, op);
81          database.UpdateAgent(entry.Id, ProcessStatus.Active);
82        }
83
84        Job job = new Job();
85        job.AgentId = entry.Id;
86        job.Operation = op;
87        job.WaitHandle = wHandle;
88
89        lock(queueLock) {
90          jobQueue.Enqueue(job);
91          runningJobs.Set();
92        }
93      }
94    }
95
96    private void GatherResults() {
97      try {
98        while(true) {
99          Job job = null;
100          lock(queueLock) if(jobQueue.Count > 0) job = jobQueue.Dequeue();
101          if(job == null) runningJobs.WaitOne();
102          else {
103            job.WaitHandle.WaitOne();
104            job.WaitHandle.Close();
105            lock(remoteCommLock) {
106              try {
107                jobManager.EndExecuteOperation(job.Operation);
108                database.UpdateAgent(job.AgentId, ProcessStatus.Finished);
109              } catch(JobExecutionException ex) {
110                database.UpdateAgent(job.AgentId, ProcessStatus.Error);
111              }
112            }
113          }
114        }
115      } finally {
116        Debug.Assert(false); // make sure we are notified when this thread is killed while debugging
117      }
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.