Number of running and pending solver sessions

When starting a new server session it is useful to know whether there are other server sessions started on the same server. This article explains how to find out the number of sessions running at an AIMMS PRO server.

Scope

We will discuss two variations on the number of server sessions:

  1. The number of AIMMS PRO sessions actually running.
  2. The number of AIMMS PRO active sessions, i.e., sessions not yet finished.

Obtain number of sessions running

To obtain the number of server session actually running, you can use pro::sessionmanager::ListSessionSinceDate.

We use the following code, where p_NoRun contains the number of server sessions actually running.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
p_ret := pro::Initialize();
if not (p_ret) then return 0; endif;

if pro::GetPROEndPoint() then
    sp_SinceDate :=  fnc_CurrentToStringDelta(-24*60*60); ! Assuming jobs older than a day are no longer interesting.

    p_AllUsers := 1 ;
    if (p_AllUsers) then
        sp_SessionModelId := "";
        sp_SessionModelVersion := "";
    else
        sp_SessionModelId := pro::ModelName; ! Retrieve sessions for all versions of the model
        sp_SessionModelVersion := ""; ! don't restrict to a specific ModelVersion
    endif;

    ! Load raw session list from PROT
    p_ret := pro::sessionmanager::ListSessionSinceDate(
        dateStr           :  sp_SinceDate,
        SessionList       :  S_SessionList,
        ClientQueue       :  sp_ClientQueue,
        WorkerQueue       :  sp_WorkerQueue,
        CurrentStatus     :  p_CurrentStatus,
        CreateTime        :  sp_CreateTime,
        userEnvironment   :  sp_UserEnv,
        UserName          :  sp_UserName,
        proj              :  sp_Application,
        clientRef         :  sp_OriginalCasePath,
        descr             :  sp_RequestDescription,
        proc              :  sp_RequestProcedure,
        timeOut           :  p_RunTimeOut,
        inputDataVersion  :  sp_VersionID,
        outputDataVersion :  sp_ResponseVersionID,
        logFileVersion    :  sp_MessageLogVersionID,
        ErrorMessage      :  sp_ErrorMessage,
        modelStatus       :  p_ActiveStatus,
        ErrorCode         :  p_ErrorCode,
        AllUsers          :  p_AllUsers,
        projectID         :  sp_SessionModelId,
        projectVersion    :  sp_SessionModelVersion);
    if not (p_ret) then return 0; endif;
    p_NoCreated      := count( i_sess | p_CurrentStatus(i_sess) = pro::PROTS_CREATED      );
    p_NoQueued       := count( i_sess | p_CurrentStatus(i_sess) = pro::PROTS_QUEUED       );
    p_NoInitializing := count( i_sess | p_CurrentStatus(i_sess) = pro::PROTS_INITIALIZING );
    p_NoReadyToRun   := count( i_sess | p_CurrentStatus(i_sess) = pro::PROTS_READY        );
    p_NoRun          := count( i_sess | p_CurrentStatus(i_sess) = pro::PROTS_RUNNING      );
else
    p_ret := 0 ;
endif;

return p_ret ;

Obtaining number of active sessions

Next, to find the active sessions we’ll aggregate number of unfinished sessions.

Based on the above, aggregating the number of unfinished sessions is simple:

1
2
3
4
5
6
p_DerivedActiveJobs(ep_obs) :=
    p_ObservedCreatedJobs(      ep_obs ) +
    p_ObservedQueuedJobs(       ep_obs ) +
    p_ObservedInitializingJobs( ep_obs ) +
    p_ObservedReadyToRunJobs(   ep_obs ) +
    p_ObservedRunningJobs(      ep_obs ) ;

Example projects

You can download two example apps to try out the code yourself:

To experiment with these apps, you should download and publish them both on AIMMS PRO. Start both apps, and press the start button of both apps.

You’ll then see how CountRunningJobs monitors multiple jobs:

../../_images/monitoring.png

You can copy the procedure pr_CountRunningJobs (in the app CountRunningJobs) to determine the number of running jobs or the number of active jobs in your own application.

Important

It is possible that two users at the same time query the number of running jobs, which return 0, and then submit a job. In this case, there may still be one job waiting for the other.

To detect such cases, the number of active jobs is one more than the number allowed to run in parallel.

To detect whether the waiting job is your job, you can test for pro::session::CurrentSessionStatus.

You can cancel the waiting job as explained in Interrupt the Server Session.

Last Updated: November, 2019