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