asynntrp.gms : Start GAMS job asynchronously and send interrupt signal to it

Description

'$Call.ASyncNC' starts a job in a new, minimized console. With 'JobHandle' one
gets the process ID of that job and the put_utility 'WinMsg' sends an interrupt
signal to that PID. On Unix we get the list of PIDs using the utility pstree and
send a interrupt signal to these processes.

Contributor: Lutz Westermann


Small Model of Type : GAMS


Category : GAMS Test library


Main file : asynntrp.gms

$Title  Start GAMS job asynchronously and send interrupt signal to it (ASYNNTRP,SEQ=585)

$ontext
'$Call.ASyncNC' starts a job in a new, minimized console. With 'JobHandle' one
gets the process ID of that job and the put_utility 'WinMsg' sends an interrupt
signal to that PID. On Unix we get the list of PIDs using the utility pstree and
send a interrupt signal to these processes.

Contributor: Lutz Westermann
$offtext

$ifthen %system.filesys% == UNIX
$call which pstree > /dev/null 2>&1
$if errorlevel 1 $abort.noerror Under Unix we require the pstree utility
$set sedcmd
$set pstree pstree -p
$if LEX==%system.platform% $set sedcmd sed 's/(/\n(/g' | grep '(' | sed 's/(\(.*\)).*/\1/'
$ifthen.DEX DEX==%system.platform%
$  set sedcmd sed 's/^.*- /(/g' | grep '^(' | cut -f 1 -d ' ' | sed 's/(//g'
$  set pstree pstree pid
$endif.DEX
$ifthen.DAX DAX==%system.platform%
$  set sedcmd sed 's/^.*- /(/g' | grep '^(' | cut -f 1 -d ' ' | sed 's/(//g'
$  set pstree pstree pid
$endif.DAX
$if "x%sedcmd%"==x
$abort.noerror pstree output requires stream editing. Please specify command to extract a list of pids
$endif

$call gamslib -q dicex
$onecho > dicexx.gms
$include dicex.gms
scalar sstat; sstat = diceU.solvestat;
$offecho

$Call.ASyncNC =gams dicexx mip=cplex gdx=diceout lo=2 threads=1
$eval jh JobHandle

display$sleep(0.5) 'Wait for cplex to get started';

file fx;
while(JobStatus(%jh%)=1,
  $$ifthen %system.filesys% == UNIX
  put_utility fx 'shell' / "%pstree% %jh% | %sedcmd% | xargs kill -2 > /dev/null 2>&1"
  $$else
  put_utility fx 'winmsg' / '___GAMSMSGWINDOW___%jh%' / "GAMS Message Interrupt";
  $$endif
  if(JobStatus(%jh%)=1, display$sleep(0.5) 'Wait before next try';);
);

display$sleep(1) 'Wait a sec';
execute 'test -f diceout.gdx || sleep 10'

scalar sstat; execute_load 'diceout' sstat;
abort$(sstat<>%Solvestat.User Interrupt%) 'Wrong status code', sstat;