Friday 9 August 2013

Windows service for scheduling Forefront Identity Manager

All FIM projects need to be able to schedule management agent runs automatically. This blogpost describes how we implemented a windows service to schedule Forefront Identity Manager synchronization service. You can find the source code and an installer at FIM-Scheduler.

Basic implementation

We started of by implementing the management agent run functionality in C# by calling the WMI interface using the System.Management library.


ManagementScope ms = new ManagementScope(wmiNs);
string query = "Select * from MIIS_ManagementAgent";
SelectQuery sq = new SelectQuery(query);
using (ManagementObjectSearcher mos 
               = new ManagementObjectSearcher(ms, sq)) {
  foreach (ManagementObject obj in mos.Get()){
    using (ManagementObject ma = obj){
      object[] param = new object[] {runProfile};
      ma.InvokeMethod("Execute",param);
    }
  }
}

So far so good, but we do not want to recompile our code every time we want to change our schedule. Therefore, we put our scheduling configuration in an xml file and let the program read our xml configuration. We defined two kind of objects: runConfiguration and sequence. Both objects contain one or more step objects. There is no difference in the definition of runConfiguration and sequence, but only runConfigurations can be used as the starting point of a schedule. A step can be one of three types:
  1. a linear sequence: all steps are executed one after the other
  2. a parallel sequence: all steps are executed in parallel
  3. a management agent: the run profile defined in the Action attribute is run for the management agent
As you would expect, you can define sequences of sequences of sequences... And of course, you can use a sequence in more than one runConfiguration and/or other sequence.

We gave every type of step a different implementation, so the result has the following structure:

Windows service

It is very easy to create a windows service with Visual Studio since it is a standart project template called, surprisingly, Windows Service.
All we needed to do was add an installer so that the executable can be installed as a service using sc.exe or installUtil.exe. There are some very good tutorials on how to create a windows service, so I'll just give you the link I used: Creating a Windows Service Application.

Scheduling

At the moment we have a program that is capable of running a given schedule and a windows service. The last issue we need to resolve is how we are going to perform the actual scheduling. Some googling gives you some options, and we chose to use the Quartz .NET library.
Quartz.NET is a full-featured, open source job scheduling system that can be used from smallest apps to large scale enterprise systems. Quartz.NET is a pure .NET library written in C# and is a port of very propular open source Java job scheduling framework, Quartz . This project owes very much to original Java project, it's father James House and the project contributors.
After some trial and error we succeeded in configuring a Quartz scheduler that read his configuration from a file and started a job. Then it was just a matter of implementing this job so that it would start the steps in the runconfiguration specified by the Quartz configuration file.

Resources

Creating a Windows Service Application
Quartz .NET
FIM-Scheduler