public static OrchestrationState WaitForInstance(TaskHubClient taskHubClient, OrchestrationInstance instance, int timeoutSeconds) { OrchestrationStatus status = OrchestrationStatus.Running; if (instance == null || string.IsNullOrWhiteSpace(instance.InstanceId)) { throw new ArgumentException("instance"); } int sleepForSeconds = 30; while (timeoutSeconds > 0) { try { var state = taskHubClient.GetOrchestrationState(instance.InstanceId); if (state != null) status = state.OrchestrationStatus; if (status == OrchestrationStatus.Running) { System.Threading.Thread.Sleep(sleepForSeconds * 1000); timeoutSeconds -= sleepForSeconds; } else { // Session state deleted after completion return state; } } catch (Exception ex) { Console.WriteLine(string.Format("Error retrieving state for instance [instanceId: '{0}', executionId: '{1}'].", instance.InstanceId, instance.ExecutionId)); Console.WriteLine(ex.ToString()); } } throw new TimeoutException("Timeout expired: " + timeoutSeconds.ToString()); }
private static void run() { string connectionString = ConfigurationManager.AppSettings["SbConnStr"]; string taskHubName = ConfigurationManager.AppSettings["TaskHubName"]; TaskHubClient taskHubClient = new TaskHubClient(taskHubName, connectionString); TaskHubWorker taskHubWorker = new TaskHubWorker(taskHubName, connectionString); taskHubWorker.CreateHub(); OrchestrationInstance instance = taskHubClient.CreateOrchestrationInstance(typeof(WfTaskOrchestration), "Hello"); taskHubWorker.AddTaskOrchestrations(typeof(WfTaskOrchestration)); taskHubWorker.AddTaskActivities(new WOEIDLookupTask(), new WForecastsTask()); taskHubWorker.Start(); Console.WriteLine("Press any key to quit."); Console.ReadLine(); taskHubWorker.Stop(true); }
static void Main(string[] args) { string tableConnectionString = ConfigurationManager.AppSettings["StorageConnectionString"]; if (CommandLine.Parser.Default.ParseArgumentsStrict(args, options)) { string connectionString = ConfigurationManager.ConnectionStrings["Microsoft.ServiceBus.ConnectionString"].ConnectionString; string taskHubName = ConfigurationManager.AppSettings["TaskHubName"]; TaskHubClient taskHubClient = new TaskHubClient(taskHubName, connectionString, tableConnectionString); TaskHubWorkerSettings settings = new TaskHubWorkerSettings(); settings.TaskOrchestrationDispatcherSettings.CompressOrchestrationState = bool.Parse(ConfigurationManager.AppSettings["CompressOrchestrationState"]); settings.TaskActivityDispatcherSettings.MaxConcurrentActivities = int.Parse(ConfigurationManager.AppSettings["MaxConcurrentActivities"]); settings.TaskOrchestrationDispatcherSettings.MaxConcurrentOrchestrations = int.Parse(ConfigurationManager.AppSettings["MaxConcurrentOrchestrations"]); TaskHubWorker taskHub = new TaskHubWorker(taskHubName, connectionString, tableConnectionString, settings); if (options.CreateHub) { taskHub.CreateHub(); } OrchestrationInstance instance = null; string instanceId = options.StartInstance; if (!string.IsNullOrWhiteSpace(instanceId)) { instance = taskHubClient.CreateOrchestrationInstance(typeof(DriverOrchestration), instanceId, new DriverOrchestrationData { NumberOfIteration = int.Parse(ConfigurationManager.AppSettings["DriverOrchestrationIterations"]), NumberOfParallelTasks = int.Parse(ConfigurationManager.AppSettings["DriverOrchestrationParallelTasks"]), SubOrchestrationData = new TestOrchestrationData { NumberOfParallelTasks = int.Parse(ConfigurationManager.AppSettings["ChildOrchestrationParallelTasks"]), NumberOfSerialTasks = int.Parse(ConfigurationManager.AppSettings["ChildOrchestrationSerialTasks"]), MaxDelayInSeconds = int.Parse(ConfigurationManager.AppSettings["TestTaskMaxDelayInMinutes"]), }, }); } else { instance = new OrchestrationInstance { InstanceId = options.InstanceId }; } Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); TestTask testTask = new TestTask(); taskHub.AddTaskActivities(testTask); taskHub.AddTaskOrchestrations(typeof(DriverOrchestration)); taskHub.AddTaskOrchestrations(typeof(TestOrchestration)); taskHub.Start(); int testTimeoutInSeconds = int.Parse(ConfigurationManager.AppSettings["TestTimeoutInSeconds"]); OrchestrationState state = WaitForInstance(taskHubClient, instance, testTimeoutInSeconds); stopWatch.Stop(); Console.WriteLine("Orchestration Status: " + state.OrchestrationStatus.ToString()); Console.WriteLine("Orchestration Result: " + state.Output); Console.WriteLine("Counter: " + testTask.counter); TimeSpan totalTime = stopWatch.Elapsed; string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", totalTime.Hours, totalTime.Minutes, totalTime.Seconds, totalTime.Milliseconds / 10); Console.WriteLine("Total Time: " + elapsedTime); taskHub.Stop(); } }
public static string GetInstanceNotCompletedMessage(TaskHubClient taskHubClient, OrchestrationInstance instance, int timeWaited) { if (instance == null || string.IsNullOrWhiteSpace(instance.InstanceId)) { throw new ArgumentException("instance"); } string history = PrintHistory(taskHubClient, instance); string message = string.Format("Instance '{0}' not completed within {1} seconds.\n History: {2}", instance, timeWaited, history); return message; }
public static string PrintHistory(TaskHubClient taskHubClient, OrchestrationInstance instance) { return taskHubClient.GetOrchestrationHistory(instance); }
public static bool WaitForInstance(TaskHubClient taskHubClient, OrchestrationInstance instance, int timeoutSeconds, bool waitForCompletion = true) { if (instance == null || string.IsNullOrWhiteSpace(instance.InstanceId)) { throw new ArgumentException("instance"); } int sleepForSeconds = 2; while (timeoutSeconds > 0) { OrchestrationState state = taskHubClient.GetOrchestrationState(instance.InstanceId); if (state == null || (waitForCompletion && state.OrchestrationStatus == OrchestrationStatus.Running)) { Thread.Sleep(sleepForSeconds*1000); timeoutSeconds -= sleepForSeconds; } else { // Session state deleted after completion return true; } } return false; }
public void TestInitialize() { client = TestHelpers.CreateTaskHubClient(); taskHub = TestHelpers.CreateTaskHub(); }
static void Main(string[] args) { if (CommandLine.Parser.Default.ParseArgumentsStrict(args, options)) { string servicebusConnectionString = Program.GetSetting("ServiceBusConnectionString"); string storageConnectionString = Program.GetSetting("StorageConnectionString"); string taskHubName = ConfigurationManager.AppSettings["taskHubName"]; TaskHubClient taskHubClient = new TaskHubClient(taskHubName, servicebusConnectionString, storageConnectionString); TaskHubWorker taskHub = new TaskHubWorker(taskHubName, servicebusConnectionString, storageConnectionString); if (options.CreateHub) { taskHub.CreateHub(); } if (!string.IsNullOrWhiteSpace(options.StartInstance)) { string instanceId = options.InstanceId; OrchestrationInstance instance = null; switch (options.StartInstance) { case "Greetings": instance = taskHubClient.CreateOrchestrationInstance(typeof(GreetingsOrchestration), instanceId, null); break; case "Greetings2": if (options.Parameters == null || options.Parameters.Length != 1) { throw new ArgumentException("parameters"); } instance = taskHubClient.CreateOrchestrationInstance(typeof(GreetingsOrchestration2), instanceId, int.Parse(options.Parameters[0])); break; case "Cron": // Sample Input: "0 12 * */2 Mon" instance = taskHubClient.CreateOrchestrationInstance(typeof(CronOrchestration), instanceId, (options.Parameters != null && options.Parameters.Length > 0) ? options.Parameters[0] : null); break; case "Average": // Sample Input: "1 50 10" if (options.Parameters == null || options.Parameters.Length != 3) { throw new ArgumentException("parameters"); } int[] input = options.Parameters.Select(p => int.Parse(p)).ToArray(); instance = taskHubClient.CreateOrchestrationInstance(typeof(AverageCalculatorOrchestration), instanceId, input); break; case "ErrorHandling": instance = taskHubClient.CreateOrchestrationInstance(typeof(ErrorHandlingOrchestration), instanceId, null); break; case "Signal": instance = taskHubClient.CreateOrchestrationInstance(typeof(SignalOrchestration), instanceId, null); break; case "Replat": instance = taskHubClient.CreateOrchestrationInstance(typeof(MigrateOrchestration), instanceId, new MigrateOrchestrationData() { SubscriptionId = "03a1cd39-47ac-4a57-9ff5-a2c2a2a76088", IsDisabled = false }); break; default: throw new Exception("Unsupported Orchestration Name: " + options.StartInstance); } Console.WriteLine("Workflow Instance Started: " + instance); } else if (!string.IsNullOrWhiteSpace(options.Signal)) { if (string.IsNullOrWhiteSpace(options.InstanceId)) { throw new ArgumentException("instantceId"); } if (options.Parameters == null || options.Parameters.Length != 1) { throw new ArgumentException("parameters"); } string instanceId = options.InstanceId; OrchestrationInstance instance = new OrchestrationInstance { InstanceId = instanceId }; taskHubClient.RaiseEvent(instance, options.Signal, options.Parameters[0]); } if (!options.SkipWorker) { try { taskHub.AddTaskOrchestrations(typeof(GreetingsOrchestration), typeof(GreetingsOrchestration2), typeof(CronOrchestration), typeof (AverageCalculatorOrchestration), typeof (ErrorHandlingOrchestration), typeof (SignalOrchestration)); taskHub.AddTaskOrchestrations(typeof(MigrateOrchestration)); taskHub.AddTaskActivities(new GetUserTask(), new SendGreetingTask(), new CronTask(), new ComputeSumTask(), new GoodTask(), new BadTask(), new CleanupTask(), new EmailTask()); taskHub.AddTaskActivitiesFromInterface<IManagementSqlOrchestrationTasks>(new ManagementSqlOrchestrationTasks()); taskHub.AddTaskActivitiesFromInterface<IMigrationTasks>(new MigrationTasks()); taskHub.Start(); Console.WriteLine("Press any key to quit."); Console.ReadLine(); taskHub.Stop(true); } catch (Exception) { // silently eat any unhadled exceptions. } } } }