IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 3, 2010, 8:50:52 AM (16 years ago)
Author:
eugene
Message:

updates from trunk

Location:
branches/simtest_nebulous_branches
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/simtest_nebulous_branches

  • branches/simtest_nebulous_branches/ippTools/src

    • Property svn:ignore
      •  

        old new  
         1*.la
         2*.lo
        13.deps
        24.gdb_history
         
        46Makefile
        57Makefile.in
         8addtool
         9caltool
         10camtool
         11chiptool
        612config.h
        713config.h.in
        8 stamp-h1
        9 *.la
        10 *.lo
         14detselect
         15dettool
         16difftool
         17disttool
         18dqstatstool
         19faketool
         20flatcorr
         21guidetool
         22magicdstool
         23magictool
         24pstamptool
         25pubtool
         26pxadmin
         27pxdata.c
         28pxinject
        1129pxtoolsErrorCodes.c
        1230pxtoolsErrorCodes.h
        13 pxadmin
        14 pxinject
        15 pztool
        1631pzgetexp
        1732pzgetimfiles
         33pztool
         34receivetool
        1835regtool
        19 guidetool
        20 chiptool
        21 camtool
         36stacktool
         37stamp-h1
        2238warptool
        23 difftool
        24 stacktool
        25 faketool
        26 dettool
        27 detselect
        28 pxdata.c
        29 magictool
        30 magicdstool
        31 caltool
        32 flatcorr
        33 pstamptool
        34 disttool
        35 receivetool
        36 
        37 pubtool
  • branches/simtest_nebulous_branches/ippTools/src/pstamptool.c

    r25054 r27840  
    4444static bool pendingjobMode(pxConfig *config);
    4545static bool updatejobMode(pxConfig *config);
     46static bool revertjobMode(pxConfig *config);
    4647static bool addprojectMode(pxConfig *config);
    4748static bool projectMode(pxConfig *config);
    4849static bool modprojectMode(pxConfig *config);
     50static bool getdependentMode(pxConfig *config);
     51static bool pendingdependentMode(pxConfig *config);
     52static bool updatedependentMode(pxConfig *config);
     53static bool revertdependentMode(pxConfig *config);
    4954
    5055# define MODECASE(caseName, func) \
     
    8085        MODECASE(PSTAMPTOOL_MODE_PENDINGJOB, pendingjobMode);
    8186        MODECASE(PSTAMPTOOL_MODE_UPDATEJOB, updatejobMode);
     87        MODECASE(PSTAMPTOOL_MODE_REVERTJOB, revertjobMode);
    8288        MODECASE(PSTAMPTOOL_MODE_ADDPROJECT, addprojectMode);
    8389        MODECASE(PSTAMPTOOL_MODE_MODPROJECT, modprojectMode);
    8490        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);
    8595        default:
    8696            psAbort("invalid option (this should not happen)");
     
    108118    PS_ASSERT_PTR_NON_NULL(config, false);
    109119
    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);
    114126
    115127    if (!pstampDataStoreInsert(config->dbh,
     
    117129            state,
    118130            lastFileset,
     131            NULL,       // timestamp
     132            label,      // label
    119133            outProduct,
    120             uri
     134            uri,
     135            pollInterval
    121136        )) {
    122137        psError(PS_ERR_UNKNOWN, false, "database error");
     
    134149    PXOPT_COPY_S64(config->args, where, "-ds_id", "ds_id", "==");
    135150
     151    PXOPT_LOOKUP_BOOL(ready, config->args, "-ready", false);
    136152    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    137153
    138154    psString query = pxDataGet("pstamptool_datastore.sql");
    139155    if (!query) {
    140         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     156        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    141157        return false;
    142158    }
     
    148164    }
    149165    psFree(where);
     166    if (ready) {
     167        psStringAppend(&query, " %s", "\nAND TIMESTAMPDIFF(SECOND, timestamp, now()) > pollInterval");
     168    }
    150169
    151170    if (!p_psDBRunQuery(config->dbh, query)) {
     
    181200
    182201    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
    194215    if (lastFileset) {
    195         psStringAppend(&query, " lastFileset = '%s'", lastFileset);
    196         needComma = true;
     216        psStringAppend(&query, " , lastFileset = '%s'", lastFileset);
    197217    }
    198218
    199219    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
    204231    psStringAppend(&query, " WHERE ds_id = %" PRId64, ds_id);
    205232
     
    212239    psU64 affected = psDBAffectedRows(config->dbh);
    213240    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 %"
    215242                                        PRIu64 " rows were modified", affected);
    216243        return false;
     
    224251    PS_ASSERT_PTR_NON_NULL(config, false);
    225252
    226     PXOPT_LOOKUP_STR(uri,         config->args, "-uri",           true, false);
    227     // PXOPT_LOOKUP_STR(outFileset,  config->args, "-out_fileset",   true, false);
    228     PXOPT_LOOKUP_S64(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");
    243270        return false;
    244271    }
     
    257284    psMetadata *where = psMetadataAlloc();
    258285    PXOPT_COPY_S64(config->args, where, "-req_id", "req_id", "==");
     286    pxAddLabelSearchArgs(config, where, "-label", "label", "LIKE");
    259287
    260288    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     
    263291    psString query = pxDataGet("pstamptool_pendingreq.sql");
    264292    if (!query) {
    265         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     293        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    266294        return false;
    267295    }
     
    315343    PS_ASSERT_PTR_NON_NULL(config, false);
    316344
    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
    318350    PXOPT_LOOKUP_U64(limit,   config->args, "-limit",  false, false);
    319351    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    320352
    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
    324365    // treat limit == 0 as "no limit"
    325366    if (limit) {
     
    342383    }
    343384    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;
    347391    }
    348392
     
    363407    PS_ASSERT_PTR_NON_NULL(config, false);
    364408
     409    psMetadata *where = psMetadataAlloc();
     410    pxAddLabelSearchArgs(config, where, "-label", "label", "LIKE");
     411
    365412    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
    366413    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    367414
    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);
    372423
    373424    // treat limit == 0 as "no limit"
     
    415466    PXOPT_LOOKUP_STR(state,      config->args, "-state",      false, false);
    416467    PXOPT_LOOKUP_STR(outProduct, config->args, "-outProduct", false, false);
    417     PXOPT_LOOKUP_STR(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);
    420471    PXOPT_LOOKUP_STR(reqType,    config->args, "-reqType",    false, false);
    421472
     
    433484    }
    434485    if (fault) {
    435         psStringAppend(&query, "%c fault = %s", c, fault);
     486        psStringAppend(&query, "%c fault = %d", c, fault);
    436487        c = ',';
    437488    }
     
    464515    // note zero is not an error
    465516    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 %"
    467518                                        PRIu64 " rows were modified", affected);
    468519        return false;
     
    476527    PS_ASSERT_PTR_NON_NULL(config, false);
    477528
    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);
    491566
    492567    return true;
     
    506581    PXOPT_LOOKUP_S16(fault,       config->args, "-fault",      false, false);
    507582    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);
    508585
    509586    // unless the job is being inserted with stop state require outputBase
    510587    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;
    513590    }
    514591
    515592    // 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;
    518595    } else if (!strcmp(job_type, "stamp")) {
    519         stampJob = true;
     596        stampJob = true;
    520597    } 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,
    525602            0, // job_id
    526603            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
    533612            )) {
    534613        psError(PS_ERR_UNKNOWN, false, "database error");
     
    538617    psU64 affected = psDBAffectedRows(config->dbh);
    539618    if (affected != 1) {
    540         psError(PS_ERR_UNKNOWN, false, 
     619        psError(PS_ERR_UNKNOWN, false,
    541620            "should have affected one row but %" PRIu64 " rows were modified",
    542621            affected);
     
    554633    PS_ASSERT_PTR_NON_NULL(config, false);
    555634
    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", "==");
    558639
    559640    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
    560641    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    561642
    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");
    566645        exit (1);
    567646    }
    568647
    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);
    586661
    587662    // treat limit == 0 as "no limit"
     
    630705    PXOPT_COPY_S64(config->args, where, "-job_id", "job_id", "==");
    631706    PXOPT_COPY_S64(config->args, where, "-req_id", "req_id", "==");
     707    pxAddLabelSearchArgs(config, where, "-label", "label", "LIKE");
     708
    632709    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
    633710    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     
    635712    psString query = pxDataGet("pstamptool_pendingjob.sql");
    636713    if (!query) {
    637         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     714        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    638715        return false;
    639716    }
     
    641718    // use psDBGenerateWhereSQL because the SQL yields an intermediate table
    642719    if (psListLength(where->list)) {
    643         psString whereClause = psDBGenerateWhereConditionSQL(where, "pstampJob");
     720        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    644721        psStringAppend(&query, " AND %s", whereClause);
    645722        psFree(whereClause);
     
    688765    PS_ASSERT_PTR_NON_NULL(config, false);
    689766
    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    }
    691774    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", "==");
    693781
    694782    psString faultStr = NULL;
     
    696784        faultStr = psStringCopy("");
    697785    } 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)) {
    706797        psError(PS_ERR_UNKNOWN, false, "database error");
    707798        psFree(query);
     
    709800    }
    710801    psFree(faultStr);
     802    psFree(query);
    711803
    712804    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
     810static 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");
    716849        return false;
    717850    }
     
    759892    psString query = pxDataGet("pstamptool_project.sql");
    760893    if (!query) {
    761         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     894        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    762895        return false;
    763896    }
     
    805938
    806939    char *query = psStringCopy ("UPDATE pstampProject SET");
    807    
     940
    808941    psStringAppend(&query, " state = '%s'", state);
    809                
     942
    810943    psStringAppend(&query, " WHERE proj_id = %" PRId64, proj_id);
    811944
     
    818951    psU64 affected = psDBAffectedRows(config->dbh);
    819952    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 %"
    821954                                        PRIu64 " rows were modified", affected);
    822955        return false;
     
    826959}
    827960
     961static 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
     1081static 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}
     1144static 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}
     1183static 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.