Created
May 10, 2012 15:36
-
-
Save ioleksiy/2653956 to your computer and use it in GitHub Desktop.
Belz. Using AppHarbor as .Net worker mule
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using Belz; | |
using Belz.QueueProvider; | |
using Belz.QueueProvider.IronMq; | |
using Prowlin; | |
namespace Daemon | |
{ | |
class Program | |
{ | |
public static readonly string ProwlApiKey = "<YOUR PROWL API KEY HERE>"; | |
public static readonly string IronMqProjectId = "<YOUR IRONMQ PROJECT ID>"; | |
public static readonly string IronMqToken = "<YOUR IRONMQ TOKEN>"; | |
// Main infinite daemon method | |
static void Main() | |
{ | |
// We will be using queue with name "test" | |
using (var q = new BelzProviderIronMq(IronMqToken, IronMqProjectId, "test")) | |
{ | |
using (var belz = new Belz.Belz(q)) | |
{ | |
// 5 parallel threads will be waiting for work | |
belz.OperationalThreadsCount = 5; | |
// every 60 seconds we will check the input queue | |
belz.QueueCheckFrequency = 60; | |
// We are registering handler fibonacci for "fibonacci" worker | |
belz.RegisterWorker<WorkerFibonacci>("fibonacci"); | |
// Run monitoring (in separate thread) | |
belz.Run(); | |
while (true) | |
{ | |
// Infinite loop | |
Thread.Sleep(100); | |
} | |
} | |
} | |
} | |
} | |
internal class WorkerFibonacci : BeltzWorker | |
{ | |
// Fibonacci calculation | |
private static int Fibonacci(int n) | |
{ | |
var a = 0; | |
var b = 1; | |
for (var i = 0; i < n; i++) | |
{ | |
var temp = a; | |
a = b; | |
b = temp + b; | |
} | |
return a; | |
} | |
// Notification with prowl | |
private static void ProwlNotify(int initial, int n) | |
{ | |
var notification = new Notification | |
{ | |
Application = "Prowlin.BelzTry", | |
Description = | |
"Value of " + initial + " is " + n, | |
Event = "Fibonacci calculated", | |
Priority = NotificationPriority.Normal | |
}; | |
notification.AddApiKey(Program.ProwlApiKey); | |
var prowlClient = new ProwlClient(); | |
prowlClient.SendNotification(notification); | |
} | |
// Main worker method | |
protected override void ExecuteNode(QueueMessage message) | |
{ | |
Console.WriteLine("Started: " + message); | |
var n1 = Int32.Parse(message.Parameters["n"]); | |
var n2 = Fibonacci(n1); | |
ProwlNotify(n1, n2); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Belz.QueueProvider; | |
using Belz.QueueProvider.IronMq; | |
namespace ConsoleApplication2 | |
{ | |
class Program | |
{ | |
public static readonly string IronMqProjectId = "<YOUR IRONMQ PROJECT ID>"; | |
public static readonly string IronMqToken = "<YOUR IRONMQ TOKEN>"; | |
static void Main(string[] args) | |
{ | |
// from command line with 1st parameter will be input value | |
if (args == null || args.Length != 1) | |
throw new ArgumentException("No fibonacci number supplied"); | |
using (var q = new BelzProviderIronMq(IronMqToken, IronMqProjectId, "test")) | |
{ | |
q.Enqueue(new QueueMessage("fibonacci").WithParameter("n", args[0])); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment