- Timestamp:
- May 3, 2010, 8:50:52 AM (16 years ago)
- Location:
- branches/simtest_nebulous_branches
- Files:
-
- 3 edited
-
. (modified) (1 prop)
-
ippTools/src (modified) (1 prop)
-
ippTools/src/pstamptool.c (modified) (31 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/simtest_nebulous_branches
- Property svn:mergeinfo changed
-
branches/simtest_nebulous_branches/ippTools/src
- Property svn:ignore
-
old new 1 *.la 2 *.lo 1 3 .deps 2 4 .gdb_history … … 4 6 Makefile 5 7 Makefile.in 8 addtool 9 caltool 10 camtool 11 chiptool 6 12 config.h 7 13 config.h.in 8 stamp-h1 9 *.la 10 *.lo 14 detselect 15 dettool 16 difftool 17 disttool 18 dqstatstool 19 faketool 20 flatcorr 21 guidetool 22 magicdstool 23 magictool 24 pstamptool 25 pubtool 26 pxadmin 27 pxdata.c 28 pxinject 11 29 pxtoolsErrorCodes.c 12 30 pxtoolsErrorCodes.h 13 pxadmin14 pxinject15 pztool16 31 pzgetexp 17 32 pzgetimfiles 33 pztool 34 receivetool 18 35 regtool 19 guidetool 20 chiptool 21 camtool 36 stacktool 37 stamp-h1 22 38 warptool 23 difftool24 stacktool25 faketool26 dettool27 detselect28 pxdata.c29 magictool30 magicdstool31 caltool32 flatcorr33 pstamptool34 disttool35 receivetool36 37 pubtool
-
- Property svn:ignore
-
branches/simtest_nebulous_branches/ippTools/src/pstamptool.c
r25054 r27840 44 44 static bool pendingjobMode(pxConfig *config); 45 45 static bool updatejobMode(pxConfig *config); 46 static bool revertjobMode(pxConfig *config); 46 47 static bool addprojectMode(pxConfig *config); 47 48 static bool projectMode(pxConfig *config); 48 49 static bool modprojectMode(pxConfig *config); 50 static bool getdependentMode(pxConfig *config); 51 static bool pendingdependentMode(pxConfig *config); 52 static bool updatedependentMode(pxConfig *config); 53 static bool revertdependentMode(pxConfig *config); 49 54 50 55 # define MODECASE(caseName, func) \ … … 80 85 MODECASE(PSTAMPTOOL_MODE_PENDINGJOB, pendingjobMode); 81 86 MODECASE(PSTAMPTOOL_MODE_UPDATEJOB, updatejobMode); 87 MODECASE(PSTAMPTOOL_MODE_REVERTJOB, revertjobMode); 82 88 MODECASE(PSTAMPTOOL_MODE_ADDPROJECT, addprojectMode); 83 89 MODECASE(PSTAMPTOOL_MODE_MODPROJECT, modprojectMode); 84 90 MODECASE(PSTAMPTOOL_MODE_PROJECT, projectMode); 91 MODECASE(PSTAMPTOOL_MODE_GETDEPENDENT, getdependentMode); 92 MODECASE(PSTAMPTOOL_MODE_PENDINGDEPENDENT, pendingdependentMode); 93 MODECASE(PSTAMPTOOL_MODE_UPDATEDEPENDENT, updatedependentMode); 94 MODECASE(PSTAMPTOOL_MODE_REVERTDEPENDENT, revertdependentMode); 85 95 default: 86 96 psAbort("invalid option (this should not happen)"); … … 108 118 PS_ASSERT_PTR_NON_NULL(config, false); 109 119 110 PXOPT_LOOKUP_STR(uri, config->args, "-uri", true, false); 111 PXOPT_LOOKUP_STR(outProduct, config->args, "-out_product", true, false); 112 PXOPT_LOOKUP_STR(lastFileset, config->args, "-last_fileset", false, false); 113 PXOPT_LOOKUP_STR(state, config->args, "-state", false, false); 120 PXOPT_LOOKUP_STR(uri, config->args, "-set_uri", true, false); 121 PXOPT_LOOKUP_STR(outProduct, config->args, "-set_out_product", true, false); 122 PXOPT_LOOKUP_STR(lastFileset, config->args, "-set_last_fileset", false, false); 123 PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, false); 124 PXOPT_LOOKUP_STR(label, config->args, "-set_label", false, false); 125 PXOPT_LOOKUP_S32(pollInterval, config->args, "-set_poll_interval",false, false); 114 126 115 127 if (!pstampDataStoreInsert(config->dbh, … … 117 129 state, 118 130 lastFileset, 131 NULL, // timestamp 132 label, // label 119 133 outProduct, 120 uri 134 uri, 135 pollInterval 121 136 )) { 122 137 psError(PS_ERR_UNKNOWN, false, "database error"); … … 134 149 PXOPT_COPY_S64(config->args, where, "-ds_id", "ds_id", "=="); 135 150 151 PXOPT_LOOKUP_BOOL(ready, config->args, "-ready", false); 136 152 PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false); 137 153 138 154 psString query = pxDataGet("pstamptool_datastore.sql"); 139 155 if (!query) { 140 psError(PXTOOLS_ERR_ DATA, false, "failed to retreive SQL statement");156 psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement"); 141 157 return false; 142 158 } … … 148 164 } 149 165 psFree(where); 166 if (ready) { 167 psStringAppend(&query, " %s", "\nAND TIMESTAMPDIFF(SECOND, timestamp, now()) > pollInterval"); 168 } 150 169 151 170 if (!p_psDBRunQuery(config->dbh, query)) { … … 181 200 182 201 PXOPT_LOOKUP_S64(ds_id, config->args, "-ds_id", true, false); 183 PXOPT_LOOKUP_STR(lastFileset, config->args, "-last_fileset", false, false); 184 PXOPT_LOOKUP_STR(state, config->args, "-state", false, false); 185 186 if (!state && !lastFileset) { 187 psError(PS_ERR_UNKNOWN, true, "at least one of -last_fileset or -state is required"); 188 return false; 189 } 190 191 char *query = psStringCopy ("UPDATE pstampDataStore SET"); 192 bool needComma = false; 193 202 PXOPT_LOOKUP_STR(lastFileset, config->args, "-set_last_fileset", false, false); 203 PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, false); 204 PXOPT_LOOKUP_STR(label, config->args, "-set_label", false, false); 205 PXOPT_LOOKUP_S32(pollInterval, config->args, "-set_poll_interval", false, false); 206 PXOPT_LOOKUP_BOOL(update_timestamp, config->args, "-update_timestamp", false); 207 208 if (!state && !lastFileset && !pollInterval && !update_timestamp && !label) { 209 psError(PS_ERR_UNKNOWN, true, "at least one of -last_fileset or -set_state is required"); 210 return false; 211 } 212 213 char *query = psStringCopy ("UPDATE pstampDataStore SET timestamp = CURRENT_TIMESTAMP() "); 214 194 215 if (lastFileset) { 195 psStringAppend(&query, " lastFileset = '%s'", lastFileset); 196 needComma = true; 216 psStringAppend(&query, " , lastFileset = '%s'", lastFileset); 197 217 } 198 218 199 219 if (state) { 200 psStringAppend(&query, "%s state = '%s'", needComma ? "," : " ", state); 201 needComma = true; // be ready in case we add another field 202 } 203 220 psStringAppend(&query, ", state = '%s'", state); 221 } 222 223 if (label) { 224 psStringAppend(&query, ", label = '%s'", label); 225 } 226 227 if (pollInterval) { 228 psStringAppend(&query, ", pollInterval = %d", pollInterval); 229 } 230 204 231 psStringAppend(&query, " WHERE ds_id = %" PRId64, ds_id); 205 232 … … 212 239 psU64 affected = psDBAffectedRows(config->dbh); 213 240 if (affected != 1) { 214 psError(PS_ERR_UNKNOWN, false, "should have affected one row but %" 241 psError(PS_ERR_UNKNOWN, false, "should have affected one row but %" 215 242 PRIu64 " rows were modified", affected); 216 243 return false; … … 224 251 PS_ASSERT_PTR_NON_NULL(config, false); 225 252 226 PXOPT_LOOKUP_STR(uri, config->args, "-uri", true, false);227 // PXOPT_LOOKUP_STR(outFileset, config->args, "-out_fileset", true, false);228 PXOPT_LOOKUP_S 64(ds_id, config->args, "-ds_id",false, false);229 230 char *query ="INSERT INTO pstampRequest" 231 " (state, uri, ds_id, fault)"232 " VALUES( 'new', '%s', %" PRId64 ", 0 )";233 if (!p_psDBRunQueryF(config->dbh, query, uri, ds_id)) {234 psError(PS_ERR_UNKNOWN, false, "database error");235 return false;236 }237 238 psU64 affected = psDBAffectedRows(config->dbh);239 if (affected != 1) {240 psError(PS_ERR_UNKNOWN, false,241 "should have affected one row but %" PRIu64 " rows were modified",242 affected);253 PXOPT_LOOKUP_STR(uri, config->args, "-uri", true, false); 254 PXOPT_LOOKUP_STR(name, config->args, "-name", false, false); 255 PXOPT_LOOKUP_STR(label, config->args, "-label", false, false); 256 PXOPT_LOOKUP_S64(ds_id, config->args, "-ds_id", false, false); 257 258 if (!pstampRequestInsert(config->dbh, 259 0, // req_id 260 ds_id, 261 "new", //state 262 name, 263 NULL, // reqType 264 label, 265 NULL, // outProduct 266 uri, 267 0 // fault 268 )) { 269 psError(PS_ERR_UNKNOWN, false, "failed to insert request"); 243 270 return false; 244 271 } … … 257 284 psMetadata *where = psMetadataAlloc(); 258 285 PXOPT_COPY_S64(config->args, where, "-req_id", "req_id", "=="); 286 pxAddLabelSearchArgs(config, where, "-label", "label", "LIKE"); 259 287 260 288 PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false); … … 263 291 psString query = pxDataGet("pstamptool_pendingreq.sql"); 264 292 if (!query) { 265 psError(PXTOOLS_ERR_ DATA, false, "failed to retreive SQL statement");293 psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement"); 266 294 return false; 267 295 } … … 315 343 PS_ASSERT_PTR_NON_NULL(config, false); 316 344 317 PXOPT_LOOKUP_S64(req_id, config->args, "-req_id", true, false); 345 psMetadata *where = psMetadataAlloc(); 346 PXOPT_COPY_S64(config->args, where, "-req_id", "req_id", "=="); 347 PXOPT_COPY_S64(config->args, where, "-not_req_id", "req_id", "!="); 348 PXOPT_COPY_STR(config->args, where, "-name", "name", "=="); 349 318 350 PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false); 319 351 PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false); 320 352 321 psString query = NULL; 322 psStringAppend(&query, "SELECT * from pstampRequest WHERE req_id = %" PRId64, req_id); 323 353 if (!psListLength(where->list)) { 354 psError(PS_ERR_UNKNOWN, true, "-req_id or -name must be supplied"); 355 return false; 356 } 357 358 psString query = psStringCopy("SELECT * from pstampRequest"); 359 360 psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); 361 psStringAppend(&query, " WHERE %s", whereClause); 362 psFree(whereClause); 363 psFree(where); 364 324 365 // treat limit == 0 as "no limit" 325 366 if (limit) { … … 342 383 } 343 384 if (!psArrayLength(output)) { 344 psTrace("pstamptool", PS_LOG_INFO, "no rows found"); 345 psFree(output); 346 return true; 385 psTrace("pstamptool", PS_LOG_INFO, "request not found"); 386 // This causes main to exit with PS_EXIT_DATA_ERROR which the script is looking for 387 psError(PXTOOLS_ERR_CONFIG, true, "request not found"); 388 psFree(output); 389 // we return false so that the caller can determine that a request does not exist 390 return false; 347 391 } 348 392 … … 363 407 PS_ASSERT_PTR_NON_NULL(config, false); 364 408 409 psMetadata *where = psMetadataAlloc(); 410 pxAddLabelSearchArgs(config, where, "-label", "label", "LIKE"); 411 365 412 PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false); 366 413 PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false); 367 414 368 psString query = 369 "SELECT * FROM pstampRequest WHERE state = 'run' AND " 370 "(SELECT count(*) FROM pstampJob WHERE pstampJob.req_id = pstampRequest.req_id " 371 " AND pstampJob.state != 'stop') = 0" ; 415 psString query = pxDataGet("pstamptool_completedreq.sql"); 416 417 if (psListLength(where->list)) { 418 psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); 419 psStringAppend(&query, " AND %s", whereClause); 420 psFree(whereClause); 421 } 422 psFree(where); 372 423 373 424 // treat limit == 0 as "no limit" … … 415 466 PXOPT_LOOKUP_STR(state, config->args, "-state", false, false); 416 467 PXOPT_LOOKUP_STR(outProduct, config->args, "-outProduct", false, false); 417 PXOPT_LOOKUP_S TR(fault,config->args, "-fault", false, false);418 PXOPT_LOOKUP_STR(uri, config->args, "-uri", false, false);419 PXOPT_LOOKUP_STR(name, config->args, "-name", false, false);468 PXOPT_LOOKUP_S16(fault, config->args, "-fault", false, false); 469 PXOPT_LOOKUP_STR(uri, config->args, "-uri", false, false); 470 PXOPT_LOOKUP_STR(name, config->args, "-name", false, false); 420 471 PXOPT_LOOKUP_STR(reqType, config->args, "-reqType", false, false); 421 472 … … 433 484 } 434 485 if (fault) { 435 psStringAppend(&query, "%c fault = % s", c, fault);486 psStringAppend(&query, "%c fault = %d", c, fault); 436 487 c = ','; 437 488 } … … 464 515 // note zero is not an error 465 516 if (affected > 1) { 466 psError(PS_ERR_UNKNOWN, false, "should have affected one row but %" 517 psError(PS_ERR_UNKNOWN, false, "should have affected one row but %" 467 518 PRIu64 " rows were modified", affected); 468 519 return false; … … 476 527 PS_ASSERT_PTR_NON_NULL(config, false); 477 528 478 PXOPT_LOOKUP_S64(req_id, config->args, "-req_id", true, false); 479 480 // printf("Revert request %" PRId64 "\n", req_id); 481 482 if (!p_psDBRunQueryF(config->dbh, "DELETE FROM pstampJob where req_id = %" PRId64, req_id)) { 483 psError(PS_ERR_UNKNOWN, false, "database error"); 484 return false; 485 } 486 if (!p_psDBRunQueryF(config->dbh, 487 "UPDATE pstampRequest set state ='new', name=NULL, reqType=NULL, fault=0 where req_id = %" PRId64, req_id)) { 488 psError(PS_ERR_UNKNOWN, false, "database error"); 489 return false; 490 } 529 psMetadata *where = psMetadataAlloc(); 530 531 PXOPT_COPY_S64(config->args, where, "-req_id", "req_id", "=="); 532 PXOPT_COPY_S64(config->args, where, "-fault", "fault", "=="); 533 PXOPT_COPY_STR(config->args, where, "-state", "pstampRequest.state", "=="); 534 pxAddLabelSearchArgs(config, where, "-label", "pstampRequest.label", "LIKE"); 535 536 if (!psListLength(where->list)) { 537 psFree(where); 538 psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required"); 539 return false; 540 } 541 542 psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); 543 psFree(where); 544 545 // delete any jobs that were queued by requests that didn't complete parsing (pstampRequest.state = 'new' 546 // If state = 'run' was supplied this will be a no-op 547 psString query = pxDataGet("pstamptool_revertreq_deletejobs.sql"); 548 psStringAppend(&query, " AND %s", whereClause); 549 if (!p_psDBRunQuery(config->dbh, query)) { 550 psError(PS_ERR_UNKNOWN, false, "database error"); 551 return false; 552 } 553 psFree(query); 554 555 // clear fault for requests 556 query = pxDataGet("pstamptool_revertreq.sql"); 557 psStringAppend(&query, " AND %s", whereClause); 558 559 psFree(whereClause); 560 561 if (!p_psDBRunQuery(config->dbh, query)) { 562 psError(PS_ERR_UNKNOWN, false, "database error"); 563 return false; 564 } 565 psFree(query); 491 566 492 567 return true; … … 506 581 PXOPT_LOOKUP_S16(fault, config->args, "-fault", false, false); 507 582 PXOPT_LOOKUP_S64(exp_id, config->args, "-exp_id", false, false); 583 PXOPT_LOOKUP_S64(options, config->args, "-options", false, false); 584 PXOPT_LOOKUP_S64(dep_id, config->args, "-dep_id", false, false); 508 585 509 586 // unless the job is being inserted with stop state require outputBase 510 587 if (strcmp(stateString, "stop") && !outputBase) { 511 psError(PS_ERR_UNKNOWN, true, "-outputBase is required");512 return false;588 psError(PS_ERR_UNKNOWN, true, "-outputBase is required"); 589 return false; 513 590 } 514 591 515 592 // default value for job_type is defined in pstamptoolConfig.c 516 if (!strcmp(job_type, "get_image") || !strcmp(job_type, "detect_query") ) {517 stampJob = false;593 if (!strcmp(job_type, "get_image") || !strcmp(job_type, "detect_query") || !strcmp(job_type, "none")) { 594 stampJob = false; 518 595 } else if (!strcmp(job_type, "stamp")) { 519 stampJob = true;596 stampJob = true; 520 597 } else { 521 psError(PS_ERR_UNKNOWN, false, "unknown value for -job_type: %s", job_type);522 return false;523 } 524 if (!pstampJobInsert(config->dbh, 598 psError(PS_ERR_UNKNOWN, false, "unknown value for -job_type: %s", job_type); 599 return false; 600 } 601 if (!pstampJobInsert(config->dbh, 525 602 0, // job_id 526 603 req_id, 527 rownum, 528 stateString, 529 job_type, 530 fault, 531 exp_id, 532 outputBase 604 rownum, 605 stateString, 606 job_type, 607 fault, 608 exp_id, 609 outputBase, 610 options, 611 dep_id 533 612 )) { 534 613 psError(PS_ERR_UNKNOWN, false, "database error"); … … 538 617 psU64 affected = psDBAffectedRows(config->dbh); 539 618 if (affected != 1) { 540 psError(PS_ERR_UNKNOWN, false, 619 psError(PS_ERR_UNKNOWN, false, 541 620 "should have affected one row but %" PRIu64 " rows were modified", 542 621 affected); … … 554 633 PS_ASSERT_PTR_NON_NULL(config, false); 555 634 556 PXOPT_LOOKUP_S64(req_id, config->args, "-req_id", false, false); 557 PXOPT_LOOKUP_S64(job_id, config->args, "-job_id", false, false); 635 psMetadata *where = psMetadataAlloc(); 636 PXOPT_COPY_S64(config->args, where, "-job_id", "job_id", "=="); 637 PXOPT_COPY_S64(config->args, where, "-req_id", "req_id", "=="); 638 PXOPT_COPY_S64(config->args, where, "-fault", "fault", "=="); 558 639 559 640 PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false); 560 641 PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false); 561 642 562 psString query = NULL; 563 564 if (!req_id && !job_id) { 565 fprintf(stderr, "either req_id or job_id must be specified\n"); 643 if (!psListLength(where->list)) { 644 fprintf(stderr, "search arguments are required\n"); 566 645 exit (1); 567 646 } 568 647 569 if (req_id) { 570 psStringAppend(&query, 571 "SELECT" 572 " pstampJob.*, pstampRequest.name, pstampRequest.outProduct" 573 " FROM pstampJob" 574 " JOIN pstampRequest USING(req_id)" 575 " WHERE req_id = %" PRId64, req_id 576 ); 577 } else { 578 psStringAppend(&query, 579 "SELECT" 580 " pstampJob.*, pstampRequest.name, pstampRequest.outProduct" 581 " FROM pstampJob" 582 " JOIN pstampRequest USING(req_id)" 583 " WHERE job_id = %" PRId64, job_id 584 ); 585 } 648 psString query = pxDataGet("pstamptool_listjob.sql"); 649 if (!query) { 650 psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement"); 651 return false; 652 } 653 654 // use psDBGenerateWhereSQL because the SQL yields an intermediate table 655 if (psListLength(where->list)) { 656 psString whereClause = psDBGenerateWhereConditionSQL(where, "pstampJob"); 657 psStringAppend(&query, " WHERE %s", whereClause); 658 psFree(whereClause); 659 } 660 psFree(where); 586 661 587 662 // treat limit == 0 as "no limit" … … 630 705 PXOPT_COPY_S64(config->args, where, "-job_id", "job_id", "=="); 631 706 PXOPT_COPY_S64(config->args, where, "-req_id", "req_id", "=="); 707 pxAddLabelSearchArgs(config, where, "-label", "label", "LIKE"); 708 632 709 PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false); 633 710 PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false); … … 635 712 psString query = pxDataGet("pstamptool_pendingjob.sql"); 636 713 if (!query) { 637 psError(PXTOOLS_ERR_ DATA, false, "failed to retreive SQL statement");714 psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement"); 638 715 return false; 639 716 } … … 641 718 // use psDBGenerateWhereSQL because the SQL yields an intermediate table 642 719 if (psListLength(where->list)) { 643 psString whereClause = psDBGenerateWhereConditionSQL(where, "pstampJob");720 psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); 644 721 psStringAppend(&query, " AND %s", whereClause); 645 722 psFree(whereClause); … … 688 765 PS_ASSERT_PTR_NON_NULL(config, false); 689 766 690 PXOPT_LOOKUP_S64(job_id, config->args, "-job_id", true, false); 767 PXOPT_LOOKUP_S64(job_id, config->args, "-job_id", false, false); 768 PXOPT_LOOKUP_S64(dep_id, config->args, "-dep_id", false, false); 769 770 if (!job_id && !dep_id) { 771 psError(PS_ERR_UNKNOWN, true, "at least -job_id or -dep_id is required"); 772 return false; 773 } 691 774 PXOPT_LOOKUP_STR(state, config->args, "-state", true, false); 692 PXOPT_LOOKUP_STR(fault, config->args, "-fault", false, false); 775 PXOPT_LOOKUP_S32(fault, config->args, "-fault", false, false); 776 777 psMetadata *where = psMetadataAlloc(); 778 779 PXOPT_COPY_S64(config->args, where, "-job_id", "job_id", "=="); 780 PXOPT_COPY_S64(config->args, where, "-dep_id", "dep_id", "=="); 693 781 694 782 psString faultStr = NULL; … … 696 784 faultStr = psStringCopy(""); 697 785 } else { 698 psStringAppend(&faultStr, ", fault = '%s'", fault); 699 } 700 701 char *query ="UPDATE pstampJob" 702 " SET state = '%s' %s" 703 " WHERE job_id = %" PRId64; 704 705 if (!p_psDBRunQueryF(config->dbh, query, state, faultStr, job_id)) { 786 psStringAppend(&faultStr, "\n, pstampJob.fault = %d", fault); 787 } 788 789 psString query = pxDataGet("pstamptool_updatejob.sql"); 790 791 psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); 792 psStringAppend(&query, " WHERE %s", whereClause); 793 psFree(whereClause); 794 psFree(where); 795 796 if (!p_psDBRunQueryF(config->dbh, query, state, faultStr)) { 706 797 psError(PS_ERR_UNKNOWN, false, "database error"); 707 798 psFree(query); … … 709 800 } 710 801 psFree(faultStr); 802 psFree(query); 711 803 712 804 psU64 affected = psDBAffectedRows(config->dbh); 713 if (affected != 1) { 714 psError(PS_ERR_UNKNOWN, false, "should have affected one row but %" 715 PRIu64 " rows were modified", affected); 805 psLogMsg("pstamptool", PS_LOG_INFO, "Updated %" PRIu64 " pstampJobs", affected); 806 807 return true; 808 } 809 810 static bool revertjobMode(pxConfig *config) 811 { 812 PS_ASSERT_PTR_NON_NULL(config, false); 813 814 psMetadata *where = psMetadataAlloc(); 815 816 PXOPT_COPY_S64(config->args, where, "-job_id", "job_id", "=="); 817 PXOPT_COPY_S64(config->args, where, "-req_id", "req_id", "=="); 818 PXOPT_COPY_S64(config->args, where, "-fault", "pstampJob.fault", "=="); 819 PXOPT_COPY_S64(config->args, where, "-req_id_min", "req_id", ">="); 820 pxAddLabelSearchArgs(config, where, "-label", "pstampRequest.label", "LIKE"); 821 822 PXOPT_LOOKUP_BOOL(all, config->args, "-all", false); 823 PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false); 824 PXOPT_LOOKUP_S16(fault, config->args, "-fault", false, false); 825 826 // By default only revert faults < 10 which are our "ipp exit codes" 827 // codes larger than that are the pstamp request interface. 828 // Don't fault those unless -fault waa provided 829 psString faultClause = ""; 830 if (!fault) { 831 faultClause = " \nAND (pstampJob.fault < 10)"; 832 } 833 834 psString query = pxDataGet("pstamptool_revertjob.sql"); 835 if (!psListLength(where->list) && !all) { 836 psFree(where); 837 psError(PXTOOLS_ERR_CONFIG, false, "search parameters or -all are required"); 838 return false; 839 } 840 if (psListLength(where->list)) { 841 psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); 842 psStringAppend(&query, " AND %s", whereClause); 843 psFree(whereClause); 844 } 845 psFree(where); 846 847 if (!p_psDBRunQueryF(config->dbh, query, faultClause)) { 848 psError(PS_ERR_UNKNOWN, false, "database error"); 716 849 return false; 717 850 } … … 759 892 psString query = pxDataGet("pstamptool_project.sql"); 760 893 if (!query) { 761 psError(PXTOOLS_ERR_ DATA, false, "failed to retreive SQL statement");894 psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement"); 762 895 return false; 763 896 } … … 805 938 806 939 char *query = psStringCopy ("UPDATE pstampProject SET"); 807 940 808 941 psStringAppend(&query, " state = '%s'", state); 809 942 810 943 psStringAppend(&query, " WHERE proj_id = %" PRId64, proj_id); 811 944 … … 818 951 psU64 affected = psDBAffectedRows(config->dbh); 819 952 if (affected != 1) { 820 psError(PS_ERR_UNKNOWN, false, "should have affected one row but %" 953 psError(PS_ERR_UNKNOWN, false, "should have affected one row but %" 821 954 PRIu64 " rows were modified", affected); 822 955 return false; … … 826 959 } 827 960 961 static bool getdependentMode(pxConfig *config) 962 { 963 PS_ASSERT_PTR_NON_NULL(config, false); 964 965 PXOPT_LOOKUP_STR(stage, config->args, "-stage", true, false); 966 PXOPT_LOOKUP_S64(stage_id, config->args, "-stage_id", true, false); 967 PXOPT_LOOKUP_STR(component, config->args, "-component", true, false); 968 PXOPT_LOOKUP_STR(imagedb, config->args, "-imagedb", true, false); 969 PXOPT_LOOKUP_STR(rlabel, config->args, "-rlabel", true, false); 970 PXOPT_LOOKUP_BOOL(need_magic, config->args, "-need_magic", false); 971 PXOPT_LOOKUP_BOOL(no_create, config->args, "-no_create", false); 972 973 psMetadata *where = psMetadataAlloc(); 974 PXOPT_COPY_STR(config->args, where, "-stage", "stage", "=="); 975 PXOPT_COPY_STR(config->args, where, "-imagedb", "imagedb", "=="); 976 PXOPT_COPY_S64(config->args, where, "-stage_id", "stage_id", "=="); 977 PXOPT_COPY_STR(config->args, where, "-component", "component", "=="); 978 979 // start a transaction eraly so it will contain any row level locks 980 if (!psDBTransaction(config->dbh)) { 981 psError(PS_ERR_UNKNOWN, false, "database error"); 982 return false; 983 } 984 985 psString query = pxDataGet("pstamptool_getdependent.sql"); 986 if (!query) { 987 psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement"); 988 return false; 989 } 990 psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); 991 if (!no_create) { 992 // This will lock the row until the transaction is committed 993 psStringAppend(&query, " AND %s FOR UPDATE", whereClause); 994 } else { 995 psStringAppend(&query, " AND %s", whereClause); 996 } 997 psFree(whereClause); 998 psFree(where); 999 1000 if (!p_psDBRunQuery(config->dbh, query)) { 1001 psError(PS_ERR_UNKNOWN, false, "database error"); 1002 psFree(query); 1003 return false; 1004 } 1005 psFree(query); 1006 1007 psArray *output = p_psDBFetchResult(config->dbh); 1008 if (!output) { 1009 psError(PS_ERR_UNKNOWN, false, "database error"); 1010 return false; 1011 } 1012 if (psArrayLength(output)) { 1013 psMetadata *dep = output->data[0]; 1014 psS64 dep_id = psMetadataLookupS64(NULL, dep, "dep_id"); 1015 if (!dep_id) { 1016 psError(PS_ERR_UNKNOWN, false, "database error"); 1017 psFree(output); 1018 return false; 1019 } 1020 printf("%" PRId64 "\n", dep_id); 1021 psFree(output); 1022 if (!psDBRollback(config->dbh)) { 1023 psError(PS_ERR_UNKNOWN, false, "database error"); 1024 } 1025 return true; 1026 } 1027 if (no_create) { 1028 if (!psDBRollback(config->dbh)) { 1029 psError(PS_ERR_UNKNOWN, false, "database error"); 1030 return false; 1031 } 1032 return true; 1033 } 1034 // no existing dependent that matches, insert one 1035 // Since we have multiple processes running jobs we have a 1036 // race condition here so that's why we need to lock the table 1037 1038 if (!pstampDependentInsert( 1039 config->dbh, 1040 0, // dep_id 1041 "new", // state 1042 stage, 1043 stage_id, 1044 component, 1045 imagedb, 1046 rlabel, 1047 need_magic, 1048 0 // fault 1049 )) { 1050 if (!psDBRollback(config->dbh)) { 1051 psError(PS_ERR_UNKNOWN, false, "database error"); 1052 } 1053 psError(PS_ERR_UNKNOWN, false, "failed to insert pstampDependent"); 1054 return false; 1055 } 1056 1057 // if we try and get this after commit zero is returned 1058 psS64 dep_id = psDBLastInsertID(config->dbh); 1059 if (!dep_id) { 1060 psError(PS_ERR_UNKNOWN, false, "psDBLastInsertID returned NULL"); 1061 if (!psDBRollback(config->dbh)) { 1062 psError(PS_ERR_UNKNOWN, false, "database error"); 1063 } 1064 return false; 1065 } 1066 1067 if (!psDBCommit(config->dbh)) { 1068 // rollback 1069 if (!psDBRollback(config->dbh)) { 1070 psError(PS_ERR_UNKNOWN, false, "database error"); 1071 } 1072 psError(PS_ERR_UNKNOWN, false, "database error"); 1073 return false; 1074 } 1075 1076 printf("%" PRId64 "\n", dep_id); 1077 1078 return true; 1079 } 1080 1081 static bool pendingdependentMode(pxConfig *config) 1082 { 1083 PS_ASSERT_PTR_NON_NULL(config, false); 1084 1085 psMetadata *where = psMetadataAlloc(); 1086 PXOPT_COPY_STR(config->args, where, "-stage", "stage", "=="); 1087 PXOPT_COPY_STR(config->args, where, "-imagedb", "imagedb", "=="); 1088 PXOPT_COPY_S64(config->args, where, "-stage_id", "stage_id", "=="); 1089 PXOPT_COPY_STR(config->args, where, "-rlabel", "rlabel", "=="); 1090 pxAddLabelSearchArgs(config, where, "-label", "pstampRequest.label", "=="); 1091 1092 PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false); 1093 PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false); 1094 1095 psString query = pxDataGet("pstamptool_pendingdependent.sql"); 1096 if (!query) { 1097 psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement"); 1098 return false; 1099 } 1100 1101 if (psListLength(where->list)) { 1102 psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); 1103 psStringAppend(&query, " AND %s", whereClause); 1104 psFree(whereClause); 1105 } 1106 psFree(where); 1107 1108 // treat limit == 0 as "no limit" 1109 if (limit) { 1110 psString limitString = psDBGenerateLimitSQL(limit); 1111 psStringAppend(&query, " %s", limitString); 1112 psFree(limitString); 1113 } 1114 1115 if (!p_psDBRunQuery(config->dbh, query)) { 1116 psError(PS_ERR_UNKNOWN, false, "database error"); 1117 psFree(query); 1118 return false; 1119 } 1120 psFree(query); 1121 1122 psArray *output = p_psDBFetchResult(config->dbh); 1123 if (!output) { 1124 psError(PS_ERR_UNKNOWN, false, "database error"); 1125 return false; 1126 } 1127 if (!psArrayLength(output)) { 1128 psTrace("pstamptool", PS_LOG_INFO, "no rows found"); 1129 psFree(output); 1130 return true; 1131 } 1132 1133 // negative simple so the default is true 1134 if (!ippdbPrintMetadatas(stdout, output, "pstampDependent", !simple)) { 1135 psError(PS_ERR_UNKNOWN, false, "failed to print array"); 1136 psFree(output); 1137 return false; 1138 } 1139 1140 psFree(output); 1141 1142 return true; 1143 } 1144 static bool updatedependentMode(pxConfig *config) 1145 { 1146 PS_ASSERT_PTR_NON_NULL(config, false); 1147 1148 PXOPT_LOOKUP_S64(dep_id, config->args, "-dep_id", true, false); 1149 PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, false); 1150 PXOPT_LOOKUP_S16(fault, config->args, "-fault", false, false); 1151 1152 if (!fault && !state) { 1153 psError(PS_ERR_UNKNOWN, true, "at least one of -set_state or fault is required"); 1154 return false; 1155 } 1156 psString query = psStringCopy("UPDATE pstampDependent SET"); 1157 bool needComma = false; 1158 if (state) { 1159 psStringAppend(&query, " state = '%s'", state); 1160 needComma = true; 1161 } 1162 if (fault) { 1163 psStringAppend(&query, "%s fault = %d", needComma ? ", " : "", fault); 1164 needComma = true; 1165 } 1166 psStringAppend(&query, " WHERE dep_id = %" PRId64, dep_id); 1167 1168 if (!p_psDBRunQuery(config->dbh, query)) { 1169 psError(PS_ERR_UNKNOWN, false, "database error"); 1170 psFree(query); 1171 return false; 1172 } 1173 1174 psU64 affected = psDBAffectedRows(config->dbh); 1175 if (affected != 1) { 1176 psError(PS_ERR_UNKNOWN, false, "should have affected one row but %" 1177 PRIu64 " rows were modified", affected); 1178 return false; 1179 } 1180 1181 return true; 1182 } 1183 static bool revertdependentMode(pxConfig *config) 1184 { 1185 PS_ASSERT_PTR_NON_NULL(config, false); 1186 1187 psMetadata *where = psMetadataAlloc(); 1188 PXOPT_COPY_S64(config->args, where, "-fault", "pstampDependent.fault", "=="); 1189 PXOPT_COPY_S64(config->args, where, "-dep_id", "dep_id", "=="); 1190 pxAddLabelSearchArgs(config, where, "-label", "pstampRequest.label", "=="); 1191 1192 if (!psListLength(where->list)) { 1193 psFree(where); 1194 psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required"); 1195 return false; 1196 } 1197 psString query = pxDataGet("pstamptool_revertdependent.sql"); 1198 if (!query) { 1199 psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement"); 1200 return false; 1201 } 1202 1203 psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); 1204 psStringAppend(&query, " AND %s", whereClause); 1205 psFree(whereClause); 1206 psFree(where); 1207 1208 if (!p_psDBRunQuery(config->dbh, query)) { 1209 psError(PS_ERR_UNKNOWN, false, "database error"); 1210 psFree(query); 1211 return false; 1212 } 1213 1214 return true; 1215 }
Note:
See TracChangeset
for help on using the changeset viewer.
