Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Clients.Hive.Slave.AzureClient/3.3/WorkerRole.cs @ 9375

Last change on this file since 9375 was 9375, checked in by fschoepp, 11 years ago

#1888:

  • Getting connection information from configuration file
File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Net;
24using System.Threading;
25using HeuristicLab.Clients.Hive.SlaveCore;
26using Microsoft.WindowsAzure.Diagnostics;
27using Microsoft.WindowsAzure.ServiceRuntime;
28using Microsoft.WindowsAzure;
29using Microsoft.WindowsAzure.StorageClient;
30using System.Text;
31
32namespace HeuristicLab.Clients.Hive.Slave.AzureClient {
33
34
35  public class WorkerRole : RoleEntryPoint {
36    private Core core;
37    private Thread coreThread;
38
39    private const string HiveServerAddressSetting = "HiveServerAddress";
40    private const string HiveServerCertificateSetting = "HiveServerCertifcateEncodedValue";
41
42    public override void Run() {
43      try {
44        LogError(new Exception("Starting Run()..."));
45        core = new Core(false);
46
47        string hiveServerAddress = RoleEnvironment.GetConfigurationSettingValue(HiveServerAddressSetting);
48        string hiveServerCertificate = RoleEnvironment.GetConfigurationSettingValue(HiveServerCertificateSetting);
49
50        // values are empty, settings from app.config are used
51        if (!string.IsNullOrEmpty(hiveServerAddress) && !string.IsNullOrEmpty(hiveServerCertificate)) {
52          core.SetNewHiveServer(hiveServerAddress, hiveServerCertificate);
53        }
54
55        coreThread = new Thread(core.Start);
56        coreThread.Start();
57
58        LogError(new Exception("Run(): Core started successfullly"));
59        while (true) {
60          Thread.Sleep(10000);
61        }
62      }
63      catch (Exception e) {
64        LogError(e);
65    throw e;
66      }
67    }
68
69    public override bool OnStart() {
70      try {
71        ServicePointManager.DefaultConnectionLimit = 12;
72        //core = new Core();
73        LogError(new Exception("Running OnStart()"));
74        try {
75          if (!String.IsNullOrEmpty(RoleEnvironment.GetConfigurationSettingValue(Constants.DiagnosticsConnectionString))) {
76            DiagnosticMonitorConfiguration dmc = DiagnosticMonitor.GetDefaultInitialConfiguration();
77            dmc.Logs.ScheduledTransferPeriod = TimeSpan.FromSeconds(5);
78            dmc.Logs.ScheduledTransferLogLevelFilter = LogLevel.Verbose;
79            DiagnosticMonitor.Start(Constants.DiagnosticsConnectionString, dmc);
80          }
81        }
82        catch (RoleEnvironmentException ex) {
83          // diagnostics connection string not in configuration
84          // -> diagnostics disabled
85          // nothing more to do
86          LogError(ex);
87        }
88
89        RoleEnvironment.Changed += RoleEnvironmentChanged;
90        LogError(new Exception("Finished OnStart() successfullly"));
91      }
92      catch (Exception ex) {
93        LogError(ex);
94    throw ex;
95      }
96      return base.OnStart();
97    }
98
99    private void LogError(Exception e) {
100      var storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue(Constants.DiagnosticsConnectionString));
101      var blobClient = storageAccount.CreateCloudBlobClient();
102      var container = blobClient.GetContainerReference("slavelog");
103      container.CreateIfNotExist();
104      var guid = Guid.NewGuid();
105      var blob = container.GetBlobReference(guid.ToString());
106      var builder = new StringBuilder();
107      builder.Append("ToString: ").Append(e.ToString()).Append("\n");
108      builder.Append("Message: ").Append(e.Message).Append("\n");
109      blob.UploadText(builder.ToString());
110    }
111
112    public override void OnStop() {
113      core.Shutdown();
114      base.OnStop();
115    }
116
117    private void RoleEnvironmentChanged(object sender, RoleEnvironmentChangedEventArgs e) {
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.