-
Notifications
You must be signed in to change notification settings - Fork 2
/
gridctl.php
1231 lines (1014 loc) · 33.7 KB
/
gridctl.php
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
$us3bin = exec( "ls -d ~us3/lims/bin" );
include_once "$us3bin/listen-config.php";
//include "$us3bin/cleanup_aira.php";
//include "$us3bin/cleanup_gfac.php";
//include "$us3bin/cleanup.php";
// Global variables
$gfac_message = "";
$updateTime = 0;
$submittime = 0;
$cluster = '';
//global $self;
global $status_ex, $status_gw;
// Produce some output temporarily, so cron will send me message
$now = time();
echo "Time started: " . date( 'Y-m-d H:i:s', $now ) . "\n";
write_log( "start of gridctl.php" );
// Get data from global GFAC DB
$gLink = mysqli_connect( $dbhost, $guser, $gpasswd, $gDB );
if ( ! $gLink )
{
write_log( "$self: Could not select DB $gDB - " . mysqli_error() );
mail_to_admin( "fail",
"Internal Error: Could not select DB $gDB $dbhost $guser Server: $servhost" );
sleep(3);
exit();
}
$query = "SELECT gfacID, us3_db, cluster, status, queue_msg, " .
"UNIX_TIMESTAMP(time), time, autoflowAnalysisID from analysis";
$result = mysqli_query( $gLink, $query );
if ( ! $result )
{
write_log( "$self: Query failed $query - " . mysqli_error( $gLink ) );
mail_to_admin( "fail", "Query failed $query\n" . mysqli_error( $gLink ) );
exit();
}
if ( mysqli_num_rows( $result ) == 0 )
{
//write_log( "$self: analysis read got numrows==0" );
exit(); // Nothing to do
}
//write_log( "$loghdr gfac-analysis rows $nrows" );
$me_devel = preg_match( "/class_devel/", $class_dir );
//echo "me_devel=$me_devel class_dir=$class_dir\n";
while ( list( $gfacID, $us3_db, $cluster, $status, $queue_msg, $time, $updateTime, $autoflowID )
= mysqli_fetch_array( $result ) )
{
// If this entry does not match class/class_devel, skip processing
//echo " gfacID=$gfacID gf_status=$status\n";
write_log( "$self: gfacID=$gfacID gf_status=$status autoflowID=$autoflowID" );
if ( preg_match( "/US3-A/i", $gfacID ) )
{ // For thrift, job and gridctl must match
$job_devel = preg_match( "/US3-ADEV/i", $gfacID );
//echo " THR: job_devel=$job_devel\n";
if ( ( $me_devel && !$job_devel ) ||
( !$me_devel && $job_devel ) )
{ // Job type and Airavata server mismatch: skip processing
continue;
}
}
else if ( $me_devel )
{ // Local (us3iab/-local) and class_devel: skip processing
//echo " LOC: me_devel=$me_devel\n";
continue;
}
// Checking we need to do for each entry
echo "us3db=$us3_db gfid=$gfacID\n";
//write_log( " us3db=$us3_db gfid=$gfacID" );
switch ( $us3_db )
{
case 'Xuslims3_cauma3' :
case 'Xuslims3_cauma3d' :
case 'Xuslims3_HHU' :
case 'Xuslims3_Uni_KN' :
$serviceURL = "http://gridfarm005.ucs.indiana.edu:9090/ogce-rest/job";
break;
default :
// $serviceURL = "http://gridfarm005.ucs.indiana.edu:8080/ogce-rest/job";
break;
}
// $awork = array();
// $awork = explode( "-", $gfacID );
// $gfacLabl = $awork[0] . "-" . $awork[1] . "-" . $awork[2];
$gfacLabl = $gfacID;
$loghdr = $self . ":" . $gfacLabl . "...:";
$status_ex = $status;
// If entry is for Airvata/Thrift, get the true current status
if ( is_aira_job( $gfacID ) )
{
$status_in = $status;
//write_log( "$loghdr status_in=$status_in" );
$status = aira_status( $gfacID, $status_in );
//echo "$loghdr status_in=$status_in status_ex=$status\n";
if($status != $status_in )
write_log( "$loghdr Set to $status from $status_in" );
//write_log( "$loghdr aira status=$status" );
}
else if ( is_gfac_job( $gfacID ) )
{
$status_gw = $status;
$status = get_gfac_status( $gfacID );
//if ( $status == 'FINISHED' )
if ( $status_gw == 'COMPLETE' )
$status = $status_gw;
//echo "$loghdr status_gw=$status_gw status=$status\n";
//write_log( "$loghdr non-AThrift status=$status status_gw=$status_gw" );
}
else
{
//write_log( "$loghdr Local gfacID=$gfacID" );
$status_gw = $status;
$status = get_local_status( $gfacID );
if ( $status_gw == 'COMPLETE' || $status == 'UNKNOWN' )
$status = $status_gw;
echo "$loghdr status_lo=$status\n";
write_log( "$loghdr Local status=$status status_gw=$status_gw" );
}
// Sometimes during testing, the us3_db entry is not set
// If $status == 'ERROR' then the condition has been processed before
if ( strlen( $us3_db ) == 0 && $status != 'ERROR' )
{
write_log( "$loghdr GFAC DB is NULL - $gfacID" );
mail_to_admin( "fail", "GFAC DB is NULL\n$gfacID" );
$query2 = "UPDATE analysis SET status='ERROR' WHERE gfacID='$gfacID'";
$result2 = mysqli_query( $gLink, $query2 );
$status = 'ERROR';
if ( ! $result2 )
write_log( "$loghdr Query failed $query2 - " . mysqli_error( $gLink ) );
update_autoflow_status( 'ERROR', 'GFAC DB is NULL' );
}
//echo " st=$status\n";
write_log( "$loghdr switch status=$status" );
switch ( $status )
{
// Already been handled
// Later update this condition to search for gfacID?
case "ERROR":
cleanup();
break;
case "SUBMITTED":
submitted( $time );
break;
case "SUBMIT_TIMEOUT":
submit_timeout( $time );
break;
case "RUNNING":
case "STARTED":
case "STAGING":
case "ACTIVE":
write_log( "$loghdr RUNNING gfacID=$gfacID" );
running( $time, $queue_msg );
break;
case "RUN_TIMEOUT":
run_timeout($time );
break;
case "DATA":
case "RESULTS_GEN":
wait_data( $time );
break;
case "DATA_TIMEOUT":
data_timeout( $time );
break;
case "COMPLETED":
case "COMPLETE":
write_log( "$loghdr COMPLETE gfacID=$gfacID" );
complete();
break;
case "CANCELLED":
case "CANCELED":
case "FAILED":
failed();
break;
case "FINISHED":
case "DONE":
if ( ! is_aira_job( $gfacID ) )
{
complete();
}
write_log( "$loghdr FINISHED gfacID=$gfacID" );
case "PROCESSING":
default:
break;
}
}
mysqli_close( $gLink );
exit();
function submitted( $updatetime )
{
global $self;
global $gLink;
global $gfacID;
global $autoflowID;
global $loghdr;
$now = time();
if ( $updatetime + 600 > $now ) return; // < 10 minutes ago
if ( $updatetime + 86400 > $now ) // Within the first 24 hours
{
if ( ( $job_status = get_gfac_status( $gfacID ) ) === false )
$job_status = get_local_status( $gfacID );
if ( $job_status == 'GFAC_STATUS_UNAVAILABLE' )
return;
if ( ! in_array( $job_status, array( 'SUBMITTED', 'INITIALIZED', 'PENDING' ) ) )
{
write_log( "$loghdr submitted:job_status=$job_status" );
update_job_status( $job_status, $gfacID );
}
return;
}
$message = "Job listed submitted longer than 24 hours";
write_log( "$self: $message - id: $gfacID" );
mail_to_admin( "hang", "$message - id: $gfacID" );
$query = "UPDATE analysis SET status='SUBMIT_TIMEOUT' WHERE gfacID='$gfacID'";
$result = mysqli_query( $gLink, $query );
if ( ! $result )
write_log( "$self: Query failed $query - " . mysqli_error( $gLink ) );
update_queue_messages( $message );
update_db( $message );
update_autoflow_status( 'SUBMIT_TIMEOUT', $message );
}
function submit_timeout( $updatetime )
{
global $self;
global $gLink;
global $gfacID;
global $autoflowID;
global $loghdr;
if ( ( $job_status = get_gfac_status( $gfacID ) ) === false )
$job_status = get_local_status( $gfacID );
if ( $job_status == 'GFAC_STATUS_UNAVAILABLE' )
return;
if ( ! in_array( $job_status, array( 'SUBMITTED', 'INITIALIZED', 'PENDING' ) ) )
{
update_job_status( $job_status, $gfacID );
return;
}
$now = time();
if ( $updatetime + 86400 > $now ) return; // < 24 hours ago ( 48 total submitted )
$message = "Job listed submitted longer than 48 hours";
write_log( "$self: $message - id: $gfacID" );
mail_to_admin( "hang", "$message - id: $gfacID" );
$query = "UPDATE analysis SET status='FAILED' WHERE gfacID='$gfacID'";
$result = mysqli_query( $gLink, $query );
if ( ! $result )
write_log( "$self: Query failed $query - " . mysqli_error( $gLink ) );
update_queue_messages( $message );
update_db( $message );
update_autoflow_status( 'FAILED', $message );
}
function running( $updatetime, $queue_msg )
{
global $self;
global $gLink;
global $gfacID;
global $autoflowID;
global $loghdr;
$now = time();
get_us3_data();
update_autoflow_status( 'RUNNING', $queue_msg );
if ( $updatetime + 600 > $now ) {
return; // message received < 10 minutes ago
}
if ( $updatetime + 86400 > $now ) // Within the first 24 hours
{
if ( ( $job_status = get_gfac_status( $gfacID ) ) === false )
$job_status = get_local_status( $gfacID );
if ( $job_status == 'GFAC_STATUS_UNAVAILABLE' )
return;
if ( ! in_array( $job_status, array( 'ACTIVE', 'RUNNING', 'STARTED' ) ) ) {
update_job_status( $job_status, $gfacID );
}
return;
}
$message = "Job listed running longer than 24 hours";
write_log( "$self: $message - id: $gfacID" );
mail_to_admin( "hang", "$message - id: $gfacID" );
$query = "UPDATE analysis SET status='RUN_TIMEOUT' WHERE gfacID='$gfacID'";
$result = mysqli_query( $gLink, $query );
if ( ! $result )
write_log( "$self: Query failed $query - " . mysqli_error( $gLink ) );
update_queue_messages( $message );
update_db( $message );
update_autoflow_status( 'RUN_TIMEOUT', $message );
}
function run_timeout( $updatetime )
{
global $self;
global $gLink;
global $gfacID;
global $autoflowID;
global $loghdr;
if ( ( $job_status = get_gfac_status( $gfacID ) ) === false )
$job_status = get_local_status( $gfacID );
if ( $job_status == 'GFAC_STATUS_UNAVAILABLE' )
return;
if ( ! in_array( $job_status, array( 'ACTIVE', 'RUNNING', 'STARTED' ) ) )
{
update_job_status( $job_status, $gfacID );
return;
}
$now = time();
get_us3_data();
if ( $updatetime + 172800 > $now ) return; // < 48 hours ago
$message = "Job listed running longer than 48 hours";
write_log( "$self: $message - id: $gfacID" );
mail_to_admin( "hang", "$message - id: $gfacID" );
$query = "UPDATE analysis SET status='FAILED' WHERE gfacID='$gfacID'";
$result = mysqli_query( $gLink, $query );
if ( ! $result )
write_log( "$self: Query failed $query - " . mysqli_error( $gLink ) );
update_queue_messages( $message );
update_db( $message );
update_autoflow_status( 'FAILED', $message );
}
function wait_data( $updatetime )
{
global $self;
global $gLink;
global $gfacID;
global $autoflowID;
global $loghdr;
$now = time();
if ( $updatetime + 3600 > $now ) // < Within the first hour
{
if ( ( $job_status = get_gfac_status( $gfacID ) ) === false )
$job_status = get_local_status( $gfacID );
if ( $job_status == 'GFAC_STATUS_UNAVAILABLE' )
return;
if ( $job_status != 'DATA' )
{
update_job_status( $job_status, $gfacID );
return;
}
// Request to resend data, but only request every 5 minutes
$minute = date( 'i' ) * 1; // Makes it an int
if ( $minute % 5 ) return;
$output_status = get_gfac_outputs( $gfacID );
if ( $output_status !== false )
mail_to_admin( "debug", "wait_data/$gfacID/$output_status" );
return;
}
$message = "Waiting for data longer than 1 hour";
write_log( "$self: $message - id: $gfacID" );
mail_to_admin( "hang", "$message - id: $gfacID" );
$query = "UPDATE analysis SET status='DATA_TIMEOUT' WHERE gfacID='$gfacID'";
$result = mysqli_query( $gLink, $query );
if ( ! $result )
write_log( "$self: Query failed $query - " . mysqli_error( $gLink ) );
update_queue_messages( $message );
update_db( $message );
update_autoflow_status( 'DATA_TIMEOUT', $message );
}
function data_timeout( $updatetime )
{
global $self;
global $gLink;
global $gfacID;
global $autoflowID;
global $loghdr;
if ( ( $job_status = get_gfac_status( $gfacID ) ) === false )
$job_status = get_local_status( $gfacID );
if ( $job_status == 'GFAC_STATUS_UNAVAILABLE' )
return;
if ( $job_status != 'DATA' )
{
update_job_status( $job_status, $gfacID );
return;
}
$now = time();
if ( $updatetime + 86400 > $now ) // < 24 hours ago
{
// Request to resend data, but only request every 15 minutes
$minute = date( 'i' ) * 1; // Makes it an int
if ( $minute % 15 ) return;
$output_status = get_gfac_outputs( $gfacID );
if ( $output_status !== false )
mail_to_admin( "debug", "data_timeout/$gfacID/$output_status" );
return;
}
$message = "Waiting for data longer than 24 hours";
write_log( "$self: $message - id: $gfacID" );
mail_to_admin( "hang", "$message - id: $gfacID" );
$query = "UPDATE analysis SET status='FAILED' WHERE gfacID='$gfacID'";
$result = mysqli_query( $gLink, $query );
if ( ! $result )
write_log( "$self: Query failed $query - " . mysqli_error( $gLink ) );
update_queue_messages( $message );
update_db( $message );
update_autoflow_status( 'FAILED', $message );
}
function complete()
{
// Just cleanup
cleanup();
}
function failed()
{
// Just cleanup
cleanup();
}
function cleanup()
{
global $self;
global $gLink;
global $gfacID;
global $autoflowID;
global $us3_db;
global $loghdr;
global $class_dir;
// Double check that the gfacID exists
$query = "SELECT count(*) FROM analysis WHERE gfacID='$gfacID'";
$result = mysqli_query( $gLink, $query );
if ( ! $result )
{
write_log( "$self: Query failed $query - " . mysqli_error( $gLink ) );
mail_to_admin( "fail", "Query failed $query\n" . mysqli_error( $gLink ) );
return;
}
list( $count ) = mysqli_fetch_array( $result );
//if ($count==0)
//write_log( "$loghdr count = $count gfacID = $gfacID" );
if ( $count == 0 ) return;
// Now check the us3 instance
$requestID = get_us3_data();
//write_log( "$loghdr requestID = $requestID gfacID = $gfacID" );
if ( $requestID == 0 ) return;
$me_devel = preg_match( "/class_devel/", $class_dir );
$me_local = preg_match( "/class_local/", $class_dir );
if ( preg_match( "/US3-A/i", $gfacID ) )
{ // Airavata job: clean up if prod/devel match
$job_devel = preg_match( "/US3-ADEV/i", $gfacID );
if ( ( !$me_devel && !$job_devel ) ||
( $me_devel && $job_devel ) )
{ // Job is of same type (prod/devel) as Server: process it
//write_log( "$loghdr CALLING aira_cleanup()" );
aira_cleanup( $us3_db, $requestID, $gLink );
}
//write_log( "$loghdr RTN FR aira_cleanup()" );
}
else
{ // Non-airavata job: clean up in a non-aira way
write_log( "$loghdr calling gfac_cleanup() reqID=$requestID" );
gfac_cleanup( $us3_db, $requestID, $gLink );
}
}
// Function to update status of job
function update_job_status( $job_status, $gfacID )
{
global $gLink;
global $query;
global $self;
global $loghdr;
switch ( $job_status )
{
case 'SUBMITTED' :
case 'SUBMITED' :
case 'INITIALIZED' :
case 'UPDATING' :
case 'PENDING' :
$status = 'SUBMITTED';
$query = "UPDATE analysis SET status='SUBMITTED' WHERE gfacID='$gfacID'";
$message = "Job status request reports job is SUBMITTED";
break;
case 'STARTED' :
case 'RUNNING' :
case 'ACTIVE' :
$status = 'RUNNING';
$query = "UPDATE analysis SET status='RUNNING' WHERE gfacID='$gfacID'";
$message = "Job status request reports job is RUNNING";
break;
case 'EXECUTING' :
$message = "Job status request reports job is EXECUTING";
break;
case 'FINISHED' :
$status = 'FINISHED';
$query = "UPDATE analysis SET status='FINISHED' WHERE gfacID='$gfacID'";
$message = "NONE";
break;
case 'DONE' :
$status = 'DONE';
$query = "UPDATE analysis SET status='DONE' WHERE gfacID='$gfacID'";
$message = "NONE";
break;
case 'COMPLETED' :
case 'COMPLETE' :
$status = 'COMPLETE';
$query = "UPDATE analysis SET status='COMPLETE' WHERE gfacID='$gfacID'";
$message = "Job status request reports job is COMPLETED";
break;
case 'DATA' :
$status = 'DATA';
$query = "UPDATE analysis SET status='DATA' WHERE gfacID='$gfacID'";
$message = "Job status request reports job is COMPLETE, waiting for data";
break;
case 'CANCELED' :
case 'CANCELLED' :
$status = 'CANCELED';
$query = "UPDATE analysis SET status='CANCELED' WHERE gfacID='$gfacID'";
$message = "Job status request reports job is CANCELED";
break;
case 'FAILED' :
$status = 'FAILED';
$query = "UPDATE analysis SET status='FAILED' WHERE gfacID='$gfacID'";
$message = "Job status request reports job is FAILED";
break;
case 'UNKNOWN' :
write_log( "$loghdr job_status='UNKNOWN', reset to 'ERROR' " );
$status = 'ERROR';
$query = "UPDATE analysis SET status='ERROR' WHERE gfacID='$gfacID'";
$message = "Job status request reports job is not in the queue";
break;
default :
// We shouldn't ever get here
$status = 'ERROR';
$query = "UPDATE analysis SET status='ERROR' WHERE gfacID='$gfacID'";
$message = "Job status was not recognized - $job_status";
write_log( "$loghdr update_job_status: " .
"Job status was not recognized - $job_status\n" .
"gfacID = $gfacID\n" );
break;
}
$result = mysqli_query( $gLink, $query );
if ( ! $result )
write_log( "$loghdr Query failed $query - " . mysqli_error( $gLink ) );
if ( $message != 'NONE' )
{
update_queue_messages( $message );
update_db( $message );
update_autoflow_status( $status, $message );
} else {
update_autoflow_status( $status, $status );
}
}
function get_us3_data()
{
global $self;
global $gfacID;
global $autoflowID;
global $dbhost;
global $user;
global $passwd;
global $us3_db;
global $updateTime;
global $loghdr;
$us3_link = mysqli_connect( $dbhost, $user, $passwd, $us3_db );
if ( ! $us3_link )
{
write_log( "$loghdr could not connect: $dbhost, $user, $passwd, $us3_db" );
mail_to_admin( "fail", "Could not connect to $dbhost : $us3_db" );
return 0;
}
$query = "SELECT HPCAnalysisRequestID, UNIX_TIMESTAMP(updateTime) " .
"FROM HPCAnalysisResult WHERE gfacID='$gfacID'";
$result = mysqli_query( $us3_link, $query );
if ( ! $result )
{
write_log( "$self: Query failed $query - " . mysqli_error( $us3_link ) );
mail_to_admin( "fail", "Query failed $query\n" . mysqli_error( $us3_link ) );
return 0;
}
$numrows = mysqli_num_rows( $result );
if ( $numrows > 1 )
{ // Duplicate gfacIDs: get last
$query = "SELECT HPCAnalysisRequestID, UNIX_TIMESTAMP(updateTime) " .
"FROM HPCAnalysisResult WHERE gfacID='$gfacID' " .
" ORDER BY HPCAnalysisResultID DESC LIMIT 1";
$result = mysqli_query( $us3_link, $query );
}
list( $requestID, $updateTime ) = mysqli_fetch_array( $result );
mysqli_close( $us3_link );
return $requestID;
}
// Function to determine if this is a gfac job or not
function is_gfac_job( $gfacID )
{
return false;
}
// Function to determine if this is an airavata/thrift job or not
function is_aira_job( $gfacID )
{
global $cluster;
if ( preg_match( "/US3-A/i", $gfacID ) )
{
// Then it's an Airavata/Thrift job
return true;
}
return false;
}
// Function to get the current job status from GFAC
function get_gfac_status( $gfacID )
{
global $serviceURL;
global $self;
global $loghdr;
global $cluster;
global $status_ex, $status_gw;
if ( is_aira_job( $gfacID ) )
{
$status_ex = getExperimentStatus( $gfacID );
if ( $status_ex == 'EXECUTING' )
{
if ( $status_gw == 'RUNNING' )
$status_ex = 'ACTIVE';
else
$status_ex = 'QUEUED';
}
$gfac_status = standard_status( $status_ex );
}
else
{
return false;
}
return $gfac_status;
}
// Function to request data outputs from GFAC
function get_gfac_outputs( $gfacID )
{
global $serviceURL;
global $self;
// Make sure it's a GFAC job and status is appropriate for this call
if ( ( $job_status = get_gfac_status( $gfacID ) ) === false )
{
// Then it's not a GFAC job
$job_status = get_local_status( $gfacID );
return $job_status;
}
if ( ! in_array( $job_status, array( 'DONE', 'FAILED', 'COMPLETE', 'FINISHED' ) ) )
{
// Then it's not appropriate to request data
return false;
}
/*
$url = "$serviceURL/registeroutput/$gfacID";
try
{
$post = new HttpRequest( $url, HttpRequest::METH_GET );
$http = $post->send();
$xml = $post->getResponseBody();
}
catch ( HttpException $e )
{
write_log( "$self: Data not available - request failed - $gfacID" );
return false;
}
mail_to_admin( "debug", "get_gfac_outputs/\n$xml/" );
$gfac_status = parse_response( $xml );
*/
return $gfac_status;
}
function parse_response( $xml )
{
global $gfac_message;
$status = "";
$gfac_message = "";
$parser = new XMLReader();
$parser->xml( $xml );
while( $parser->read() )
{
$type = $parser->nodeType;
if ( $type == XMLReader::ELEMENT )
$name = $parser->name;
else if ( $type == XMLReader::TEXT )
{
if ( $name == "status" )
$status = $parser->value;
else
$gfac_message = $parser->value;
}
}
$parser->close();
return $status;
}
// Function to get status from local cluster
function get_local_status( $gfacID )
{
global $cluster;
global $self;
$is_demel3 = preg_match( "/demeler3/", $cluster );
$is_demel1 = preg_match( "/demeler1/", $cluster );
$is_jetstr = preg_match( "/jetstream/", $cluster );
$is_chino = preg_match( "/chinook/", $cluster );
$is_umont = preg_match( "/umontana/", $cluster );
$is_us3iab = preg_match( "/us3iab/", $cluster );
$is_slurm = ( $is_jetstr || $is_us3iab );
$is_squeu = ( $is_jetstr || $is_chino || $is_umont || $is_us3iab || $is_demel1 );
$ruser = "us3";
if ( $is_squeu )
$cmd = "squeue -t all -j $gfacID 2>&1|tail -n 1";
else
$cmd = "/usr/bin/qstat -a $gfacID 2>&1|tail -n 1";
//write_log( "$self cmd: $cmd" );
//write_log( "$self cluster: $cluster" );
//write_log( "$self gfacID: $gfacID" );
write_log( "$self gfacID $gfacID cluster $cluster" );
if ( ! $is_us3iab )
{
$system = "$cluster.uleth.ca";
if ( $is_slurm )
$system = "$cluster";
$system = preg_replace( "/\-local/", "", $system );
if ( $is_demel3 )
{
$system = "demeler3.uleth.ca";
}
if ( $is_chino )
{
$system = "chinook.hs.umt.edu";
}
if ( $is_umont )
{
$system = "login.gscc.umt.edu";
$ruser = "bd142854e";
}
write_log( "$self !is_usiab system $system" );
$cmd = "/usr/bin/ssh -x $ruser@$system " . $cmd;
write_log( "$self cmd: $cmd" );
}
$result = exec( $cmd );
//write_log( "$self result: $result" );
echo "locstat: cmd=$cmd result=$result\n";
write_log( "$self locstat: cmd=$cmd result=$result" );
$secwait = 2;
$num_try = 0;
// Sleep and retry up to 3 times if ssh has "ssh_exchange_identification" error
while ( preg_match( "/ssh_exchange_id/", $result ) && $num_try < 3 )
{
sleep( $secwait );
$num_try++;
$secwait *= 2;
write_log( "$me: num_try=$num_try secwait=$secwait" );
}
if ( preg_match( "/^qstat: Unknown/", $result ) ||
preg_match( "/ssh_exchange_id/", $result ) )
{
write_log( "$self get_local_status: Local job $gfacID unknown result=$result" );
return 'UNKNOWN';
}
$values = preg_split( "/\s+/", $result );
$jstat = ( $is_squeu == 0 ) ? $values[ 9 ] : $values[ 5 ];
write_log( "$self: get_local_status: job status = /$jstat/");
switch ( $jstat )
{
case "W" : // Waiting for execution time to be reached
case "E" : // Job is exiting after having run
case "R" : // Still running
case "CG" : // Job is completing
$status = 'ACTIVE';
break;
case "C" : // Job has completed
case "ST" : // Job has disappeared
case "CD" : // Job has completed
$status = 'COMPLETED';
break;
case "T" : // Job is being moved
case "H" : // Held
case "Q" : // Queued
case "PD" : // Queued
case "CF" : // Queued
$status = 'SUBMITTED';
break;
case "CA" : // Job has been canceled
$status = 'CANCELED';
break;
case "F" : // Job has failed
case "BF" : // Job has failed
case "NF" : // Job has failed
case "TO" : // Job has timed out
case "" : // Job has disappeared
$status = 'FAILED';
break;
default :
$status = 'UNKNOWN'; // This should not occur
break;
}
write_log( "$self: get_local_status: status = $status");
return $status;
}
function update_queue_messages( $message )
{
global $self;
global $gLink;
global $gfacID;
// Get analysis table ID
$query = "SELECT id FROM analysis " .
"WHERE gfacID = '$gfacID' ";
$result = mysqli_query( $gLink, $query );
if ( ! $result )
{
write_log( "$self: Query failed $query - " . mysqli_error( $gLink ) );
return;
}
list( $analysisID ) = mysqli_fetch_array( $result );
// Insert message into queue_message table
$query = "INSERT INTO queue_messages SET " .
"message = '" . mysqli_real_escape_string( $gLink, $message ) . "', " .
"analysisID = '$analysisID' ";
$result = mysqli_query( $gLink, $query );
if ( ! $result )
{
write_log( "$self: Query failed $query - " . mysqli_error( $gLink ) );
return;
}
}
function update_db( $message )
{
global $self;
global $gfacID;
global $dbhost;
global $user;
global $passwd;
global $us3_db;
$us3_link = mysqli_connect( $dbhost, $user, $passwd, $us3_db );
if ( ! $us3_link )
{
write_log( "$self: could not connect: $dbhost, $user, $passwd" );
mail_to_admin( "fail", "Could not connect to $dbhost : $us3_db" );
return 0;
}
$requestID = get_us3_data();