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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Threading;
|
---|
27 | using System.ServiceModel;
|
---|
28 | using System.Data.Common;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Grid {
|
---|
31 | [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
|
---|
32 | public class EngineStore : IEngineStore {
|
---|
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;
|
---|
39 | public int WaitingJobs {
|
---|
40 | get {
|
---|
41 | return waitingJobs;
|
---|
42 | }
|
---|
43 | }
|
---|
44 | private int runningJobs;
|
---|
45 | public int RunningJobs {
|
---|
46 | get {
|
---|
47 | return runningJobs;
|
---|
48 | }
|
---|
49 | }
|
---|
50 | private int results;
|
---|
51 | public int WaitingResults {
|
---|
52 | get {
|
---|
53 | return results;
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | public EngineStore() {
|
---|
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 | }
|
---|
66 | database = new Database(connectionString);
|
---|
67 | }
|
---|
68 |
|
---|
69 | private object t = new object();
|
---|
70 | public bool TryTakeEngine(out Guid guid, out byte[] engine) {
|
---|
71 | lock(t) {
|
---|
72 | List<JobEntry> waitingJobs = database.GetWaitingJobs();
|
---|
73 | if(waitingJobs.Count == 0) {
|
---|
74 | guid = Guid.Empty; engine = null;
|
---|
75 | return false;
|
---|
76 | } else {
|
---|
77 | JobEntry oldestEntry = waitingJobs.OrderBy(a => a.CreationTime).First();
|
---|
78 | guid = oldestEntry.Guid;
|
---|
79 | engine = oldestEntry.RawData;
|
---|
80 | database.UpdateJobState(guid, HeuristicLab.Grid.JobState.Busy);
|
---|
81 | return true;
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | public void StoreResult(Guid guid, byte[] result) {
|
---|
87 | database.DeleteExpiredResults();
|
---|
88 | // add the new result
|
---|
89 | database.SetJobResult(guid, result);
|
---|
90 | }
|
---|
91 |
|
---|
92 | internal void AddEngine(Guid guid, byte[] engine) {
|
---|
93 | database.InsertJob(guid, HeuristicLab.Grid.JobState.Waiting, engine);
|
---|
94 | }
|
---|
95 |
|
---|
96 | internal byte[] GetResult(Guid guid) {
|
---|
97 | if(JobState(guid) == HeuristicLab.Grid.JobState.Finished) {
|
---|
98 | JobEntry entry = database.GetJob(guid);
|
---|
99 | return entry.RawData;
|
---|
100 | } else {
|
---|
101 | // JobState is Busy, Waiting or Unknown
|
---|
102 | // no result yet, check for which jobs we waited too long and requeue those jobs
|
---|
103 | database.RestartExpiredActiveJobs();
|
---|
104 | return null;
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | internal JobState JobState(Guid guid) {
|
---|
109 | return database.GetJob(guid).Status;
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|