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;
- WorkflowManagementServer server = new WorkflowManagementServer();
- try
- {
- K2Connection.CreateConnection(server);
- ProcessInstanceCriteriaFilter filter = new ProcessInstanceCriteriaFilter();
- foreach (ProcessInstance instance in server.GetProcessInstancesAll(filter))
- server.DeleteProcessInstances(instance.ID, true);
- }
- catch (Exception ex)
- {
- Program.Error(ex);
- }
- finally
- {
- server.Connection.Close();
- }
And secondly, the log data – this comes from a separate database which needs to be archived out.
- // Create archive temp db
- try
- {
- CreateSqlTempDb();
- }
- catch (Exception)
- {
- // ... code elided for clarity ...
- return;
- }
- WorkflowManagementServer server = new WorkflowManagementServer();
- try
- {
- K2Connection.CreateConnection(server);
- server.Archive(ArchiveConnectionString, "K2ServerLog", "_Archive", DateTime.Now.AddMonths(-24), DateTime.Now);
- }
- catch (Exception)
- {
- // ... code elided for clarity ...
- }
- finally
- {
- server.Connection.Close();
- }
- try
- {
- DropSqlTempDb();
- }
- catch (Exception ex)
- {
- // ... code elided for clarity ...
- }
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