Friday, 16 April 2010

Programmatically clear down K2 process information?

In K2, your development environment can become very cluttered, very quickly. I searched for a tool to be able to clear down all of the process instance data for processes that were still running and the archive/log data for processes that have completed to give me a clear, pristine and virginal K2 workspace ready to muck up again with more work in progress processes :)

That tool didn’t exist, so I wrote my own (and it was surprisingly simple!). Here are the key components.

First of all, to clear all currently running, active or errored process instances;

  1. WorkflowManagementServer server = new WorkflowManagementServer();
  2. try
  3. {
  4.     K2Connection.CreateConnection(server);
  5.  
  6.     ProcessInstanceCriteriaFilter filter = new ProcessInstanceCriteriaFilter();
  7.  
  8.     foreach (ProcessInstance instance in server.GetProcessInstancesAll(filter))
  9.         server.DeleteProcessInstances(instance.ID, true);
  10. }
  11. catch (Exception ex)
  12. {
  13.     Program.Error(ex);
  14. }
  15. finally
  16. {
  17.     server.Connection.Close();
  18. }

And secondly, the log data – this comes from a separate database which needs to be archived out.

  1. // Create archive temp db
  2. try
  3. {
  4.     CreateSqlTempDb();
  5. }
  6. catch (Exception)
  7. {
  8.     // ... code elided for clarity ...
  9.     return;
  10. }
  11.  
  12. WorkflowManagementServer server = new WorkflowManagementServer();
  13. try
  14. {
  15.     K2Connection.CreateConnection(server);
  16.     server.Archive(ArchiveConnectionString, "K2ServerLog", "_Archive", DateTime.Now.AddMonths(-24), DateTime.Now);
  17. }
  18. catch (Exception)
  19. {
  20.     // ... code elided for clarity ...
  21. }
  22. finally
  23. {
  24.     server.Connection.Close();
  25. }
  26.  
  27. try
  28. {
  29.     DropSqlTempDb();
  30. }
  31. catch (Exception ex)
  32. {
  33.     // ... code elided for clarity ...
  34. }

CreateSqlTempDb and DropSqlTempDb simply create and drop an empty database in SQL, which the archive tool then moves the data to. The ArchiveConnectionString is a standard connection string to the archive db you create.

Obviously you don’t want to be running this on ANY production environments!!!!

No comments:

Post a comment