Working with class jobs

How to create a new Class JOB

In this section we are going to create a simple beeper JOB, then we’ll add a JSON-RPC interface and will test it with a python script.

Creating the JOB class

  • Create a new Delphi Package

New Delphi Package

  • Using the dcp files available in the <DMS_HOME>\SDK folder, add the following DCPs among the Requires packages section:
    • dmsRT
    • dmvcframeworkRT.dcp
    • loggerproRT.dcp
    • LockBoxDR
  • Open the project options and set the Package Output Directory to point to the folder jobspackage of your DMSContainer instance.

Project Options

  • Save the package as dms_job_beeper.dproj add a new unit called Jobs.Beeper.Job.pas.

Write the following code into the unit Jobs.Beeper.Job.pas

unit Jobs.Beeper.Job;

interface

uses DMSCommonsU, DMSCustomJob, DMSLoggingU;

type
    TBeepJob = class(TCustomJob)
    protected
        procedure DoExecute; override;
    end;

implementation

uses
    WinApi.Windows, System.SysUtils;

{ TBeepJob }

procedure TBeepJob.DoExecute;
begin
    Log.Info('Hello! I''m going to play something amazing!!', JobName);
    WinApi.Windows.Beep(440, 80);
end;

initialization

Randomize;
    TBeepJob.ClassName;

end.
  • Build the package and check that the file dms_job_beeper.bpl is present into the folder <DMS_HOME>\jobspackage

NOTE: At the startup DMSContainer loads all the packages inside the folder <DMS_HOME>\jobspackage whom name starts with dms_job_. Using a different name, the package will not be loaded.

  • Open the <DMS_HOME>\conf\jobsmanager.json file and add a new JSON object, as the last of the jobs array, to configure the new beeper job.

Now, your jobsmanager.json file should looks like the following

{
"jobs":[
    {
        "enabled": true,
        "jobname": "email",
        "schedule": "* * * * * *", 
        "jobtype":"class", 
        "jobclass":"Jobs.Email.Job.TJobEmail",
        "rpcclass":"Jobs.Email.RPC.TEmailRPC",
        "rpcuri":"/emailrpc"
    },
# other jobs here...	
    {
        "enabled": true,
        "jobname": "beeper",
        "schedule": "* * * * * *", 
        "jobtype":"class", 
        "jobclass":"Jobs.Beeper.Job.TBeepJob"
    }
]
}
  • Save the file and re/start your DMSContainer instance using the RUN_DEBUG.BAT batch file.
  • If the instance is running as console, you should ear a beep every minute. Moreover in the logs folder your should see a new log file named dms_00.beeper.log
  • Congratulations! Your first DMSContainer job is running!

If DMSContainer doesn’t start, check the main log file names dms_00.main.log

However, working without a debugger is not a viable solution nowadays, so let’s configure the Delphi IDE to help in the development of DMSContainer Jobs.

Configuring Delphi debugger

If while working with small package a debugger can be a nice addition but not a requirement, if the packages you are working on gets bigger, you definitively need a debugger. The Delphi debugger is one of the best on the market, so using DMSContainer you can use it and enjoy a pleasant development experience.

Follow these steps to instruct DMSContainer and Delphi to work nicely together.

  • Stop the DMSContainer service if running.
  • Open the Delphi IDE and open the dms_job_beeper.dproj project in the IDE.
  • Go to menu Project->Options and in the opening window, select the node Debugger.
  • In the field Host Application write the full path of the DMSContainerService.exe executable (default is <DMS_HOME>\DMSContainerService.exe).
  • In the field Parameters write /console.
  • Now, select the node Debugger->Environment Block and add the following variable in the box “User System Override”:
    • Variable DMS_HOME with value C:\bitTimeProfessionals\DMSContainer_3_1_0 (or the folder where you decided to install DMSContainer).
  • Confirm your changes hitting “OK”
  • Run your project.
  • In this case, DMSContainer runs in debug mode (because of the switch `/console) so a console is showed and you can easily put your breakpoints in the BPL code as usual.
  • When you want to release the BPL containing your ClassJob, just run the DMSContainerService as usual from the Windows Service Console.

WARNING: If for some reason you want to run DMSContainer as console also outside the Delphi IDE, you can still use the /console switch opening a command prompt and issuing the following commands (remember to stop the DMSContainerService.exe from the Windows Services).

set DMS_HOME=C:\bitTimeProfessionals\DMSContainer_3_1_0
DMSContainerService.exe /console

If it works for you, you can also use the RUN_DEBUG.BAT file to run DMSContainer as console application in debug mode.

Adding a JSON-RPC interface to the beeper job

In this section we’ll add a JSON-RPC interface to the beeper job.

  • Stop the DMSContainer instance
  • Open the project dms_job_beeper.dproj (or create a new package following the instructions of the previous paragraphs)

Add a new unit called Jobs.Calculator.RPC.pas. The following code is a a simple, but working, RPC class which can be remotely accessed through the JSON-RPC interface

unit Jobs.Calculator.RPC;

interface

uses
    DMSCustomRPC;

type
    TCalculatorRPC = class(TCustomRPC)
    public
        function Sum(const A, B: Integer): Integer;
    end;

implementation

{ TCalculatorRPC }

function TCalculatorRPC.Sum(const A, B: Integer): Integer;
begin
    Result := A + B;
end;

end.

  • Build the package and check that the file dms_job_beeper.bpl is present into the folder <DMS_HOME>\jobpackages
  • Open the jobsmanager.json file and add a new JSON object to configure our new job.

Your jobsmanager.json file should looks like the following

{
"jobs":[
    {
        "enabled": true,
        "jobname": "email",
        "schedule": "* * * * * *", 
        "jobtype":"class", 
        "jobclass":"Jobs.Email.Job.TJobEmail",
        "rpcclass":"Jobs.Email.RPC.TEmailRPC",
        "rpcuri":"/emailrpc"
    },
    {
        "enabled": true,
        "jobname": "beeper",
        "schedule": "* * * * * *", 
        "jobtype":"class", 
        "jobclass":"Jobs.Beeper.Job.TBeepJob",
        "rpcclass":"Jobs.Calculator.RPC.TCalculatorRPC",
        "rpcuri":"/beeperrpc"
    }
]
}
  • Re/Start your DMSContainer instance.
  • Congratulations! Your first DMSContainer job with an RPC interface is running!

Now you first Class Job is up and running.. it’s time to test it!