Skip to content

Instantly share code, notes, and snippets.

@robdmoore
Last active December 16, 2015 18:09
Show Gist options
  • Save robdmoore/5475809 to your computer and use it in GitHub Desktop.
Save robdmoore/5475809 to your computer and use it in GitHub Desktop.
Sample app to test Azure Web Farm background worker
using System;
using System.Threading;
using AzureToolkit;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
namespace ConsoleApplication1
{
public class ConsoleResult : TableServiceEntity
{
public ConsoleResult(string filePath, string machineName, int consoleVersion, int count, bool continueRunning, bool runForever)
{
PartitionKey = machineName + ";" + filePath;
RowKey = DateTime.UtcNow.ToString("o");
ConsoleVersion = consoleVersion;
Count = count;
ContinueRunning = continueRunning;
RunForever = runForever;
}
public int ConsoleVersion { get; set; }
public int Count { get; set; }
public bool ContinueRunning { get; set; }
public bool RunForever { get; set; }
}
class Program
{
static int Main()
{
const string connectionString = "AccountName=ZZZ;AccountKey=ZZZ;DefaultEndpointsProtocol=https";
var filePath = Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", "").Replace("/", "_").Replace("\\", "_").Replace(" ", "_").Replace(":", "_");
var storageFactory = new AzureStorageFactory(CloudStorageAccount.Parse(connectionString));
var table = storageFactory.GetTable<ConsoleResult>(typeof(ConsoleResult).Name);
table.Initialize();
var random = new Random();
var count = 1;
while (count <= 5)
{
var continueRunning = random.Next(0, 1000) < 750;
var runForever = random.Next(0, 1000) > 900;
table.Add(new ConsoleResult(filePath, Environment.MachineName, 1 /* Increment this every deploy to see the app updating */, count, continueRunning, runForever));
if (!continueRunning)
return 1;
if (runForever)
while (true) { Thread.Sleep(TimeSpan.FromSeconds(1)); }
count++;
}
return 0;
}
}
}
@robdmoore
Copy link
Author

@MattDavies - this has been incredibly useful. I've done some really solid testing on this now and I'm pretty happy it's working great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment