Changeset 27839 for branches/simmosaic_branches/ippTools/src/pstamptool.c
- Timestamp:
- May 3, 2010, 8:45:22 AM (16 years ago)
- Location:
- branches/simmosaic_branches
- Files:
-
- 3 edited
-
. (modified) (1 prop)
-
ippTools/src (modified) (1 prop)
-
ippTools/src/pstamptool.c (modified) (34 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/simmosaic_branches
- Property svn:mergeinfo changed
-
branches/simmosaic_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/simmosaic_branches/ippTools/src/pstamptool.c
r21412 r27839 34 34 static bool datastoreMode(pxConfig *config); 35 35 static bool moddatastoreMode(pxConfig *config); 36 static bool addReqMode(pxConfig *config); 37 static bool completedReqMode(pxConfig *config); 38 static bool listReqMode(pxConfig *config); 39 static bool pendingReqMode(pxConfig *config); 40 static bool updateReqMode(pxConfig *config); 41 static bool revertReqMode(pxConfig *config); 42 static bool addJobMode(pxConfig *config); 43 static bool listJobMode(pxConfig *config); 44 static bool pendingJobMode(pxConfig *config); 45 static bool updateJobMode(pxConfig *config); 46 static bool addProjectMode(pxConfig *config); 36 static bool addreqMode(pxConfig *config); 37 static bool completedreqMode(pxConfig *config); 38 static bool listreqMode(pxConfig *config); 39 static bool pendingreqMode(pxConfig *config); 40 static bool updatereqMode(pxConfig *config); 41 static bool revertreqMode(pxConfig *config); 42 static bool addjobMode(pxConfig *config); 43 static bool listjobMode(pxConfig *config); 44 static bool pendingjobMode(pxConfig *config); 45 static bool updatejobMode(pxConfig *config); 46 static bool revertjobMode(pxConfig *config); 47 static bool addprojectMode(pxConfig *config); 47 48 static bool projectMode(pxConfig *config); 48 static bool modProjectMode(pxConfig *config); 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) \ … … 70 75 MODECASE(PSTAMPTOOL_MODE_DATASTORE, datastoreMode); 71 76 MODECASE(PSTAMPTOOL_MODE_MODDATASTORE, moddatastoreMode); 72 MODECASE(PSTAMPTOOL_MODE_ADDREQ, addReqMode); 73 MODECASE(PSTAMPTOOL_MODE_COMPLETEDREQ, completedReqMode); 74 MODECASE(PSTAMPTOOL_MODE_LISTREQ, listReqMode); 75 MODECASE(PSTAMPTOOL_MODE_PENDINGREQ, pendingReqMode); 76 MODECASE(PSTAMPTOOL_MODE_UPDATEREQ, updateReqMode); 77 MODECASE(PSTAMPTOOL_MODE_REVERTREQ, revertReqMode); 78 MODECASE(PSTAMPTOOL_MODE_ADDJOB, addJobMode); 79 MODECASE(PSTAMPTOOL_MODE_LISTJOB, listJobMode); 80 MODECASE(PSTAMPTOOL_MODE_PENDINGJOB, pendingJobMode); 81 MODECASE(PSTAMPTOOL_MODE_UPDATEJOB, updateJobMode); 82 MODECASE(PSTAMPTOOL_MODE_ADDPROJECT, addProjectMode); 83 MODECASE(PSTAMPTOOL_MODE_MODPROJECT, modProjectMode); 77 MODECASE(PSTAMPTOOL_MODE_ADDREQ, addreqMode); 78 MODECASE(PSTAMPTOOL_MODE_COMPLETEDREQ, completedreqMode); 79 MODECASE(PSTAMPTOOL_MODE_LISTREQ, listreqMode); 80 MODECASE(PSTAMPTOOL_MODE_PENDINGREQ, pendingreqMode); 81 MODECASE(PSTAMPTOOL_MODE_UPDATEREQ, updatereqMode); 82 MODECASE(PSTAMPTOOL_MODE_REVERTREQ, revertreqMode); 83 MODECASE(PSTAMPTOOL_MODE_ADDJOB, addjobMode); 84 MODECASE(PSTAMPTOOL_MODE_LISTJOB, listjobMode); 85 MODECASE(PSTAMPTOOL_MODE_PENDINGJOB, pendingjobMode); 86 MODECASE(PSTAMPTOOL_MODE_UPDATEJOB, updatejobMode); 87 MODECASE(PSTAMPTOOL_MODE_REVERTJOB, revertjobMode); 88 MODECASE(PSTAMPTOOL_MODE_ADDPROJECT, addprojectMode); 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; … … 220 247 } 221 248 222 static bool add ReqMode(pxConfig *config)223 { 224 PS_ASSERT_PTR_NON_NULL(config, false); 225 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);249 static bool addreqMode(pxConfig *config) 250 { 251 PS_ASSERT_PTR_NON_NULL(config, false); 252 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 } … … 251 278 } 252 279 253 static bool pending ReqMode(pxConfig *config)280 static bool pendingreqMode(pxConfig *config) 254 281 { 255 282 PS_ASSERT_PTR_NON_NULL(config, false); … … 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 } … … 311 339 } 312 340 313 static bool listReqMode(pxConfig *config) 314 { 315 PS_ASSERT_PTR_NON_NULL(config, false); 316 317 PXOPT_LOOKUP_S64(req_id, config->args, "-req_id", true, false); 341 static bool listreqMode(pxConfig *config) 342 { 343 PS_ASSERT_PTR_NON_NULL(config, false); 344 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 … … 359 403 } 360 404 361 static bool completedReqMode(pxConfig *config) 362 { 363 PS_ASSERT_PTR_NON_NULL(config, false); 405 static bool completedreqMode(pxConfig *config) 406 { 407 PS_ASSERT_PTR_NON_NULL(config, false); 408 409 psMetadata *where = psMetadataAlloc(); 410 pxAddLabelSearchArgs(config, where, "-label", "label", "LIKE"); 364 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" … … 408 459 } 409 460 410 static bool update ReqMode(pxConfig *config)461 static bool updatereqMode(pxConfig *config) 411 462 { 412 463 PS_ASSERT_PTR_NON_NULL(config, false); … … 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; … … 472 523 } 473 524 474 static bool revertReqMode(pxConfig *config) 475 { 476 PS_ASSERT_PTR_NON_NULL(config, false); 477 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 } 491 492 return true; 493 } 494 495 static bool addJobMode(pxConfig *config) 525 static bool revertreqMode(pxConfig *config) 526 { 527 PS_ASSERT_PTR_NON_NULL(config, false); 528 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); 566 567 return true; 568 } 569 570 static bool addjobMode(pxConfig *config) 496 571 { 497 572 bool stampJob = false; 498 char *query = NULL; 499 500 PS_ASSERT_PTR_NON_NULL(config, false); 501 502 PXOPT_LOOKUP_S64(uri, config->args, "-uri", true, false); 573 574 PS_ASSERT_PTR_NON_NULL(config, false); 575 503 576 PXOPT_LOOKUP_S64(req_id, config->args, "-req_id", true, false); 504 577 PXOPT_LOOKUP_STR(rownum, config->args, "-rownum", true, false); 505 578 PXOPT_LOOKUP_STR(job_type, config->args, "-job_type", false, false); 506 PXOPT_LOOKUP_STR(outputBase, config->args, "-outputBase", true, false); 507 PXOPT_LOOKUP_STR(argString, config->args, "-args", false, false); 579 PXOPT_LOOKUP_STR(outputBase, config->args, "-outputBase", false, false); 508 580 PXOPT_LOOKUP_STR(stateString, config->args, "-state", false, false); 509 PXOPT_LOOKUP_S TR(fault, config->args, "-fault", false, false);581 PXOPT_LOOKUP_S16(fault, config->args, "-fault", false, false); 510 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); 585 586 // unless the job is being inserted with stop state require outputBase 587 if (strcmp(stateString, "stop") && !outputBase) { 588 psError(PS_ERR_UNKNOWN, true, "-outputBase is required"); 589 return false; 590 } 511 591 512 592 // default value for job_type is defined in pstamptoolConfig.c 513 if (!strcmp(job_type, "get_image") || !strcmp(job_type, "detect_query") ) {514 stampJob = false;593 if (!strcmp(job_type, "get_image") || !strcmp(job_type, "detect_query") || !strcmp(job_type, "none")) { 594 stampJob = false; 515 595 } else if (!strcmp(job_type, "stamp")) { 516 stampJob = true;596 stampJob = true; 517 597 } else { 518 psError(PS_ERR_UNKNOWN, false, "unknown value for -job_type: %s", job_type); 519 return false; 520 } 521 522 if (stampJob && !argString) { 523 psError(PS_ERR_UNKNOWN, true, "-args is required for stamp job"); 524 return false; 525 } 526 527 if (stampJob) { 528 query = pxDataGet("pstamptool_addjob_stampjob.sql"); 529 } else { 530 query = pxDataGet("pstamptool_addjob_otherjob.sql"); 531 } 532 if (!query) { 533 psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement"); 534 return false; 535 } 536 537 if (!p_psDBRunQueryF(config->dbh, query, req_id, rownum, stateString, job_type, uri, exp_id, outputBase, fault, argString)) { 598 psError(PS_ERR_UNKNOWN, false, "unknown value for -job_type: %s", job_type); 599 return false; 600 } 601 if (!pstampJobInsert(config->dbh, 602 0, // job_id 603 req_id, 604 rownum, 605 stateString, 606 job_type, 607 fault, 608 exp_id, 609 outputBase, 610 options, 611 dep_id 612 )) { 538 613 psError(PS_ERR_UNKNOWN, false, "database error"); 539 614 return false; … … 542 617 psU64 affected = psDBAffectedRows(config->dbh); 543 618 if (affected != 1) { 544 psError(PS_ERR_UNKNOWN, false, 619 psError(PS_ERR_UNKNOWN, false, 545 620 "should have affected one row but %" PRIu64 " rows were modified", 546 621 affected); … … 554 629 } 555 630 556 static bool listJobMode(pxConfig *config) 557 { 558 PS_ASSERT_PTR_NON_NULL(config, false); 559 560 PXOPT_LOOKUP_S64(req_id, config->args, "-req_id", false, false); 561 PXOPT_LOOKUP_S64(job_id, config->args, "-job_id", false, false); 631 static bool listjobMode(pxConfig *config) 632 { 633 PS_ASSERT_PTR_NON_NULL(config, false); 634 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", "=="); 562 639 563 640 PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false); 564 641 PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false); 565 642 566 psString query = NULL; 567 568 if (!req_id && !job_id) { 569 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"); 570 645 exit (1); 571 646 } 572 647 573 if (req_id) { 574 psStringAppend(&query, 575 "SELECT" 576 " pstampJob.*, pstampRequest.name, pstampRequest.outProduct" 577 " FROM pstampJob" 578 " JOIN pstampRequest USING(req_id)" 579 " WHERE req_id = %" PRId64, req_id 580 ); 581 } else { 582 psStringAppend(&query, 583 "SELECT" 584 " pstampJob.*, pstampRequest.name, pstampRequest.outProduct" 585 " FROM pstampJob" 586 " JOIN pstampRequest USING(req_id)" 587 " WHERE job_id = %" PRId64, job_id 588 ); 589 } 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); 590 661 591 662 // treat limit == 0 as "no limit" … … 626 697 } 627 698 628 static bool pending JobMode(pxConfig *config)699 static bool pendingjobMode(pxConfig *config) 629 700 { 630 701 PS_ASSERT_PTR_NON_NULL(config, false); … … 634 705 PXOPT_COPY_S64(config->args, where, "-job_id", "job_id", "=="); 635 706 PXOPT_COPY_S64(config->args, where, "-req_id", "req_id", "=="); 707 pxAddLabelSearchArgs(config, where, "-label", "label", "LIKE"); 708 636 709 PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false); 637 710 PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false); … … 639 712 psString query = pxDataGet("pstamptool_pendingjob.sql"); 640 713 if (!query) { 641 psError(PXTOOLS_ERR_ DATA, false, "failed to retreive SQL statement");714 psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement"); 642 715 return false; 643 716 } … … 645 718 // use psDBGenerateWhereSQL because the SQL yields an intermediate table 646 719 if (psListLength(where->list)) { 647 psString whereClause = psDBGenerateWhereConditionSQL(where, "pstampJob");720 psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); 648 721 psStringAppend(&query, " AND %s", whereClause); 649 722 psFree(whereClause); … … 688 761 } 689 762 690 static bool updateJobMode(pxConfig *config) 691 { 692 PS_ASSERT_PTR_NON_NULL(config, false); 693 694 PXOPT_LOOKUP_S64(job_id, config->args, "-job_id", true, false); 763 static bool updatejobMode(pxConfig *config) 764 { 765 PS_ASSERT_PTR_NON_NULL(config, false); 766 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 } 695 774 PXOPT_LOOKUP_STR(state, config->args, "-state", true, false); 696 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", "=="); 697 781 698 782 psString faultStr = NULL; … … 700 784 faultStr = psStringCopy(""); 701 785 } else { 702 psStringAppend(&faultStr, ", fault = '%s'", fault); 703 } 704 705 char *query ="UPDATE pstampJob" 706 " SET state = '%s' %s" 707 " WHERE job_id = %" PRId64; 708 709 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)) { 710 797 psError(PS_ERR_UNKNOWN, false, "database error"); 711 798 psFree(query); … … 713 800 } 714 801 psFree(faultStr); 802 psFree(query); 715 803 716 804 psU64 affected = psDBAffectedRows(config->dbh); 717 if (affected != 1) { 718 psError(PS_ERR_UNKNOWN, false, "should have affected one row but %" 719 PRIu64 " rows were modified", affected); 720 return false; 721 } 722 723 return true; 724 } 725 static bool addProjectMode(pxConfig *config) 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"); 849 return false; 850 } 851 852 return true; 853 } 854 static bool addprojectMode(pxConfig *config) 726 855 { 727 856 PS_ASSERT_PTR_NON_NULL(config, false); … … 763 892 psString query = pxDataGet("pstamptool_project.sql"); 764 893 if (!query) { 765 psError(PXTOOLS_ERR_ DATA, false, "failed to retreive SQL statement");894 psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement"); 766 895 return false; 767 896 } … … 801 930 return true; 802 931 } 803 static bool mod ProjectMode(pxConfig *config)932 static bool modprojectMode(pxConfig *config) 804 933 { 805 934 PS_ASSERT_PTR_NON_NULL(config, false); … … 809 938 810 939 char *query = psStringCopy ("UPDATE pstampProject SET"); 811 940 812 941 psStringAppend(&query, " state = '%s'", state); 813 942 814 943 psStringAppend(&query, " WHERE proj_id = %" PRId64, proj_id); 815 944 … … 822 951 psU64 affected = psDBAffectedRows(config->dbh); 823 952 if (affected != 1) { 824 psError(PS_ERR_UNKNOWN, false, "should have affected one row but %" 953 psError(PS_ERR_UNKNOWN, false, "should have affected one row but %" 825 954 PRIu64 " rows were modified", affected); 826 955 return false; … … 830 959 } 831 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.
