anamika das
anamika das

Reputation: 21

Loading any persistent workflow containing delay activity when it is a runnable instance in the store

We are trying to load and resume workflows which have a delay. I have seen the Microsoft sample of Absolute Delay for this using store.WaitForEvents and LoadRunnableInstance to load the workflow. However here the workflow is already known.

In our case we want to have an event waiting for the store.WaitForEvents after every say 5 seconds to check if there is a runnable instance and if so only load and run that /those particular instances. Is there a way I could know which workflow instance is ready.

We are maintaing the workflow id and the xaml associated to it in our database, so if we could know the workflow instance id we could get the xaml mapped to it, create the workflow and then do a LOadRunnableInstance on it.

Any help would be greatly appreciated.

Microsoft sample (Absolute Delay)

public void Run(){
    wfHostTypeName = XName.Get("Version" + Guid.NewGuid().ToString(), 
                               typeof(WorkflowWithDelay).FullName);

    this.instanceStore = SetupSqlpersistenceStore();

    this.instanceHandle = 
        CreateInstanceStoreOwnerHandle(instanceStore, wfHostTypeName);
    WorkflowApplication wfApp = CreateWorkflowApp();
    wfApp.Run();

    while (true) 
    {
        this.waitHandler.WaitOne();
        if (completed) 
        {
            break;
        }

        WaitForRunnableInstance(this.instanceHandle);
        wfApp = CreateWorkflowApp();

        try 
        {
            wfApp.LoadRunnableInstance();
            waitHandler.Reset();
            wfApp.Run();
        } 
        catch (InstanceNotReadyException) 
        {
            Console.WriteLine("Handled expected InstanceNotReadyException, retrying...");
        }
    }

    Console.WriteLine("workflow completed.");
}

public void WaitForRunnableInstance(InstanceHandle handle) 
{
    var events=instanceStore.WaitForEvents(handle, TimeSpan.MaxValue);
    bool foundRunnable = false;
    foreach (var persistenceEvent in events) 
    {
        if (persistenceEvent.Equals(HasRunnableWorkflowEvent.Value)) 
        {
            foundRunnable = true;
            break;
        }
    }

    if (!foundRunnable) {
        Console.WriteLine("no runnable instance");
    }
}

Thanks

Anamika

Upvotes: 2

Views: 1587

Answers (2)

Marcus
Marcus

Reputation: 111

I had a similar problem with durable delay activities and WorkflowApplicationHost. Ended up creating my own 'Delay' activity that worked essentially the same way as the one out of the box, (takes an arg that describes when to resume the workflow, and then bookmarks itself). Instead of saving delay info in the SqlInstanceStore though, my Delay Activity created a record in a seperate db. (similar to the one you are using to track the Workflow Ids and Xaml). I then wrote a simple service that polled that DB for expired delays and initiated a resume of the necessary workflow.

Oh, and the Delay activity deleted it's record from that DB on bookmark resume.

HTH

Upvotes: 2

Joe Clancy
Joe Clancy

Reputation: 1437

I'd suggest having a separate SqlPersistenceStore for each workflow definition you're hosting.

Upvotes: 0

Related Questions