Schedule Regular Jobs

Some applications involve solving a mathematical program regularly, for instance every night or every ten minutes. In this article, we’ll discuss how an AIMMS job can reschedule itself, as depicted in the next picture.

../../_images/DelegationLevel.png

Effectively, this realizes that the job at hand is solved regularly. As you can see from this image:

  • The WebUI session you start via AIMMS PRO has an associated AIMMS data session. This data session has a pro::CurrentDelegationLevel() of 0; there is no delegation of work yet.

  • The AIMMS solver session started from this AIMMS Data Session has a pro::CurrentDelegationLevel() of 1; it is the consequence of one delegation.

  • The AIMMS solver session started from the above AIMMS solver session has a pro::CurrentDelegationLevel() of 1 higher than its predecessor.

To start this sequence, the following code is used.

1Procedure pr_OnButtonStartServerSessions {
2    Body: {
3        ep_PayloadProcedure := StringToElement( AllProcedures, "pr_Friesian", create: 0 );
4        pr_IterativeJobScheduling(
5            maxDelegateLevel   :  4,
6            timeIncrement      :  2[second],
7            epPayloadProcedure :  ep_PayloadProcedure);
8    }
9}

Each line is explained as follows:

  1. Determine the payload procedure that should be executed by each solver session. Here it is the pr_Friesian, as Friesian horses are excellent workhorses.

  2. A call to the procedure that actually starts each solver session.

  3. The number of server sessions started is controlled by maxDelegateLevel

  4. The argument timeIncrement, is the time between sessions measured in seconds.

  5. The procedure to be executed regularly passed via the arguments.

The center piece of the project is the procedure pr_IterativeJobScheduling:

 1Procedure pr_IterativeJobScheduling {
 2    Arguments: (maxDelegateLevel,timeIncrement,epPayloadProcedure);
 3    Body: {
 4        if pro::CurrentDelegationLevel() < maxDelegateLevel then
 5            if pro::DelegateToServer(
 6                    requestDescription :  formatString("The %i'th iteration of %e",  pro::CurrentDelegationLevel()+1, epPayloadProcedure),
 7                    waitForCompletion  :  0,
 8                    completionCallback :  'pro::session::EmptyCallback',
 9                    delegationOverride :  pro::CurrentDelegationLevel() + 1,
10                    scheduledAt        :  if pro::CurrentDelegationLevel() then MomentToString( sp_LocalTimFmt, [second], CurrentToString(sp_LocalTimFmt), timeIncrement ) else "" endif
11                ) then
12                return 1 ;
13            endif ;
14        endif ;
15
16        Apply( epPayloadProcedure );
17    }
18    Parameter maxDelegateLevel {
19        Property: Input;
20    }
21    Parameter timeIncrement {
22        Unit: second;
23        Property: Input;
24    }
25    ElementParameter epPayloadProcedure {
26        Range: AllProcedures;
27        Default: 'MainExecution';
28        Property: Input;
29    }
30}

Each portion of the procedure code is explained below:

  1. Line 4: Limit the number of recurring jobs. When your application does not have a fixed number of jobs, you can remove this line and the argument maxDelegateLevel.

  2. Line 6: requestDescription: to properly identify each job in job overviews and in the session logs.

  3. Line 9: delegationOverride: necessary to submit jobs from within server sessions.

  4. Line 10: scheduleAt: Construct the next time the job is to be executed. By using the Local timezone, ambiguities regarding daylight saving time are avoided. Here sp_LocalTimFmt = "%c%y-%m-%d %H:%M:%S%TZ('Local')".

  5. Line 16: This will execute the payload for each of the server sessions started. The APPLY operator is used here.

To operate, the example that can be downloaded here.

  1. Create an .aimmspack, publish on your favorite AIMMS PRO system.

  2. Launch it and press the only button

  3. Close the app. Yes, once the sequence of server sessions is started, the WebUI of the enclosed example is no longer of use - it can be closed.

  4. Go to job tab in the AIMMS PRO portal and watch new jobs being created, queued, running, and finished.

../../_images/PROJobs.png

Note

  • When you check the session.log files, you may encounter a line like:

    12:10:46,186 0x7f6389d90700 [INFO] {PRO.Client.Library} pr_Friesian(): At 2018-09-04 12:10:46 (UTC) delegation level is 3
    

    That is because the procedure pr_Friesian uses the procedure call pro::management::LocalLogInfo(...); to log some information about current solver session.

  • When you want to interrupt a sequence of server jobs, please terminate the scheduled session before terminating the running session.