[3175] | 1 | function flag=waitonlock(md,executionpath,login,port)
|
---|
[1] | 2 | %WAITONLOCK - wait for a file
|
---|
| 3 | %
|
---|
| 4 | % This routine will return when a file named 'filename' is written to disk.
|
---|
[2226] | 5 | % If the time limit given in input is exceeded, return 0
|
---|
[1] | 6 | %
|
---|
| 7 | % Usage:
|
---|
[2555] | 8 | % flag=waitonlock(md,executionpath)
|
---|
[1] | 9 |
|
---|
[2555] | 10 | %Get filename (lock file) and options
|
---|
[5954] | 11 | executionpath=md.cluster.executionpath;
|
---|
| 12 | cluster=md.cluster.name;
|
---|
| 13 | login=md.cluster.login;
|
---|
| 14 | port=md.cluster.port;
|
---|
| 15 | timelimit=md.waitonlock;
|
---|
[5546] | 16 | filename=[executionpath '/' md.runtimename '/' md.name '.lock'];
|
---|
[2226] | 17 |
|
---|
[2555] | 18 | %waitonlock will work if the lock is on the same machine only:
|
---|
| 19 | if ~strcmpi(oshostname(),cluster),
|
---|
[3175] | 20 |
|
---|
| 21 | if port,
|
---|
| 22 | %there is a tunnel, so we have a short at looking for the lock file.
|
---|
| 23 |
|
---|
| 24 | disp(['waiting for ' filename ' hold on... (Ctrl+C to exit)'])
|
---|
| 25 | time=0; ispresent=0;
|
---|
| 26 | while (ispresent==0 & time<timelimit)
|
---|
[5546] | 27 | [status, result]=system(['ssh -q -p ' num2str(port) ' ' login '@localhost "if ( -e ' executionpath '/' md.runtimename '/' md.name '.lock ) echo 1"']);
|
---|
[5299] | 28 | if ~isempty(result),
|
---|
| 29 | if ismember('1',result),
|
---|
| 30 | ispresent=1;
|
---|
| 31 | else
|
---|
| 32 | ispresent=0;
|
---|
| 33 | end
|
---|
| 34 | else
|
---|
| 35 | ispresent=0;
|
---|
| 36 | end
|
---|
[3175] | 37 | pause(10); %tunnel can be unstable, let's not use it too much
|
---|
| 38 | time=time+1/60;
|
---|
| 39 | end
|
---|
[5264] | 40 | disp(['waitonlock: lock detected, with value of result:|' result '|']);
|
---|
[3175] | 41 |
|
---|
| 42 | %build output
|
---|
| 43 | if (time>timelimit),
|
---|
| 44 | disp('Time limit exceeded. Increase md.waitonlock');
|
---|
| 45 | disp('The results must be loaded manually with md=loadresultsfromcluster(md).');
|
---|
| 46 | error(['waitonlock error message: time limit exceeded']);
|
---|
| 47 | flag=0;
|
---|
| 48 | else
|
---|
| 49 | flag=1;
|
---|
| 50 | end
|
---|
[2555] | 51 | else
|
---|
[3175] | 52 | disp('solution launched on remote cluster. log in to detect job completion.');
|
---|
| 53 | choice=input('Is the job successfully completed? (y/n)','s');
|
---|
| 54 | if ~strcmp(choice,'y'),
|
---|
| 55 | disp('Results not loaded... exiting');
|
---|
| 56 | flag=0;
|
---|
| 57 | else
|
---|
| 58 | flag=1;
|
---|
| 59 | end
|
---|
[2555] | 60 | end
|
---|
[2226] | 61 |
|
---|
[2555] | 62 | %job is running on the same machine
|
---|
[2226] | 63 | else
|
---|
[2555] | 64 |
|
---|
| 65 | %initialize time and file presence test flag
|
---|
| 66 | time=0; ispresent=0;
|
---|
| 67 | disp(['waiting for ' filename ' hold on... (Ctrl+C to exit)'])
|
---|
| 68 |
|
---|
| 69 | %loop till file .lock exist or time is up
|
---|
| 70 | while (ispresent==0 & time<timelimit)
|
---|
| 71 | ispresent=exist(filename,'file');
|
---|
| 72 | pause(1);
|
---|
| 73 | time=time+1/60;
|
---|
| 74 | end
|
---|
| 75 |
|
---|
| 76 | %build output
|
---|
| 77 | if (time>timelimit),
|
---|
[2656] | 78 | disp('Time limit exceeded. Increase md.waitonlock');
|
---|
| 79 | disp('The results must be loaded manually with md=loadresultsfromcluster(md).');
|
---|
| 80 | error(['waitonlock error message: time limit exceeded']);
|
---|
[2555] | 81 | flag=0;
|
---|
| 82 | else
|
---|
| 83 | flag=1;
|
---|
| 84 | end
|
---|
[2226] | 85 | end
|
---|