Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Grid/3.2/EngineStore.cs @ 2530

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

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

File size: 3.8 KB
RevLine 
[2]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.Text;
[500]25using System.Linq;
[2]26using System.Threading;
27using System.ServiceModel;
[500]28using System.Data.Common;
[2]29
30namespace HeuristicLab.Grid {
[32]31  [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
[2]32  public class EngineStore : IEngineStore {
[500]33    private Database database;
34
35    private static readonly string dbFile = AppDomain.CurrentDomain.BaseDirectory + "/enginestore.db3";
36    private static readonly string connectionString = "Data Source=\"" + dbFile + "\";Pooling=False";
37
38    private int waitingJobs;
[2]39    public int WaitingJobs {
40      get {
[500]41        return waitingJobs;
[2]42      }
43    }
[500]44    private int runningJobs;
[2]45    public int RunningJobs {
46      get {
[500]47        return runningJobs;
[2]48      }
49    }
[500]50    private int results;
[2]51    public int WaitingResults {
52      get {
[500]53        return results;
[2]54      }
55    }
[506]56    private object locker = new object();
[2]57    public EngineStore() {
[500]58      DbProviderFactory fact;
59      fact = DbProviderFactories.GetFactory("System.Data.SQLite");
60      if(!System.IO.File.Exists(dbFile)) {
61        database = new Database(connectionString);
62        database.CreateNew();
63      } else {
64        database = new Database(connectionString);
65      }
[504]66
67      // init counters
68      waitingJobs = (int)database.GetJobCount(JobState.Waiting);
69      runningJobs = (int)database.GetJobCount(JobState.Busy);
70      results = (int)database.GetJobCount(JobState.Finished);
[2]71    }
72
[219]73    public bool TryTakeEngine(out Guid guid, out byte[] engine) {
[502]74      JobEntry nextWaitingJob = database.GetNextWaitingJob();
75      if(nextWaitingJob == null) {
76        guid = Guid.Empty; engine = null;
77        return false;
78      } else {
79        guid = nextWaitingJob.Guid;
80        engine = nextWaitingJob.RawData;
[506]81        lock(locker) {
82          runningJobs++;
83          waitingJobs--;
84        }
[502]85        return true;
[2]86      }
87    }
88
89    public void StoreResult(Guid guid, byte[] result) {
[500]90      database.DeleteExpiredResults();
91      // add the new result
92      database.SetJobResult(guid, result);
[506]93      lock(locker) {
94        results++;
95        runningJobs--;
96      }
[2]97    }
98
99    internal void AddEngine(Guid guid, byte[] engine) {
[504]100      database.InsertJob(guid, JobState.Waiting, engine);
[506]101      lock(locker) {
102        waitingJobs++;
103      }
[2]104    }
105
106    internal byte[] GetResult(Guid guid) {
[504]107      if(GetJobState(guid) == JobState.Finished) {
[500]108        JobEntry entry = database.GetJob(guid);
109        return entry.RawData;
110      } else {
[501]111        // JobState is Busy, Waiting or Unknown
112        // no result yet, check for which jobs we waited too long and requeue those jobs
113        database.RestartExpiredActiveJobs();
114        return null;
[2]115      }
116    }
117
[504]118    internal JobState GetJobState(Guid guid) {
[502]119      return database.GetJobState(guid);
[219]120    }
[2]121  }
122}
Note: See TracBrowser for help on using the repository browser.