#region License Information /* HeuristicLab * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Net; using System.Threading; using HeuristicLab.Clients.Hive.SlaveCore; using Microsoft.WindowsAzure.Diagnostics; using Microsoft.WindowsAzure.ServiceRuntime; using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.StorageClient; using System.Text; namespace HeuristicLab.Clients.Hive.Slave.AzureClient { public class WorkerRole : RoleEntryPoint { private Core core; private Thread coreThread; private const string HiveServerAddressSetting = "HiveServerAddress"; private const string HiveServerCertificateSetting = "HiveServerCertifcateEncodedValue"; public override void Run() { try { LogError(new Exception("Starting Run()...")); core = new Core(false); string hiveServerAddress = RoleEnvironment.GetConfigurationSettingValue(HiveServerAddressSetting); string hiveServerCertificate = RoleEnvironment.GetConfigurationSettingValue(HiveServerCertificateSetting); // values are empty, settings from app.config are used if (!string.IsNullOrEmpty(hiveServerAddress) && !string.IsNullOrEmpty(hiveServerCertificate)) { core.SetNewHiveServer(hiveServerAddress, hiveServerCertificate); } coreThread = new Thread(core.Start); coreThread.Start(); LogError(new Exception("Run(): Core started successfullly")); while (true) { Thread.Sleep(10000); } } catch (Exception e) { LogError(e); throw e; } } public override bool OnStart() { try { ServicePointManager.DefaultConnectionLimit = 12; //core = new Core(); LogError(new Exception("Running OnStart()")); try { if (!String.IsNullOrEmpty(RoleEnvironment.GetConfigurationSettingValue(Constants.DiagnosticsConnectionString))) { DiagnosticMonitorConfiguration dmc = DiagnosticMonitor.GetDefaultInitialConfiguration(); dmc.Logs.ScheduledTransferPeriod = TimeSpan.FromSeconds(5); dmc.Logs.ScheduledTransferLogLevelFilter = LogLevel.Verbose; DiagnosticMonitor.Start(Constants.DiagnosticsConnectionString, dmc); } } catch (RoleEnvironmentException ex) { // diagnostics connection string not in configuration // -> diagnostics disabled // nothing more to do LogError(ex); } RoleEnvironment.Changed += RoleEnvironmentChanged; LogError(new Exception("Finished OnStart() successfullly")); } catch (Exception ex) { LogError(ex); throw ex; } return base.OnStart(); } private void LogError(Exception e) { var storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue(Constants.DiagnosticsConnectionString)); var blobClient = storageAccount.CreateCloudBlobClient(); var container = blobClient.GetContainerReference("slavelog"); container.CreateIfNotExist(); var guid = Guid.NewGuid(); var blob = container.GetBlobReference(guid.ToString()); var builder = new StringBuilder(); builder.Append("ToString: ").Append(e.ToString()).Append("\n"); builder.Append("Message: ").Append(e.Message).Append("\n"); blob.UploadText(builder.ToString()); } public override void OnStop() { core.Shutdown(); base.OnStop(); } private void RoleEnvironmentChanged(object sender, RoleEnvironmentChangedEventArgs e) { } } }