[973924]: / qiita_db / processing_job.py

Download this file

2542 lines (2233 with data), 107.6 kB

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
# -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
#
# Distributed under the terms of the BSD 3-clause License.
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------
import networkx as nx
import qiita_db as qdb
import pandas as pd
from numpy import log as nlog # noqa
from collections import defaultdict, Iterable
from datetime import datetime, timedelta
from itertools import chain
from json import dumps, loads
from multiprocessing import Process, Queue, Event
from re import search, findall
from subprocess import Popen, PIPE
from time import sleep
from uuid import UUID
from os.path import join
from humanize import naturalsize
from os import environ
from qiita_core.qiita_settings import qiita_config
from qiita_db.util import create_nested_path
class Watcher(Process):
# TODO: Qiita will need a proper mapping of these states to Qiita states
# Currently, these strings are being inserted directly into Qiita's status
# table. Qiita will be unfamiliar with many of these. We will need at least
# one additional job type for 'Held': A job waiting for another to complete
# before it can run.
#
# Note that the main Qiita script instantiates an object of this class in
# a separate thread, so it can periodically update the database w/metadata
# from Watcher's queue. Qiita's script also calls qdb.complete() so there
# are no circular references. TODO: replace w/a REST call.
# valid Qiita states:
# The current status of the job, one of {'queued', 'running',
# 'success', 'error', 'in_construction', 'waiting'}
# TODO: what to map in_construction to?
job_state_map = {'C': 'completed', 'E': 'exiting', 'H': 'held',
'Q': 'queued', 'R': 'running', 'T': 'moving',
'W': 'waiting', 'S': 'suspended'}
# TODO: moving, waiting, and suspended have been mapped to
# 'running' in Qiita, as 'waiting' in Qiita connotes that the
# main job itself has completed, and is waiting on validator
# jobs to finish, etc. Revisit
job_scheduler_to_qiita_state_map = {'completed': 'completed',
'held': 'queued',
'queued': 'queued',
'exiting': 'running',
'running': 'running',
'moving': 'running',
'waiting': 'running',
'suspended': 'running',
'DROPPED': 'error'}
def __init__(self):
super(Watcher, self).__init__()
# set self.owner to qiita, or whomever owns processes we need to watch.
self.owner = qiita_config.job_scheduler_owner
# Setting a polling value less than 60 seconds allows for multiple
# chances to catch the exit status before it disappears.
self.polling_value = qiita_config.job_scheduler_poll_val
# the cross-process method by which to communicate across
# process boundaries. Note that when Watcher object runs,
# another process will get created, and receive a copy of
# the Watcher object. At this point, these self.* variables
# become local to each process. Hence, the main process
# can't see self.processes for example; theirs will just
# be empty.
self.queue = Queue()
self.processes = {}
# the cross-process sentinel value to shutdown Watcher
self.event = Event()
def _element_extract(self, snippet, list_of_elements,
list_of_optional_elements):
results = {}
missing_elements = []
for element in list_of_elements:
value = search('<%s>(.*?)</%s>' % (element, element), snippet)
if value:
results[element] = value.group(1)
else:
missing_elements.append(element)
if missing_elements:
raise AssertionError("The following elements were not found: %s"
% ', '.join(missing_elements))
for element in list_of_optional_elements:
value = search('<%s>(.*?)</%s>' % (element, element), snippet)
if value:
results[element] = value.group(1)
return results
def _process_dependent_jobs(self, results):
# when a job has its status changed, check to see if the job completed
# with an error. If so, check to see if it had any jobs that were being
# 'held' on this job's successful completion. If we are maintaining
# state on any of these jobs, mark them as 'DROPPED', because they will
# no longer appear in qstat output.
if results['job_state'] == 'completed':
if results['exit_status'] == '0':
return
if 'depend' in results:
tmp = results['depend'].split(':')
if tmp[0] == 'beforeok':
tmp.pop(0)
for child_job_id in tmp:
# jobs in 'beforeok' are labeled with the complete
# job id and what looks to be the server name doing
# the work. For now, simply remove the
# '@host.domain.org' (server) component.
child_job_id = child_job_id.split('@')[0]
self.processes[child_job_id]['job_state'] = 'DROPPED'
self.queue.put(self.processes[child_job_id])
def run(self):
# check to see if qstat is available. If not, exit immediately.
proc = Popen("qstat -x", shell=True, stdout=PIPE, stderr=PIPE)
proc.wait()
if proc.returncode != 0:
# inform any process expecting data from Watcher
self.queue.put('QUIT')
self.event.set()
while not self.event.is_set():
proc = Popen("qstat -x", shell=True, stdout=PIPE, stderr=PIPE)
stdout, stderr = proc.communicate()
if proc.returncode == 0:
# qstat returned successfully with metadata on processes
# break up metadata into individual <Job></Job> elements
# for processing.
m = findall('<Job>(.*?)</Job>', stdout.decode('ascii'))
for item in m:
# filter out jobs that don't belong to owner
if search('<Job_Owner>%s</Job_Owner>' % self.owner, item):
# extract the metadata we want.
# if a job has completed, an exit_status element will
# be present. We also want that.
results = self._element_extract(item, ['Job_Id',
'Job_Name',
'job_state'],
['depend'])
tmp = Watcher.job_state_map[results['job_state']]
results['job_state'] = tmp
if results['job_state'] == 'completed':
results2 = self._element_extract(item,
['exit_status'],
[])
results['exit_status'] = results2['exit_status']
# determine if anything has changed since last poll
if results['Job_Id'] in self.processes:
if self.processes[results['Job_Id']] != results:
# metadata for existing job has changed
self.processes[results['Job_Id']] = results
self.queue.put(results)
self._process_dependent_jobs(results)
else:
# metadata for new job inserted
self.processes[results['Job_Id']] = results
self.queue.put(results)
else:
self.queue.put('QUIT')
self.event.set()
# don't join(), since we are exiting from the main loop
sleep(self.polling_value)
def stop(self):
# 'poison pill' to thread/process
self.queue.put('QUIT')
# setting self.event is a safe way of communicating a boolean
# value across processes and threads.
# when this event is 'set' by the main line of execution in Qiita,
# (or in any other process if need be), Watcher's run loop will
# stop and the Watcher process will exit.
self.event.set()
# Here, it is assumed that we are running this from the main
# context. By joining(), we're waiting for the Watcher process to
# end before returning from this method.
self.join()
def launch_local(env_script, start_script, url, job_id, job_dir):
# launch_local() differs from launch_job_scheduler(), as no Watcher() is
# used.
# each launch_local() process will execute the cmd as a child process,
# wait, and update the database once cmd has completed.
#
# As processes are lighter weight than jobs, this should be fine.
# This is how the current job model works locally.
cmd = [start_script, url, job_id, job_dir]
print("ENV_SCRIPT: %s" % env_script)
print("START_SCRIPT: %s" % start_script)
print("URL: %s" % url)
print("JOB ID: %s" % job_id)
print("JOB DIR: %s" % job_dir)
# When Popen() executes, the shell is not in interactive mode,
# so it is not sourcing any of the bash configuration files
# We need to source it so the env_script are available
cmd = "bash -c '%s; %s'" % (env_script, ' '.join(cmd))
print("CMD STRING: %s" % cmd)
# Popen() may also need universal_newlines=True
proc = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
# Communicate pulls all stdout/stderr from the PIPEs
# This call waits until cmd is done
stdout, stderr = proc.communicate()
print("STDOUT: %s" % stdout)
print("STDERR: %s" % stderr)
# proc.returncode will be equal to None if the process hasn't finished
# yet. If cmd was terminated by a SIGNAL, it will be a negative value.
# (*nix platforms only)
error = None
print("RETURN CODE: %s" % proc.returncode)
print("JOB ID: %s" % job_id)
if proc.returncode != 0:
error = "error from launch_local when launching cmd='%s'" % cmd
error = "%s\n%s\n%s" % (error, stdout, stderr)
# Forcing the creation of a new connection
qdb.sql_connection.create_new_transaction()
ProcessingJob(job_id).complete(False, error=error)
def launch_job_scheduler(env_script, start_script, url, job_id, job_dir,
dependent_job_id, resource_params):
# note that job_id is Qiita's UUID, not a job_scheduler job ID
cmd = [start_script, url, job_id, job_dir]
lines = [
'#!/bin/bash',
f'#SBATCH --error {job_dir}/slurm-error.txt',
f'#SBATCH --output {job_dir}/slurm-output.txt']
lines.append("echo $SLURM_JOBID")
lines.append("source ~/.bash_profile")
lines.append(env_script)
epilogue = environ.get('QIITA_JOB_SCHEDULER_EPILOGUE', '')
if epilogue:
lines.append(f"#SBATCH --epilog {epilogue}")
lines.append(' '.join(cmd))
# writing the script file
create_nested_path(job_dir)
fp = join(job_dir, '%s.txt' % job_id)
with open(fp, 'w') as job_file:
job_file.write("\n".join(lines))
sbatch_cmd = ['sbatch']
if dependent_job_id:
# note that a dependent job should be submitted before the
# 'parent' job ends
sbatch_cmd.append("-d")
sbatch_cmd.append("afterok:%s" % dependent_job_id)
sbatch_cmd.append(resource_params)
sbatch_cmd.append(fp)
stdout, stderr, return_value = _system_call(' '.join(sbatch_cmd))
if return_value != 0:
raise AssertionError(f'Error submitting job: {sbatch_cmd} :: {stderr}')
job_id = stdout.strip('\n').split(" ")[-1]
return job_id
def _system_call(cmd):
"""Execute the command `cmd`
Parameters
----------
cmd : str
The string containing the command to be run.
Returns
-------
tuple of (str, str, int)
The standard output, standard error and exist status of the
executed command
Notes
-----
This function is ported from QIIME (http://www.qiime.org), previously named
qiime_system_call. QIIME is a GPL project, but we obtained permission from
the authors of this function to port it to Qiita and keep it under BSD
license.
"""
proc = Popen(cmd, universal_newlines=True, shell=True, stdout=PIPE,
stderr=PIPE)
# Communicate pulls all stdout/stderr from the PIPEs
# This call blocks until the command is done
stdout, stderr = proc.communicate()
return_value = proc.returncode
return stdout, stderr, return_value
class ProcessingJob(qdb.base.QiitaObject):
r"""Models a job that executes a command in a set of artifacts
Attributes
----------
user
command
parameters
status
log
heartbeat
step
Methods
-------
exists
create
"""
_table = 'processing_job'
_launch_map = {'qiita-plugin-launcher':
{'function': launch_local,
'execute_in_process': False},
'qiita-plugin-launcher-slurm':
{'function': launch_job_scheduler,
'execute_in_process': True}}
@classmethod
def exists(cls, job_id):
"""Check if the job `job_id` exists
Parameters
----------
job_id : str
The job id
Returns
-------
bool
True if the job `job_id` exists. False otherwise.
"""
try:
UUID(job_id)
except ValueError:
return False
with qdb.sql_connection.TRN:
sql = """SELECT EXISTS(SELECT *
FROM qiita.processing_job
WHERE processing_job_id = %s)"""
qdb.sql_connection.TRN.add(sql, [job_id])
return qdb.sql_connection.TRN.execute_fetchlast()
@classmethod
def by_external_id(cls, external_id):
"""Return Qiita Job UUID associated with external_id
Parameters
----------
external_id : str
An external id (e.g. job scheduler Job ID)
Returns
-------
str
Qiita Job UUID, if found, otherwise None
"""
with qdb.sql_connection.TRN:
sql = """SELECT processing_job_id FROM qiita.processing_job
WHERE external_job_id = %s"""
qdb.sql_connection.TRN.add(sql, [external_id])
return cls(qdb.sql_connection.TRN.execute_fetchlast())
@property
def resource_allocation_info(self):
"""Return resource allocation defined for this job. For
external computational resources only.
Returns
-------
str
A resource allocation string useful to the external resource
"""
with qdb.sql_connection.TRN:
analysis = None
if self.command.name == 'complete_job':
jtype = 'COMPLETE_JOBS_RESOURCE_PARAM'
params = self.parameters.values
v = loads(params['payload'])
# assume an empty string for name is preferable to None
name = ''
if v['artifacts'] is not None:
an_element = list(v['artifacts'].keys())[0]
name = v['artifacts'][an_element]['artifact_type']
# for analysis we have two options, either use the
# input_artifacts or use the parameter 'analysis' of the job
# to complete
job = ProcessingJob(params['job_id'])
params = job.parameters.values
ia = job.input_artifacts
if 'analysis' in params and params['analysis'] is not None:
analysis = qdb.analysis.Analysis(params['analysis'])
elif ia:
analysis = ia[0].analysis
elif self.command.name == 'release_validators':
jtype = 'RELEASE_VALIDATORS_RESOURCE_PARAM'
tmp = ProcessingJob(self.parameters.values['job'])
name = tmp.parameters.command.name
if tmp.input_artifacts:
analysis = tmp.input_artifacts[0].analysis
elif self.command.name == 'Validate':
jtype = 'VALIDATOR'
vals = self.parameters.values
name = vals['artifact_type']
if vals['analysis'] is not None:
analysis = qdb.analysis.Analysis(vals['analysis'])
elif self.id == 'register':
jtype = 'REGISTER'
name = 'REGISTER'
else:
# assume anything else is a command
jtype = 'RESOURCE_PARAMS_COMMAND'
name = self.command.name
# for analysis we have two options, either use the
# input_artifacts or use the parameter 'analysis' of self
params = self.parameters.values
ia = self.input_artifacts
if 'analysis' in params and params['analysis'] is not None:
analysis = qdb.analysis.Analysis(params['analysis'])
elif ia:
analysis = ia[0].analysis
# first, query for resources matching name and type
sql = """SELECT allocation FROM
qiita.processing_job_resource_allocation
WHERE name = %s and job_type = %s"""
qdb.sql_connection.TRN.add(sql, [name, jtype])
result = qdb.sql_connection.TRN.execute_fetchflatten()
# if no matches for both type and name were found, query the
# 'default' value for the type
if not result:
sql = """SELECT allocation FROM
qiita.processing_job_resource_allocation WHERE
name = %s and job_type = %s"""
qdb.sql_connection.TRN.add(sql, ['default', jtype])
result = qdb.sql_connection.TRN.execute_fetchflatten()
if not result:
AssertionError(
"Could not match %s to a resource allocation!" % name)
allocation = result[0]
# adding user_level extra parameters
allocation = f'{allocation} {self.user.slurm_parameters}'.strip()
# adding analysis reservation
if analysis is not None:
sr = analysis.slurm_reservation
if sr is not None:
allocation = f'{allocation} --reservation {sr}'
if ('{samples}' in allocation or '{columns}' in allocation or
'{input_size}' in allocation):
samples, columns, input_size = self.shape
parts = []
error_msg = ('Obvious incorrect allocation. Please '
'contact %s' % qiita_config.help_email)
for part in allocation.split('--'):
param = ''
if part.startswith('time '):
param = 'time '
elif part.startswith('mem '):
param = 'mem '
else:
# if parts is empty, this is the first part so no --
if parts:
parts.append(f'--{part.strip()}')
else:
parts.append(part.strip())
continue
part = part[len(param):]
if ('{samples}' in part or '{columns}' in part or
'{input_size}' in part):
# to make sure that the formula is correct and avoid
# possible issues with conversions, we will check that
# all the variables {samples}/{columns}/{input_size}
# present in the formula are not None, if any is None
# we will set the job's error (will stop it) and the
# message is gonna be shown to the user within the job
if (('{samples}' in part and samples is None) or
('{columns}' in part and columns is None) or
('{input_size}' in part and input_size is
None)):
self._set_error(error_msg)
return 'Not valid'
try:
# if eval has something that can't be processed
# it will raise a NameError
value = eval(part.format(
samples=samples, columns=columns,
input_size=input_size))
except NameError:
self._set_error(error_msg)
return 'Not valid'
else:
if value <= 0:
self._set_error(error_msg)
return 'Not valid'
if param == 'time ':
td = timedelta(seconds=value)
if td.days > 0:
days = td.days
td = td - timedelta(days=days)
part = f'{days}-{str(td)}'
else:
part = str(td)
part = part.split('.')[0]
else:
part = naturalsize(
value, gnu=True, format='%.0f')
parts.append(f'--{param}{part}'.strip())
allocation = ' '.join(parts)
return allocation
@classmethod
def create(cls, user, parameters, force=False):
"""Creates a new job in the system
Parameters
----------
user : qiita_db.user.User
The user executing the job
parameters : qiita_db.software.Parameters
The parameters of the job being executed
force : bool
Force creation on duplicated parameters
Returns
-------
qiita_db.processing_job.ProcessingJob
The newly created job
Notes
-----
If force is True the job is going to be created even if another job
exists with the same parameters
"""
TTRN = qdb.sql_connection.TRN
with TTRN:
command = parameters.command
if not force:
# check if a job with the same parameters already exists
sql = """SELECT processing_job_id, email,
processing_job_status, COUNT(aopj.artifact_id)
FROM qiita.processing_job
LEFT JOIN qiita.processing_job_status
USING (processing_job_status_id)
LEFT JOIN qiita.artifact_output_processing_job aopj
USING (processing_job_id)
WHERE command_id = %s AND processing_job_status IN (
'success', 'waiting', 'running', 'in_construction') {0}
GROUP BY processing_job_id, email,
processing_job_status"""
# we need to use ILIKE because of booleans as they can be
# false or False
params = []
for k, v in parameters.values.items():
# this is necessary in case we have an Iterable as a value
# but that is string
if isinstance(v, Iterable) and not isinstance(v, str):
for vv in v:
params.extend([k, str(vv)])
else:
params.extend([k, str(v)])
if params:
# divided by 2 as we have key-value pairs
len_params = int(len(params)/2)
sql = sql.format(' AND ' + ' AND '.join(
["command_parameters->>%s ILIKE %s"] * len_params))
params = [command.id] + params
TTRN.add(sql, params)
else:
# the sql variable expects the list of parameters but if
# there is no param we need to replace the {0} with an
# empty string
TTRN.add(sql.format(""), [command.id])
# checking that if the job status is success, it has children
# [2] status, [3] children count
existing_jobs = [r for r in TTRN.execute_fetchindex()
if r[2] != 'success' or r[3] > 0]
if existing_jobs:
raise ValueError(
'Cannot create job because the parameters are the '
'same as jobs that are queued, running or already '
'have succeeded:\n%s' % '\n'.join(
["%s: %s" % (jid, status)
for jid, _, status, _ in existing_jobs]))
sql = """INSERT INTO qiita.processing_job
(email, command_id, command_parameters,
processing_job_status_id)
VALUES (%s, %s, %s, %s)
RETURNING processing_job_id"""
status = qdb.util.convert_to_id(
"in_construction", "processing_job_status")
sql_args = [user.id, command.id,
parameters.dump(), status]
TTRN.add(sql, sql_args)
job_id = TTRN.execute_fetchlast()
# Link the job with the input artifacts
sql = """INSERT INTO qiita.artifact_processing_job
(artifact_id, processing_job_id)
VALUES (%s, %s)"""
pending = defaultdict(dict)
for pname, vals in command.parameters.items():
if vals[0] == 'artifact':
artifact_info = parameters.values[pname]
# If the artifact_info is a list, then the artifact
# still doesn't exist because the current job is part
# of a workflow, so we can't link
if not isinstance(artifact_info, list):
TTRN.add(sql, [artifact_info, job_id])
else:
pending[artifact_info[0]][pname] = artifact_info[1]
elif pname == 'artifact':
TTRN.add(sql, [parameters.values[pname], job_id])
if pending:
sql = """UPDATE qiita.processing_job
SET pending = %s
WHERE processing_job_id = %s"""
TTRN.add(sql, [dumps(pending), job_id])
TTRN.execute()
return cls(job_id)
@property
def user(self):
"""The user that launched the job
Returns
-------
qiita_db.user.User
The user that launched the job
"""
with qdb.sql_connection.TRN:
sql = """SELECT email
FROM qiita.processing_job
WHERE processing_job_id = %s"""
qdb.sql_connection.TRN.add(sql, [self.id])
email = qdb.sql_connection.TRN.execute_fetchlast()
return qdb.user.User(email)
@property
def command(self):
"""The command that the job executes
Returns
-------
qiita_db.software.Command
The command that the job executes
"""
with qdb.sql_connection.TRN:
sql = """SELECT command_id
FROM qiita.processing_job
WHERE processing_job_id = %s"""
qdb.sql_connection.TRN.add(sql, [self.id])
cmd_id = qdb.sql_connection.TRN.execute_fetchlast()
return qdb.software.Command(cmd_id)
@property
def parameters(self):
"""The parameters used in the job's command
Returns
-------
qiita_db.software.Parameters
The parameters used in the job's command
"""
with qdb.sql_connection.TRN:
sql = """SELECT command_id, command_parameters
FROM qiita.processing_job
WHERE processing_job_id = %s"""
qdb.sql_connection.TRN.add(sql, [self.id])
res = qdb.sql_connection.TRN.execute_fetchindex()[0]
return qdb.software.Parameters.load(
qdb.software.Command(res[0]), values_dict=res[1])
@property
def input_artifacts(self):
"""The artifacts used as input in the job
Returns
-------
list of qiita_db.artifact.Artifact
The artifacs used as input in the job
"""
with qdb.sql_connection.TRN:
sql = """SELECT artifact_id
FROM qiita.artifact_processing_job
WHERE processing_job_id = %s
ORDER BY artifact_id"""
qdb.sql_connection.TRN.add(sql, [self.id])
return [qdb.artifact.Artifact(aid)
for aid in qdb.sql_connection.TRN.execute_fetchflatten()]
@property
def status(self):
"""The status of the job
Returns
-------
str
The current status of the job, one of {'queued', 'running',
'success', 'error', 'in_construction', 'waiting'}
"""
with qdb.sql_connection.TRN:
sql = """SELECT processing_job_status
FROM qiita.processing_job_status
JOIN qiita.processing_job
USING (processing_job_status_id)
WHERE processing_job_id = %s"""
qdb.sql_connection.TRN.add(sql, [self.id])
return qdb.sql_connection.TRN.execute_fetchlast()
def _generate_notification_message(self, value, error_msg):
ignored_software = ('artifact definition',)
ignored_commands = ('Validate', 'complete_job', 'release_validators')
# abort early conditions (don't send an email notification)
# tentatively accept the overhead of a function-call, even when a
# notification isn't sent, just to keep the logic clean and
# centralized.
if value == 'waiting':
# notification not needed.
return None
if not self.user.info['receive_processing_job_emails']:
# notification not needed.
return None
if self.command.software.name in ignored_software:
# notification not needed.
return None
if self.command.name in ignored_commands:
# notification not needed.
return None
# generate subject line
subject = (f'{self.command.name}: {value}, {self.id} '
f'[{self.external_id}]')
# generate message line
message = ''
input_artifacts = self.input_artifacts
if input_artifacts is None:
# this is an admin job. display command name and parameters
message = (f'Admin Job {self.command.name} '
f'{self.command.parameters}')
else:
for artifact in input_artifacts:
if artifact.prep_templates:
# this is a processing job. display the study id as link,
# prep ids, data_type, and command name.
study_ids = [x.study_id for x in artifact.prep_templates]
prep_ids = [x.id for x in artifact.prep_templates]
data_types = [x.data_type() for x in
artifact.prep_templates]
# there should only be one study id
study_ids = set(study_ids)
if len(study_ids) > 1:
raise qdb.exceptions.QiitaError("More than one Study "
"ID was found: "
f"{study_ids}")
study_id = study_ids.pop()
# there should be at least one prep_id and probably more.
prep_ids = list(set(prep_ids))
if len(prep_ids) == 0:
raise qdb.exceptions.QiitaError("No Prep IDs were "
"found")
if len(prep_ids) == 1:
study_url = (f'{qiita_config.base_url}/study/'
f'description/{study_id}?prep_id='
f'{prep_ids[0]}')
else:
study_url = (f'{qiita_config.base_url}/study/'
f'description/{study_id}')
# convert into a string for presentation.
prep_ids = [str(x) for x in prep_ids]
prep_ids = ', '.join(prep_ids)
# there should be only one data type.
data_types = set(data_types)
if len(data_types) > 1:
raise qdb.exceptions.QiitaError("More than one data "
"type was found: "
f"{data_types}")
data_type = data_types.pop()
message = f'{self.command.name}\n'
message += f'Prep IDs: {prep_ids}\n'
message += f'{study_url}\n'
message += f'Data Type: {data_type}\n'
elif artifact.analysis:
# this is an analysis job. display analysis id as link and
# the command name.
message = f'Analysis Job {self.command.name}\n'
message += f'{qiita_config.base_url}/analysis/'
message += f'description/{artifact.analysis.id}/\n'
else:
raise qdb.exceptions.QiitaError("Unknown Condition")
# append legacy message line
message += 'New status: %s' % (value)
if value == 'error' and error_msg is not None:
message += f'\n\nError:\n{error_msg}'
return {'subject': subject, 'message': message}
def _set_status(self, value, error_msg=None):
"""Sets the status of the job
Parameters
----------
value : str, {'queued', 'running', 'success', 'error',
'in_construction', 'waiting'}
The new status of the job
error_msg : str, optional
If not None this is the message that is going to be sent to the
user when the value is 'error'
Raises
------
qiita_db.exceptions.QiitaDBStatusError
- If the current status of the job is 'success'
- If the current status of the job is 'running' and `value` is
'queued'
"""
with qdb.sql_connection.TRN:
current_status = self.status
if current_status == 'success':
raise qdb.exceptions.QiitaDBStatusError(
"Cannot change the status of a 'success' job")
elif current_status == 'running' and value == 'queued':
raise qdb.exceptions.QiitaDBStatusError(
"Cannot revert the status of a 'running' job to 'queued'")
new_status = qdb.util.convert_to_id(
value, "processing_job_status")
msg = self._generate_notification_message(value, error_msg)
if msg is not None:
# send email
qdb.util.send_email(self.user.email, msg['subject'],
msg['message'])
# send email to our sys-admin if error from admin
if self.user.level in {'admin', 'wet-lab admin'}:
if value == 'error':
qdb.util.send_email(
qiita_config.sysadmin_email, msg['subject'],
msg['message'])
sql = """UPDATE qiita.processing_job
SET processing_job_status_id = %s
WHERE processing_job_id = %s"""
qdb.sql_connection.TRN.add(sql, [new_status, self.id])
qdb.sql_connection.TRN.execute()
@property
def external_id(self):
"""Retrieves the external id"""
with qdb.sql_connection.TRN:
sql = """SELECT external_job_id
FROM qiita.processing_job
WHERE processing_job_id = %s"""
qdb.sql_connection.TRN.add(sql, [self.id])
result = qdb.sql_connection.TRN.execute_fetchlast()
if result is None:
result = 'Not Available'
return result
@external_id.setter
def external_id(self, value):
"""Sets the external job id of the job
Parameters
----------
value : str, {'queued', 'running', 'success', 'error',
'in_construction', 'waiting'}
The job's new status
Raises
------
qiita_db.exceptions.QiitaDBStatusError
- If the current status of the job is 'success'
- If the current status of the job is 'running' and `value` is
'queued'
"""
sql = """UPDATE qiita.processing_job
SET external_job_id = %s
WHERE processing_job_id = %s"""
qdb.sql_connection.perform_as_transaction(sql, [value, self.id])
@property
def release_validator_job(self):
"""Retrieves the release validator job
Returns
-------
qiita_db.processing_job.ProcessingJob or None
The release validator job of this job
"""
rvalidator = None
with qdb.sql_connection.TRN:
sql = """SELECT processing_job_id
FROM qiita.processing_job
WHERE command_id in (
SELECT command_id
FROM qiita.software_command
WHERE name = 'release_validators')
AND command_parameters->>'job' = %s"""
qdb.sql_connection.TRN.add(sql, [self.id])
results = qdb.sql_connection.TRN.execute_fetchflatten()
if results:
rvalidator = ProcessingJob(results[0])
return rvalidator
def submit(self, parent_job_id=None, dependent_jobs_list=None):
"""Submits the job to execution
This method has the ability to submit itself, as well as a list of
other ProcessingJob objects. If a list of ProcessingJob objects is
supplied, they will be submitted conditionally on the successful
execution of this object.
Users of this method don't need to set parent_job_id. It is used
internally by submit() for subsequent submit() calls for dependents.
Raises
------
QiitaDBOperationNotPermittedError
If the job is not in 'waiting' or 'in_construction' status
"""
with qdb.sql_connection.TRN:
status = self.status
if status not in {'in_construction', 'waiting'}:
raise qdb.exceptions.QiitaDBOperationNotPermittedError(
"Can't submit job, not in 'in_construction' or "
"'waiting' status. Current status: %s" % status)
self._set_status('queued')
# At this point we are going to involve other processes. We need
# to commit the changes to the DB or the other processes will not
# see these changes
qdb.sql_connection.TRN.commit()
job_dir = join(qdb.util.get_work_base_dir(), self.id)
command = self.command
software = command.software
cname = command.name
plugin_start_script = software.start_script
plugin_env_script = software.environment_script
# Appending the portal URL so the job requests the information from the
# portal server that submitted the job
url = "%s%s" % (qiita_config.base_url, qiita_config.portal_dir)
# if the word ENVIRONMENT is in the plugin_env_script we have a special
# case where we are going to execute some command and then wait for the
# plugin to return their own id (first implemented for
# fast-bowtie2+woltka)
#
# This is the hardcoded lines described in issue:
# https://github.com/qiita-spots/qiita/issues/3340
# the idea is that in the future we shouldn't check specific command
# names to know if it should be executed differently and the
# plugin should let Qiita know that a specific command should be ran
# as job array or not
cnames_to_skip = {'Calculate Cell Counts', 'Calculate RNA Copy Counts'}
if 'ENVIRONMENT' in plugin_env_script and cname not in cnames_to_skip:
# the job has to be in running state so the plugin can change its`
# status
with qdb.sql_connection.TRN:
self._set_status('running')
qdb.sql_connection.TRN.commit()
create_nested_path(job_dir)
cmd = (f'{plugin_env_script}; {plugin_start_script} '
f'{url} {self.id} {job_dir}')
stdout, stderr, return_value = _system_call(cmd)
if return_value != 0 or stderr != '':
self._set_error(stderr)
job_id = stdout
# note that dependent jobs, such as m validator jobs marshalled into
# n 'queues' require the job_id returned by an external scheduler such
# as Torque's MOAB, rather than a job name that can be defined within
# Qiita. Hence, this method must be able to handle the case where a job
# requires metadata from a late-defined and time-sensitive source.
elif qiita_config.plugin_launcher in ProcessingJob._launch_map:
launcher = ProcessingJob._launch_map[qiita_config.plugin_launcher]
if launcher['execute_in_process']:
# run this launcher function within this process.
# usually this is done if the launcher spawns other processes
# before returning immediately, usually with a job ID that can
# be used to monitor the job's progress.
try:
resource_params = self.resource_allocation_info
except qdb.exceptions.QiitaDBUnknownIDError as e:
# this propagates the error to the job and using str(e)
# should be fine as we just want the last calculation
# error
self._set_error(str(e))
# note that parent_job_id is being passed transparently from
# submit declaration to the launcher.
# TODO: In proc launches should throw exceptions, that are
# handled by this code. Out of proc launches will need to
# handle exceptions by catching them and returning an error
# code.
job_id = launcher['function'](plugin_env_script,
plugin_start_script,
url,
self.id,
job_dir,
parent_job_id, resource_params)
if dependent_jobs_list:
# a dependent_jobs_list will always have at least one
# job
next_job = dependent_jobs_list.pop(0)
if not dependent_jobs_list:
# dependent_jobs_list is now empty
dependent_jobs_list = None
# The idea here is that a list of jobs is considered a
# chain. Each job in the chain is submitted with the job
# id of job submitted before it; a job will only run if
# 'parent_job' ran successfully. Each iteration of submit()
# launches a job, pulls the next job from the list, and
# submits it. The remainder of the list is also passed to
# continue the process.
next_job.submit(parent_job_id=job_id,
dependent_jobs_list=dependent_jobs_list)
elif not launcher['execute_in_process']:
# run this launcher function as a new process.
# usually this is done if the launcher performs work that takes
# an especially long time, or waits for children who perform
# such work.
p = Process(target=launcher['function'],
args=(plugin_env_script,
plugin_start_script,
url,
self.id,
job_dir))
p.start()
job_id = p.pid
if dependent_jobs_list:
# for now, treat dependents as independent when
# running locally. This means they will not be
# organized into n 'queues' or 'chains', and
# will all run simultaneously.
for dependent in dependent_jobs_list:
# register dependent job as queued to make qiita
# aware of this child process
dependent._set_status('queued')
dep_software = dependent.command.software
dep_job_dir = join(qdb.util.get_work_base_dir(),
dependent.id)
p = Process(target=launcher['function'],
args=(dep_software.environment_script,
dep_software.start_script,
url,
dependent.id,
dep_job_dir))
p.start()
# assign the child process ID as external id to
# the dependent
dependent.external_id = p.pid
else:
error = ("execute_in_process must be defined",
"as either true or false")
raise AssertionError(error)
else:
error = "plugin_launcher should be one of two values for now"
raise AssertionError(error)
# note that at this point, self.id is Qiita's UUID for a Qiita
# job. job_id at this point is an external ID (e.g. Torque Job
# ID). Record the mapping between job_id and self.id using
# external_id.
if job_id is not None:
self.external_id = job_id
def release(self):
"""Releases the job from the waiting status and creates the artifact
Returns
-------
dict of {int: int}
The mapping between the job output and the artifact
"""
with qdb.sql_connection.TRN:
if self.command.software.type != 'artifact definition':
raise qdb.exceptions.QiitaDBOperationNotPermittedError(
"Only artifact definition jobs can be released")
# Retrieve the artifact information from the DB
sql = """SELECT artifact_info
FROM qiita.processing_job_validator
WHERE validator_id = %s"""
qdb.sql_connection.TRN.add(sql, [self.id])
a_info = qdb.sql_connection.TRN.execute_fetchlast()
provenance = loads(self.parameters.values['provenance'])
job = ProcessingJob(provenance['job'])
if 'data_type' in a_info:
# This job is resulting from a private job
parents = None
params = None
name = None
data_type = a_info['data_type']
pvals = job.parameters.values
if 'analysis' in pvals:
cmd_out_id = None
analysis = qdb.analysis.Analysis(
job.parameters.values['analysis'])
else:
cmd_out_id = provenance['cmd_out_id']
analysis = None
a_info = a_info['artifact_data']
else:
# This job is resulting from a plugin job
parents = job.input_artifacts
params = job.parameters
cmd_out_id = provenance['cmd_out_id']
name = provenance['name']
analysis = None
data_type = None
# Create the artifact
atype = a_info['artifact_type']
filepaths = a_info['filepaths']
a = qdb.artifact.Artifact.create(
filepaths, atype, parents=parents,
processing_parameters=params,
analysis=analysis, data_type=data_type, name=name)
self._set_status('success')
mapping = {}
if cmd_out_id is not None:
mapping = {cmd_out_id: a.id}
return mapping
def release_validators(self):
"""Allows all the validator job spawned by this job to complete"""
if self.command.software.type not in ('artifact transformation',
'private'):
raise qdb.exceptions.QiitaDBOperationNotPermittedError(
"Only artifact transformation and private jobs can "
"release validators")
# Check if all the validators are completed. Validator jobs can be
# in two states when completed: 'waiting' in case of success
# or 'error' otherwise
validator_ids = ['%s [%s]' % (j.id, j.external_id)
for j in self.validator_jobs
if j.status not in ['waiting', 'error']]
# Active polling - wait until all validator jobs are completed
# TODO: As soon as we see one errored validator, we should kill
# the other jobs and exit early. Don't wait for all of the jobs
# to complete.
while validator_ids:
jids = ', '.join(validator_ids)
self.step = ("Validating outputs (%d remaining) via "
"job(s) %s" % (len(validator_ids), jids))
sleep(10)
validator_ids = ['%s [%s]' % (j.id, j.external_id)
for j in self.validator_jobs
if j.status not in ['waiting', 'error']]
# Check if any of the validators errored
errored = [j for j in self.validator_jobs
if j.status == 'error']
if errored:
# At least one of the validators failed, Set the rest of the
# validators and the current job as failed
waiting = [j.id for j in self.validator_jobs
if j.status == 'waiting']
common_error = "\n".join(
["Validator %s error message: %s" % (j.id, j.log.msg)
for j in errored])
val_error = "%d sister validator jobs failed: %s" % (
len(errored), common_error)
for j in waiting:
ProcessingJob(j)._set_error(val_error)
self._set_error('%d validator jobs failed: %s'
% (len(errored), common_error))
else:
mapping = {}
# Loop through all validator jobs and release them, allowing
# to create the artifacts. Note that if any artifact creation
# fails, the rollback operation will make sure that the
# previously created artifacts are not in there
for vjob in self.validator_jobs:
mapping.update(vjob.release())
if mapping:
sql = """INSERT INTO
qiita.artifact_output_processing_job
(artifact_id, processing_job_id,
command_output_id)
VALUES (%s, %s, %s)"""
sql_args = [[aid, self.id, outid]
for outid, aid in mapping.items()]
with qdb.sql_connection.TRN:
qdb.sql_connection.TRN.add(sql, sql_args, many=True)
self._update_and_launch_children(mapping)
self._set_status('success')
def _complete_artifact_definition(self, artifact_data):
""""Performs the needed steps to complete an artifact definition job
In order to complete an artifact definition job we need to create
the artifact, and then start all the jobs that were waiting for this
artifact to be created. Note that each artifact definition job creates
one and only one artifact.
Parameters
----------
artifact_data : {'filepaths': list of (str, str), 'artifact_type': str}
Dict with the artifact information. `filepaths` contains the list
of filepaths and filepath types for the artifact and
`artifact_type` the type of the artifact
Notes
-----
The `provenance` in the job.parameters can contain a `direct_creation`
flag to avoid having to wait for the complete job to create a new
artifact, which is normally ran during regular processing. Skipping is
fine because we are adding an artifact to an existing job outside of
regular processing
"""
with qdb.sql_connection.TRN:
atype = artifact_data['artifact_type']
filepaths = artifact_data['filepaths']
# We need to differentiate if this artifact is the
# result of a previous job or uploading
job_params = self.parameters.values
if job_params['provenance'] is not None:
# The artifact is a result from a previous job
provenance = loads(job_params['provenance'])
if provenance.get('direct_creation', False):
original_job = ProcessingJob(provenance['job'])
artifact = qdb.artifact.Artifact.create(
filepaths, atype,
parents=original_job.input_artifacts,
processing_parameters=original_job.parameters,
analysis=job_params['analysis'],
name=job_params['name'])
sql = """
INSERT INTO qiita.artifact_output_processing_job
(artifact_id, processing_job_id,
command_output_id)
VALUES (%s, %s, %s)"""
qdb.sql_connection.TRN.add(
sql, [artifact.id, original_job.id,
provenance['cmd_out_id']])
qdb.sql_connection.TRN.execute()
self._set_status('success')
else:
if provenance.get('data_type') is not None:
artifact_data = {'data_type': provenance['data_type'],
'artifact_data': artifact_data}
sql = """UPDATE qiita.processing_job_validator
SET artifact_info = %s
WHERE validator_id = %s"""
qdb.sql_connection.TRN.add(
sql, [dumps(artifact_data), self.id])
qdb.sql_connection.TRN.execute()
# Can't create the artifact until all validators
# are completed
self._set_status('waiting')
else:
# The artifact is uploaded by the user or is the initial
# artifact of an analysis
if ('analysis' in job_params and
job_params['analysis'] is not None):
pt = None
an = qdb.analysis.Analysis(job_params['analysis'])
sql = """SELECT data_type
FROM qiita.analysis_processing_job
WHERE analysis_id = %s
AND processing_job_id = %s"""
qdb.sql_connection.TRN.add(sql, [an.id, self.id])
data_type = qdb.sql_connection.TRN.execute_fetchlast()
elif job_params['template'] is not None:
pt = qdb.metadata_template.prep_template.PrepTemplate(
job_params['template'])
an = None
data_type = None
else:
pt = None
an = None
data_type = 'Job Output Folder'
artifact = qdb.artifact.Artifact.create(
filepaths, atype, prep_template=pt, analysis=an,
data_type=data_type, name=job_params['name'])
self._set_status('success')
# we need to update the children jobs to replace the input
# for the newly created artifact via the validator
for c in self.children:
self._helper_update_children({atype: artifact.id})
c.submit()
def _complete_artifact_transformation(self, artifacts_data):
"""Performs the needed steps to complete an artifact transformation job
In order to complete an artifact transformation job, we need to create
a validate job for each artifact output and submit it.
Parameters
----------
artifacts_data : dict of dicts
The generated artifact information keyed by output name.
The format of each of the internal dictionaries must be
{'filepaths': list of (str, str), 'artifact_type': str}
where `filepaths` contains the list of filepaths and filepath types
for the artifact and `artifact_type` the type of the artifact
Raises
------
QiitaDBError
If there is more than one prep information attached to the new
artifact
"""
validator_jobs = []
with qdb.sql_connection.TRN:
cmd_id = self.command.id
for out_name, a_data in artifacts_data.items():
# Correct the format of the filepaths parameter so we can
# create a validate job
filepaths = defaultdict(list)
for fp, fptype in a_data['filepaths']:
filepaths[fptype].append(fp)
atype = a_data['artifact_type']
# The validate job needs a prep information file. In theory,
# a job can be generated from more that one prep information
# file, so we check here if we have one or more templates. At
# this moment, If we allow more than one template, there is a
# fair amount of changes that need to be done on the plugins,
# so we are going to restrict the number of templates to one.
# Note that at this moment there is no way of generating an
# artifact from 2 or more artifacts, so we can impose this
# limitation now and relax it later.
templates = set()
for artifact in self.input_artifacts:
templates.update(pt.id for pt in artifact.prep_templates)
template = None
analysis = None
if len(templates) > 1:
raise qdb.exceptions.QiitaDBError(
"Currently only single prep template "
"is allowed, found %d" % len(templates))
elif len(templates) == 1:
template = templates.pop()
elif self.input_artifacts:
# In this case we have 0 templates. What this means is that
# this artifact is being generated in the analysis pipeline
# All the artifacts included in the analysis pipeline
# belong to the same analysis, so we can just ask the
# first artifact for the analysis that it belongs to
analysis = self.input_artifacts[0].analysis.id
# Once the validate job completes, it needs to know if it has
# been generated from a command (and how) or if it has been
# uploaded. In order to differentiate these cases, we populate
# the provenance parameter with some information about the
# current job and how this artifact has been generated. This
# does not affect the plugins since they can ignore this
# parameter
sql = """SELECT command_output_id
FROM qiita.command_output
WHERE name = %s AND command_id = %s"""
qdb.sql_connection.TRN.add(sql, [out_name, cmd_id])
cmd_out_id = qdb.sql_connection.TRN.execute_fetchlast()
naming_params = self.command.naming_order
if naming_params:
params = self.parameters.values
art_name = "%s %s" % (
out_name, ' '.join([str(params[p]).split('/')[-1]
for p in naming_params]))
else:
art_name = out_name
provenance = {'job': self.id,
'cmd_out_id': cmd_out_id,
'name': art_name}
if self.command.software.type == 'private':
provenance['data_type'] = 'Job Output Folder'
# Get the validator command for the current artifact type and
# create a new job
# see also release_validators()
cmd = qdb.software.Command.get_validator(atype)
values_dict = {
'files': dumps(filepaths), 'artifact_type': atype,
'template': template, 'provenance': dumps(provenance),
'analysis': None}
if analysis is not None:
values_dict['analysis'] = analysis
validate_params = qdb.software.Parameters.load(
cmd, values_dict=values_dict)
validator_jobs.append(
ProcessingJob.create(self.user, validate_params, True))
# Change the current step of the job
self.step = "Validating outputs (%d remaining) via job(s) %s" % (
len(validator_jobs), ', '.join(['%s [%s]' % (
j.id, j.external_id) for j in validator_jobs]))
# Link all the validator jobs with the current job
self._set_validator_jobs(validator_jobs)
# Submit m validator jobs as n lists of jobs
n = qiita_config.job_scheduler_dependency_q_cnt
if n is None:
n = 2
# taken from:
# https://www.geeksforgeeks.org/break-list-chunks-size-n-python/
lists = [validator_jobs[i * n:(i + 1) * n]
for i in range((len(validator_jobs) + n - 1) // n)]
for sub_list in lists:
# each sub_list will always have at least a lead_job
lead_job = sub_list.pop(0)
if not sub_list:
# sub_list is now empty
sub_list = None
lead_job.submit(dependent_jobs_list=sub_list)
# Submit the job that will release all the validators
plugin = qdb.software.Software.from_name_and_version(
'Qiita', 'alpha')
cmd = plugin.get_command('release_validators')
params = qdb.software.Parameters.load(
cmd, values_dict={'job': self.id})
job = ProcessingJob.create(self.user, params)
# Doing the submission outside of the transaction
job.submit()
def _set_validator_jobs(self, validator_jobs):
"""Sets the validator jobs for the current job
Parameters
----------
validator_jobs : list of ProcessingJob
The validator_jobs for the current job
"""
with qdb.sql_connection.TRN:
sql = """INSERT INTO qiita.processing_job_validator
(processing_job_id, validator_id)
VALUES (%s, %s)"""
sql_args = [[self.id, j.id] for j in validator_jobs]
qdb.sql_connection.TRN.add(sql, sql_args, many=True)
qdb.sql_connection.TRN.execute()
def complete(self, success, artifacts_data=None, error=None):
"""Completes the job, either with a success or error status
Parameters
----------
success : bool
Whether the job has completed successfully or not
artifacts_data : dict of dicts, optional
The generated artifact information keyed by output name.
The format of each of the internal dictionaries must be
{'filepaths': list of (str, str), 'artifact_type': str}
where `filepaths` contains the list of filepaths and filepath types
for the artifact and `artifact_type` the type of the artifact
error : str, optional
If the job was not successful, the error message
Raises
------
qiita_db.exceptions.QiitaDBOperationNotPermittedError
If the job is not in running state
"""
with qdb.sql_connection.TRN:
if success:
if self.status != 'running':
# If the job is not running, we only allow to complete it
# if it did not succeed
raise qdb.exceptions.QiitaDBOperationNotPermittedError(
"Can't complete job: not in a running state")
if artifacts_data:
if self.command.software.type == 'artifact definition':
# There is only one artifact created
_, a_data = artifacts_data.popitem()
self._complete_artifact_definition(a_data)
else:
self._complete_artifact_transformation(artifacts_data)
else:
self._set_status('success')
else:
self._set_error(error)
@property
def log(self):
"""The log entry attached to the job if it failed
Returns
-------
qiita_db.logger.LogEntry or None
If the status of the job is `error`, returns the LogEntry attached
to the job
"""
with qdb.sql_connection.TRN:
res = None
if self.status == 'error':
sql = """SELECT logging_id
FROM qiita.processing_job
WHERE processing_job_id = %s"""
qdb.sql_connection.TRN.add(sql, [self.id])
log_id = qdb.sql_connection.TRN.execute_fetchlast()
res = qdb.logger.LogEntry(log_id)
return res
def _set_error(self, error):
"""Attaches a log entry to the job
Parameters
----------
error : str
The error message
Raises
------
qiita_db.exceptions.QiitaDBOperationNotPermittedError
If the status of the job is 'success'
"""
with qdb.sql_connection.TRN:
if self.status == 'success':
raise qdb.exceptions.QiitaDBOperationNotPermittedError(
"Can only set up the log for jobs whose status is 'error'")
log = qdb.logger.LogEntry.create('Runtime', error)
sql = """UPDATE qiita.processing_job
SET logging_id = %s
WHERE processing_job_id = %s"""
qdb.sql_connection.TRN.add(sql, [log.id, self.id])
qdb.sql_connection.TRN.execute()
# All the children should be marked as failure
for c in self.children:
c.complete(False, error="Parent job '%s' failed." % self.id)
# set as error after everything is in place
self._set_status('error', error_msg=error)
@property
def heartbeat(self):
"""The timestamp of the last heartbeat received from the job
Returns
-------
datetime
The last heartbeat timestamp
"""
with qdb.sql_connection.TRN:
sql = """SELECT heartbeat
FROM qiita.processing_job
WHERE processing_job_id = %s"""
qdb.sql_connection.TRN.add(sql, [self.id])
return qdb.sql_connection.TRN.execute_fetchlast()
def update_heartbeat_state(self):
"""Updates the heartbeat of the job
In case that the job is in `queued` status, it changes the status to
`running`.
Raises
------
QiitaDBOperationNotPermittedError
If the job is already completed
"""
with qdb.sql_connection.TRN:
status = self.status
if status == 'queued':
self._set_status('running')
elif status != 'running':
raise qdb.exceptions.QiitaDBOperationNotPermittedError(
"Can't execute heartbeat on job: already completed")
sql = """UPDATE qiita.processing_job
SET heartbeat = %s
WHERE processing_job_id = %s"""
qdb.sql_connection.TRN.add(sql, [datetime.now(), self.id])
qdb.sql_connection.TRN.execute()
@property
def step(self):
"""Returns the current step of the job
Returns
-------
str
The current step of the job
"""
with qdb.sql_connection.TRN:
sql = """SELECT step
FROM qiita.processing_job
WHERE processing_job_id = %s"""
qdb.sql_connection.TRN.add(sql, [self.id])
return qdb.sql_connection.TRN.execute_fetchlast()
@step.setter
def step(self, value):
"""Sets the current step of the job
Parameters
----------
value : str
The new current step of the job
Raises
------
qiita_db.exceptions.QiitaDBOperationNotPermittedError
If the status of the job is not 'running'
"""
if self.status != 'running':
raise qdb.exceptions.QiitaDBOperationNotPermittedError(
"Cannot change the step of a job whose status is not "
"'running'")
sql = """UPDATE qiita.processing_job
SET step = %s
WHERE processing_job_id = %s"""
qdb.sql_connection.perform_as_transaction(sql, [value, self.id])
@property
def children(self):
"""The children jobs
Returns
-------
generator of qiita_db.processing_job.ProcessingJob
The children jobs
"""
with qdb.sql_connection.TRN:
sql = """SELECT child_id
FROM qiita.parent_processing_job
WHERE parent_id = %s"""
qdb.sql_connection.TRN.add(sql, [self.id])
for jid in qdb.sql_connection.TRN.execute_fetchflatten():
yield ProcessingJob(jid)
@property
def validator_jobs(self):
"""The validators of this job
Returns
-------
generator of qiita_db.processing_job.ProcessingJob
The validators of this job
"""
with qdb.sql_connection.TRN:
sql = """SELECT validator_id
FROM qiita.processing_job_validator pjv
JOIN qiita.processing_job pj
ON pjv.validator_id = pj.processing_job_id
JOIN qiita.processing_job_status USING (
processing_job_status_id)
WHERE pjv.processing_job_id = %s"""
qdb.sql_connection.TRN.add(sql, [self.id])
for jid in qdb.sql_connection.TRN.execute_fetchflatten():
yield ProcessingJob(jid)
def _helper_update_children(self, new_map):
ready = []
sql = """SELECT command_parameters, pending
FROM qiita.processing_job
WHERE processing_job_id = %s"""
sql_update = """UPDATE qiita.processing_job
SET command_parameters = %s,
pending = %s
WHERE processing_job_id = %s"""
sql_link = """INSERT INTO qiita.artifact_processing_job
(artifact_id, processing_job_id)
VALUES (%s, %s)"""
for c in self.children:
qdb.sql_connection.TRN.add(sql, [c.id])
params, pending = qdb.sql_connection.TRN.execute_fetchflatten()
for pname, out_name in pending[self.id].items():
a_id = new_map[out_name]
params[pname] = str(a_id)
del pending[self.id]
# Link the input artifact with the child job
qdb.sql_connection.TRN.add(sql_link, [a_id, c.id])
# Force to insert a NULL in the DB if pending is empty
pending = pending if pending else None
qdb.sql_connection.TRN.add(sql_update,
[dumps(params), pending, c.id])
qdb.sql_connection.TRN.execute()
if pending is None:
# The child already has all the parameters
# Add it to the ready list
ready.append(c)
return ready
def _update_children(self, mapping):
"""Updates the children of the current job to populate the input params
Parameters
----------
mapping : dict of {int: int}
The mapping between output parameter and artifact
Returns
-------
list of qiita_db.processing_job.ProcessingJob
The list of childrens that are ready to be submitted
"""
with qdb.sql_connection.TRN:
sql = """SELECT command_output_id, name
FROM qiita.command_output
WHERE command_output_id IN %s"""
sql_args = [tuple(mapping.keys())]
qdb.sql_connection.TRN.add(sql, sql_args)
res = qdb.sql_connection.TRN.execute_fetchindex()
new_map = {name: mapping[oid] for oid, name in res}
return self._helper_update_children(new_map)
def _update_and_launch_children(self, mapping):
"""Updates the children of the current job to populate the input params
Parameters
----------
mapping : dict of {int: int}
The mapping between output parameter and artifact
"""
ready = self._update_children(mapping)
# Submit all the children that already have all the input parameters
for c in ready:
if c.status in {'in_construction', 'waiting'}:
c.submit()
# some jobs create several children jobs/validators and this
# can clog the submission process; giving it a second to
# avoid this
sleep(1)
@property
def outputs(self):
"""The outputs of the job
Returns
-------
dict of {str: qiita_db.artifact.Artifact}
The outputs of the job keyed by output name
"""
with qdb.sql_connection.TRN:
if self.status != 'success':
raise qdb.exceptions.QiitaDBOperationNotPermittedError(
"Can't return the outputs of a non-success job")
sql = """SELECT artifact_id, name
FROM qiita.artifact_output_processing_job
JOIN qiita.command_output USING (command_output_id)
WHERE processing_job_id = %s"""
qdb.sql_connection.TRN.add(sql, [self.id])
return {
name: qdb.artifact.Artifact(aid)
for aid, name in qdb.sql_connection.TRN.execute_fetchindex()}
@property
def processing_job_workflow(self):
"""The processing job workflow
Returns
-------
ProcessingWorkflow
The processing job workflow the job
"""
with qdb.sql_connection.TRN:
# Retrieve the workflow root jobs
sql = """SELECT get_processing_workflow_roots
FROM qiita.get_processing_workflow_roots(%s)"""
qdb.sql_connection.TRN.add(sql, [self.id])
res = qdb.sql_connection.TRN.execute_fetchindex()
if res:
sql = """SELECT processing_job_workflow_id
FROM qiita.processing_job_workflow_root
WHERE processing_job_id = %s"""
qdb.sql_connection.TRN.add(sql, [res[0][0]])
r = qdb.sql_connection.TRN.execute_fetchindex()
return (qdb.processing_job.ProcessingWorkflow(r[0][0]) if r
else None)
else:
return None
@property
def pending(self):
"""A dictionary with the information about the predecessor jobs
Returns
-------
dict
A dict with {job_id: {parameter_name: output_name}}"""
with qdb.sql_connection.TRN:
sql = """SELECT pending
FROM qiita.processing_job
WHERE processing_job_id = %s"""
qdb.sql_connection.TRN.add(sql, [self.id])
res = qdb.sql_connection.TRN.execute_fetchlast()
return res if res is not None else {}
@property
def hidden(self):
"""Whether the job is hidden or not
Returns
-------
bool
Whether the jobs is hidden or not
"""
with qdb.sql_connection.TRN:
sql = """SELECT hidden
FROM qiita.processing_job
WHERE processing_job_id = %s"""
qdb.sql_connection.TRN.add(sql, [self.id])
return qdb.sql_connection.TRN.execute_fetchlast()
def hide(self):
"""Hides the job from the user
Raises
------
QiitaDBOperationNotPermittedError
If the job is not in the error status
"""
with qdb.sql_connection.TRN:
status = self.status
if status != 'error':
raise qdb.exceptions.QiitaDBOperationNotPermittedError(
'Only jobs in error status can be hidden. Current status: '
'%s' % status)
sql = """UPDATE qiita.processing_job
SET hidden = %s
WHERE processing_job_id = %s"""
qdb.sql_connection.TRN.add(sql, [True, self.id])
qdb.sql_connection.TRN.execute()
@property
def shape(self):
"""Number of samples, metadata columns and input size of this job
Returns
-------
int, int, int
Number of samples, metadata columns and input size. None means it
couldn't be calculated
"""
samples = None
columns = None
prep_info = None
study_id = None
analysis_id = None
artifact = None
input_size = None
parameters = self.parameters.values
QUIDError = qdb.exceptions.QiitaDBUnknownIDError
if self.command.name == 'Validate':
# Validate only has two options to calculate it's size: template (a
# job that has a preparation linked) or analysis (is from an
# analysis). However, 'template' can be present and be None
if 'template' in parameters and parameters['template'] is not None:
try:
PT = qdb.metadata_template.prep_template.PrepTemplate
prep_info = PT(parameters['template'])
except QUIDError:
pass
else:
study_id = prep_info.study_id
elif 'analysis' in parameters:
analysis_id = parameters['analysis']
elif self.command.name == 'build_analysis_files':
# build analysis is a special case because the analysis doesn't
# exist yet
sanalysis = qdb.analysis.Analysis(parameters['analysis']).samples
samples = sum([len(sams) for sams in sanalysis.values()])
# only count the biom files
input_size = sum([fp['fp_size'] for aid in sanalysis
for fp in qdb.artifact.Artifact(aid).filepaths
if fp['fp_type'] == 'biom'])
columns = self.parameters.values['categories']
if columns is not None:
columns = len(columns)
elif self.command.software.name == 'Qiita':
if self.command.name == 'delete_sample_or_column':
MT = qdb.metadata_template
_id = parameters['obj_id']
try:
if parameters['obj_class'] == 'SampleTemplate':
obj = MT.sample_template.SampleTemplate(_id)
else:
obj = MT.prep_template.PrepTemplate(_id)
samples = len(obj)
except QUIDError:
pass
else:
if 'study' in parameters:
study_id = parameters['study']
elif 'study_id' in parameters:
study_id = parameters['study_id']
elif 'analysis' in parameters:
analysis_id = parameters['analysis']
elif 'analysis_id' in parameters:
analysis_id = parameters['analysis_id']
elif 'artifact' in parameters:
try:
artifact = qdb.artifact.Artifact(
parameters['artifact'])
except QUIDError:
pass
elif self.command.name == 'Sequence Processing Pipeline':
body = self.parameters.values['sample_sheet']['body']
samples = body.count('\r')
stemp = body.count('\n')
if stemp > samples:
samples = stemp
elif self.input_artifacts:
artifact = self.input_artifacts[0]
if artifact.artifact_type == 'BIOM':
input_size = sum([fp['fp_size'] for a in self.input_artifacts
for fp in a.filepaths
if fp['fp_type'] == 'biom'])
else:
input_size = sum([fp['fp_size'] for a in self.input_artifacts
for fp in a.filepaths])
# if there is an artifact, then we need to get the study_id/analysis_id
if artifact is not None:
if artifact.study is not None:
# only count samples in the prep template
prep_info = artifact.prep_templates[0]
study_id = prep_info.study_id
elif artifact.analysis is not None:
analysis_id = artifact.analysis.id
# now retrieve the sample/columns based on study_id/analysis_id
if study_id is not None:
try:
st = qdb.study.Study(study_id).sample_template
except QUIDError:
pass
else:
if prep_info is not None:
samples = len(prep_info)
columns = len(prep_info.categories) + len(st.categories)
elif st is not None:
samples = len(st)
columns = len(st.categories)
elif analysis_id is not None:
try:
analysis = qdb.analysis.Analysis(analysis_id)
except qdb.exceptions.QiitaDBUnknownIDError:
pass
else:
mfp = qdb.util.get_filepath_information(
analysis.mapping_file)['fullpath']
samples, columns = pd.read_csv(
mfp, sep='\t', dtype=str).shape
input_size = sum([fp['fp_size'] for aid in analysis.samples for
fp in qdb.artifact.Artifact(aid).filepaths])
return samples, columns, input_size
@property
def complete_processing_job(self):
sql = """SELECT processing_job_id FROM qiita.software_command
JOIN qiita.processing_job USING (command_id)
WHERE name = 'complete_job' AND
command_parameters->>'job_id' = %s LIMIT 1"""
with qdb.sql_connection.TRN:
qdb.sql_connection.TRN.add(sql, [self.id])
result = qdb.sql_connection.TRN.execute_fetchflatten()
if result:
return qdb.processing_job.ProcessingJob(result[0])
return None
@property
def trace(self):
""" Returns as a text array the full trace of the job, from itself
to validators and complete jobs"""
lines = [f'{self.id} [{self.external_id}] ({self.status}): '
f'{self.command.name} | {self.resource_allocation_info}']
cjob = self.complete_processing_job
if cjob is not None:
lines.append(f' {cjob.id} [{cjob.external_id}] ({cjob.status})| '
f'{cjob.resource_allocation_info}')
vjob = self.release_validator_job
if vjob is not None:
lines.append(f' {vjob.id} [{vjob.external_id}] '
f' ({vjob.status}) | '
f'{vjob.resource_allocation_info}')
for v in self.validator_jobs:
lines.append(f' {v.id} [{v.external_id}] ({v.status}): '
f'{v.command.name} | {v.resource_allocation_info}')
cjob = v.complete_processing_job
if cjob is not None:
lines.append(f' {cjob.id} [{cjob.external_id}] '
f'({cjob.status}) | '
f'{cjob.resource_allocation_info}')
return lines
class ProcessingWorkflow(qdb.base.QiitaObject):
"""Models a workflow defined by the user
Parameters
----------
user : qiita_db.user.User
The user that modeled the workflow
root : list of qiita_db.processing_job.ProcessingJob
The first job in the workflow
"""
_table = "processing_job_workflow"
@classmethod
def _common_creation_steps(cls, user, root_jobs, name=None):
"""Executes the common creation steps
Parameters
----------
user : qiita_db.user.User
The user creating the workflow
root_jobs : list of qiita_db.processing_job.ProcessingJob
The root jobs of the workflow
name : str, optional
The name of the workflow. Default: generated from user's name
"""
with qdb.sql_connection.TRN:
# Insert the workflow in the processing_job_workflow table
name = name if name else "%s's workflow" % user.info['name']
sql = """INSERT INTO qiita.processing_job_workflow (email, name)
VALUES (%s, %s)
RETURNING processing_job_workflow_id"""
qdb.sql_connection.TRN.add(sql, [user.email, name])
w_id = qdb.sql_connection.TRN.execute_fetchlast()
# Connect the workflow with it's initial set of jobs
sql = """INSERT INTO qiita.processing_job_workflow_root
(processing_job_workflow_id, processing_job_id)
VALUES (%s, %s)"""
sql_args = [[w_id, j.id] for j in root_jobs]
qdb.sql_connection.TRN.add(sql, sql_args, many=True)
qdb.sql_connection.TRN.execute()
return cls(w_id)
@classmethod
def from_default_workflow(cls, user, dflt_wf, req_params, name=None,
force=False):
"""Creates a new processing workflow from a default workflow
Parameters
----------
user : qiita_db.user.User
The user creating the workflow
dflt_wf : qiita_db.software.DefaultWorkflow
The default workflow
req_params : dict of {qdb.software.Command: dict of {str: object}}
The required parameters values for the source commands in the
workflow, keyed by command. The inner dicts are keyed by
parameter name.
name : str, optional
Name of the workflow. Default: generated from user's name
force : bool
Force creation on duplicated parameters
Returns
-------
qiita_db.processing_job.ProcessingWorkflow
The newly created workflow
"""
with qdb.sql_connection.TRN:
dflt_g = dflt_wf.graph
# Find the roots of the workflow. That is, the nodes that do not
# have a parent in the graph (in_degree = 0)
in_degrees = dflt_g.in_degree()
# We can potentially access this information from the nodes
# multiple times, so caching in here
# [0] in_degrees returns a tuple, where [0] is the element we want
all_nodes = {}
roots = {}
for node, position in in_degrees:
dp = node.default_parameter
cmd = dp.command
if position == 0:
roots[node] = (cmd, dp)
all_nodes[node] = (cmd, dp)
# Check that we have all the required parameters
root_cmds = set(c for c, _ in roots.values())
if root_cmds != set(req_params):
error_msg = ['Provided required parameters do not match the '
'initial set of commands for the workflow.']
missing = [c.name for c in root_cmds - set(req_params)]
if missing:
error_msg.append(
' Command(s) "%s" are missing the required parameter '
'set.' % ', '.join(missing))
extra = [c.name for c in set(req_params) - root_cmds]
if extra:
error_msg.append(
' Paramters for command(s) "%s" have been provided, '
'but they are not the initial commands for the '
'workflow.' % ', '.join(extra))
raise qdb.exceptions.QiitaDBError(''.join(error_msg))
# Start creating the root jobs
node_to_job = {
n: ProcessingJob.create(
user,
qdb.software.Parameters.from_default_params(
p, req_params[c]), force)
for n, (c, p) in roots.items()}
root_jobs = node_to_job.values()
# SQL used to create the edges between jobs
sql = """INSERT INTO qiita.parent_processing_job
(parent_id, child_id)
VALUES (%s, %s)"""
# Create the rest of the jobs. These are different form the root
# jobs because they depend on other jobs to complete in order to be
# submitted
for n in nx.topological_sort(dflt_g):
if n in node_to_job:
# We have already visited this node
# (because it is a root node)
continue
cmd, dflt_params = all_nodes[n]
job_req_params = {}
parent_ids = []
# Each incoming edge represents an artifact that is generated
# by the source job of the edge
for source, dest, data in dflt_g.in_edges(n, data=True):
# Retrieve the id of the parent job - it already exists
# because we are visiting the nodes in topological order
source_id = node_to_job[source].id
parent_ids.append(source_id)
# Get the connections between the job and the source
connections = data['connections'].connections
for out, in_param, _ in connections:
# We take advantage of the fact the parameters are
# stored in JSON to encode the name of the output
# artifact from the previous job
job_req_params[in_param] = [source_id, out]
# At this point we should have all the requried parameters for
# the current job, so create it
new_job = ProcessingJob.create(
user, qdb.software.Parameters.from_default_params(
dflt_params, job_req_params), force)
node_to_job[n] = new_job
# Create the parent-child links in the DB
sql_args = [[pid, new_job.id] for pid in parent_ids]
qdb.sql_connection.TRN.add(sql, sql_args, many=True)
return cls._common_creation_steps(user, root_jobs, name)
@classmethod
def from_scratch(cls, user, parameters, name=None, force=False):
"""Creates a new processing workflow from scratch
Parameters
----------
user : qiita_db.user.User
The user creating the workflow
parameters : qiita_db.software.Parameters
The parameters of the first job in the workflow
name : str, optional
Name of the workflow. Default: generated from user's name
force : bool
Force creation on duplicated parameters
Returns
-------
qiita_db.processing_job.ProcessingWorkflow
The newly created workflow
"""
job = ProcessingJob.create(user, parameters, force)
return cls._common_creation_steps(user, [job], name)
@property
def name(self):
""""The name of the workflow
Returns
-------
str
The name of the workflow
"""
with qdb.sql_connection.TRN:
sql = """SELECT name
FROM qiita.processing_job_workflow
WHERE processing_job_workflow_id = %s"""
qdb.sql_connection.TRN.add(sql, [self.id])
return qdb.sql_connection.TRN.execute_fetchlast()
@property
def user(self):
"""The user that created the workflow
Returns
-------
qdb.user.User
The user that created the workflow
"""
with qdb.sql_connection.TRN:
sql = """SELECT email
FROM qiita.processing_job_workflow
WHERE processing_job_workflow_id = %s"""
qdb.sql_connection.TRN.add(sql, [self.id])
email = qdb.sql_connection.TRN.execute_fetchlast()
return qdb.user.User(email)
@property
def graph(self):
"""Returns the graph of jobs that represent the workflow
Returns
-------
networkx.DiGraph
The graph representing the workflow
"""
g = nx.DiGraph()
with qdb.sql_connection.TRN:
# Retrieve all graph workflow nodes
sql = """SELECT parent_id, child_id
FROM qiita.get_processing_workflow_edges(%s)"""
qdb.sql_connection.TRN.add(sql, [self.id])
edges = qdb.sql_connection.TRN.execute_fetchindex()
nodes = {}
if edges:
nodes = {jid: ProcessingJob(jid)
for jid in set(chain.from_iterable(edges))}
edges = [(nodes[s], nodes[d]) for s, d in edges]
g.add_edges_from(edges)
# It is possible that there are root jobs that doesn't have any
# child, so they do not appear on edge list
sql = """SELECT processing_job_id
FROM qiita.processing_job_workflow_root
WHERE processing_job_workflow_id = %s"""
sql_args = [self.id]
if nodes:
sql += " AND processing_job_id NOT IN %s"
sql_args.append(tuple(nodes))
qdb.sql_connection.TRN.add(sql, sql_args)
nodes = [
ProcessingJob(jid)
for jid in qdb.sql_connection.TRN.execute_fetchflatten()]
g.add_nodes_from(nodes)
return g
def _raise_if_not_in_construction(self):
"""Raises an error if the workflow is not in construction
Raises
------
qiita_db.exceptions.QiitaDBOperationNotPermittedError
If the workflow is not in construction
"""
with qdb.sql_connection.TRN:
# To know if the workflow is in construction or not it suffices
# to look at the status of the root jobs
sql = """SELECT DISTINCT processing_job_status
FROM qiita.processing_job_workflow_root
JOIN qiita.processing_job USING (processing_job_id)
JOIN qiita.processing_job_status
USING (processing_job_status_id)
WHERE processing_job_workflow_id = %s"""
qdb.sql_connection.TRN.add(sql, [self.id])
res = qdb.sql_connection.TRN.execute_fetchflatten()
# If the above SQL query returns a single element and the value
# is different from in construction, it means that all the jobs
# in the workflow are in the same status and it is not
# 'in_construction', hence raise the error. If the above SQL query
# returns more than value (len(res) > 1) it means that the workflow
# is no longer in construction cause some jobs have been submited
# for processing. Note that if the above query doesn't retrun any
# value, it means that no jobs are in the workflow and that means
# that the workflow is in construction.
if (len(res) == 1 and res[0] != 'in_construction') or len(res) > 1:
# The workflow is no longer in construction, raise an error
raise qdb.exceptions.QiitaDBOperationNotPermittedError(
"Workflow not in construction")
def add(self, dflt_params, connections=None, req_params=None,
opt_params=None, force=False):
"""Adds a new job to the workflow
Parameters
----------
dflt_params : qiita_db.software.DefaultParameters
The DefaultParameters object used
connections : dict of {qiita_db.processing_job.ProcessingJob:
{str: str}}, optional
Dictionary keyed by the jobs in which the new job depends on,
and values is a dict mapping between source outputs and new job
inputs
req_params : dict of {str: object}, optional
Any extra required parameter values, keyed by parameter name.
Default: None, all the requried parameters are provided through
the `connections` dictionary
opt_params : dict of {str: object}, optional
The optional parameters to change from the default set, keyed by
parameter name. Default: None, use the values in `dflt_params`
force : bool
Force creation on duplicated parameters
Raises
------
qiita_db.exceptions.QiitaDBOperationNotPermittedError
If the workflow is not in construction
"""
with qdb.sql_connection.TRN:
self._raise_if_not_in_construction()
# checking that the new number of artifacts is not above
# max_artifacts_in_workflow
current_artifacts = sum(
[len(j.command.outputs) for j in self.graph.nodes()])
to_add_artifacts = len(dflt_params.command.outputs)
total_artifacts = current_artifacts + to_add_artifacts
max_artifacts = qdb.util.max_artifacts_in_workflow()
if total_artifacts > max_artifacts:
raise ValueError(
"Cannot add new job because it will create more "
f"artifacts (current: {current_artifacts} + new: "
f"{to_add_artifacts} = {total_artifacts}) that what is "
f"allowed in a single workflow ({max_artifacts})")
if connections:
# The new Job depends on previous jobs in the workflow
req_params = req_params if req_params else {}
# Loop through all the connections to add the relevant
# parameters
for source, mapping in connections.items():
source_id = source.id
for out, in_param in mapping.items():
req_params[in_param] = [source_id, out]
new_job = ProcessingJob.create(
self.user, qdb.software.Parameters.from_default_params(
dflt_params, req_params, opt_params=opt_params), force)
# SQL used to create the edges between jobs
sql = """INSERT INTO qiita.parent_processing_job
(parent_id, child_id)
VALUES (%s, %s)"""
sql_args = [[s.id, new_job.id] for s in connections]
qdb.sql_connection.TRN.add(sql, sql_args, many=True)
qdb.sql_connection.TRN.execute()
else:
# The new job doesn't depend on any previous job in the
# workflow, so it is a new root job
new_job = ProcessingJob.create(
self.user, qdb.software.Parameters.from_default_params(
dflt_params, req_params, opt_params=opt_params), force)
sql = """INSERT INTO qiita.processing_job_workflow_root
(processing_job_workflow_id, processing_job_id)
VALUES (%s, %s)"""
sql_args = [self.id, new_job.id]
qdb.sql_connection.TRN.add(sql, sql_args)
qdb.sql_connection.TRN.execute()
return new_job
def remove(self, job, cascade=False):
"""Removes a given job from the workflow
Parameters
----------
job : qiita_db.processing_job.ProcessingJob
The job to be removed
cascade : bool, optional
If true, remove the also the input job's children. Default: False.
Raises
------
qiita_db.exceptions.QiitaDBOperationNotPermittedError
If the workflow is not in construction
If the job to be removed has children and `cascade` is `False`
"""
with qdb.sql_connection.TRN:
self._raise_if_not_in_construction()
# Check if the given job has children
children = list(job.children)
if children:
if not cascade:
raise qdb.exceptions.QiitaDBOperationNotPermittedError(
"Can't remove job '%s': it has children" % job.id)
else:
# We need to remove all job's children, remove them first
# and then remove the current job
for c in children:
self.remove(c, cascade=True)
# Remove any edges (it can only appear as a child)
sql = """DELETE FROM qiita.parent_processing_job
WHERE child_id = %s"""
qdb.sql_connection.TRN.add(sql, [job.id])
# Remove as root job
sql = """DELETE FROM qiita.processing_job_workflow_root
WHERE processing_job_id = %s"""
qdb.sql_connection.TRN.add(sql, [job.id])
# Remove the input reference
sql = """DELETE FROM qiita.artifact_processing_job
WHERE processing_job_id = %s"""
qdb.sql_connection.TRN.add(sql, [job.id])
# Remove the job
sql = """DELETE FROM qiita.processing_job
WHERE processing_job_id = %s"""
qdb.sql_connection.TRN.add(sql, [job.id])
qdb.sql_connection.TRN.execute()
def submit(self):
"""Submits the workflow to execution
Raises
------
qiita_db.exceptions.QiitaDBOperationNotPermittedError
If the workflow is not in construction
"""
with qdb.sql_connection.TRN:
self._raise_if_not_in_construction()
g = self.graph
# In order to avoid potential race conditions, we are going to set
# all the children in 'waiting' status before submitting
# the root nodes
in_degrees = dict(g.in_degree())
roots = []
for job, degree in in_degrees.items():
if degree == 0:
roots.append(job)
else:
job._set_status('waiting')
for job in roots:
job.submit()