IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Feb 21, 2008, 6:09:46 PM (18 years ago)
Author:
jhoblitt
Message:

replumb pz* steps

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippTools/src/pztool.c

    r16509 r16588  
    4545static psArray *pzGetPendingCameras(pxConfig *config);
    4646static psArray *pzArrayZip(psArray *arraySet, psS64 limit);
     47static bool pzDownloadExpSetState(pxConfig *config, const char *exp_name, const char *camera, const char *telescope, const char *state);
    4748
    4849# define MODECASE(caseName, func) \
     
    204205            "SELECT"
    205206            "   summitExp.*"
    206             " FROM pzPendingExp"
    207             " JOIN summitExp"
     207            " FROM summitExp"
     208            " LEFT JOIN pzDownloadExp"
    208209            "   USING(exp_name, camera, telescope)"
     210            " WHERE"
     211            "   pzDownloadExp.exp_name IS NULL"
     212            "   AND pzDownloadExp.camera IS NULL"
     213            "   AND pzDownloadExp.telescope IS NULL"
    209214        );
    210215
    211216    if (config->where) {
    212         psString whereClause = psDBGenerateWhereConditionSQL(config->where, "pzPendingExp");
     217        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "pzDownloadExp");
    213218        psStringAppend(&query, " AND %s", whereClause);
    214219        psFree(whereClause);
     
    281286
    282287        if (config->where) {
    283             psString whereClause = psDBGenerateWhereConditionSQL(config->where, "pzPendingImfile");
     288            psString whereClause = psDBGenerateWhereConditionSQL(config->where, "pzDownloadImfile");
    284289            psStringAppend(&query, " AND %s", whereClause);
    285290            psFree(whereClause);
     
    355360
    356361    // need to know exp_name, camera, telescope, class, class_id (to find the
    357     // pzPendingImfile entry and the URI for the newImfile entry.
     362    // pzDownloadImfile entry and the URI for the newImfile entry.
    358363
    359364    // start a transaction so it's all rows or nothing
     
    363368    }
    364369
    365     // cp the imfile into pzDoneImfile
     370    // cp the imfile into pzDownloadImfile
    366371    {
    367372        char *query =
    368             "INSERT INTO pzDoneImfile"
     373            "INSERT INTO pzDownloadImfile"
    369374            "   SELECT"
    370             "       pzPendingImfile.*,"
     375            "       exp_name,"
     376            "       camera,"
     377            "       telescope,"
     378            "       class,"
     379            "       class_id,"
    371380            "       '%s'," // uri of downloaded file
    372381            "       '%d'" // fault code
    373             "   FROM pzPendingImfile"
     382            "   FROM summitImfile"
    374383            "   WHERE"
    375             "       pzPendingImfile.exp_name = '%s'"
    376             "       AND pzPendingImfile.camera = '%s'"
    377             "       AND pzPendingImfile.telescope = '%s'"
    378             "       AND pzPendingImfile.class = '%s'"
    379             "       AND pzPendingImfile.class_id = '%s'";
     384            "       summitImfile.exp_name = '%s'"
     385            "       AND summitImfile.camera = '%s'"
     386            "       AND summitImfile.telescope = '%s'"
     387            "       AND summitImfile.class = '%s'"
     388            "       AND summitImfile.class_id = '%s'";
    380389
    381390        if (!p_psDBRunQuery(config->dbh, query, uri, code, exp_name, camera, telescope, class, class_id)) {
     
    400409    }
    401410
    402     // removve the entry from pzPendingImfile
    403     {
    404         char *query =
    405             "DELETE FROM pzPendingImfile"
    406             "   WHERE"
    407             "       pzPendingImfile.exp_name = '%s'"
    408             "       AND pzPendingImfile.camera = '%s'"
    409             "       AND pzPendingImfile.telescope = '%s'"
    410             "       AND pzPendingImfile.class = '%s'"
    411             "       AND pzPendingImfile.class_id = '%s'";
    412 
    413         if (!p_psDBRunQuery(config->dbh, query, exp_name, camera, telescope, class, class_id)) {
    414             // rollback
    415             if (!psDBRollback(config->dbh)) {
    416                 psError(PS_ERR_UNKNOWN, false, "database error");
    417             }
     411    if (!copydoneCompleteExp(config)) {
     412        // rollback
     413        if (!psDBRollback(config->dbh)) {
    418414            psError(PS_ERR_UNKNOWN, false, "database error");
    419             return false;
    420         }
    421 
    422         // sanity check: we should have removed only one row
    423         psU64 affected = psDBAffectedRows(config->dbh);
    424         if (psDBAffectedRows(config->dbh) != 1) {
    425             // rollback
    426             if (!psDBRollback(config->dbh)) {
    427                 psError(PS_ERR_UNKNOWN, false, "database error");
    428             }
    429             psError(PS_ERR_UNKNOWN, false, "should have affected 1 row but %" PRIu64 " rows were modified", affected);
    430             return false;
    431         }
     415        }
     416        psError(PS_ERR_UNKNOWN, false, "copydoneCompleteExp() failed");
     417        return false;
    432418    }
    433419
     
    439425        }
    440426        psError(PS_ERR_UNKNOWN, false, "database error");
    441         return false;
    442     }
    443 
    444     if (!copydoneCompleteExp(config)) {
    445         psError(PS_ERR_UNKNOWN, false, "copydoneCompleteExp() failed");
    446427        return false;
    447428    }
     
    493474    }
    494475
    495     // start a transaction so all newExp's can start off with a state of run
    496     if (!psDBTransaction(config->dbh)) {
    497         psError(PS_ERR_UNKNOWN, false, "database error");
    498         psFree(output);
    499         return false;
    500     }
    501 
    502 
    503476   for (long i = 0; i < psArrayLength(output); i++) {
    504477        psMetadata *row = output->data[i];
    505478
    506         pzDoneExpRow *doneExp = pzDoneExpObjectFromMetadata(row);
     479        pzDownloadExpRow *doneExp = pzDownloadExpObjectFromMetadata(row);
    507480
    508481        if (!newExpInsert(config->dbh,
     
    521494                )
    522495        ) {
    523             // rollback
    524             if (!psDBRollback(config->dbh)) {
    525                 psError(PS_ERR_UNKNOWN, false, "database error");
    526             }
    527496            psError(PS_ERR_UNKNOWN, false, "database error");
    528497            psFree(doneExp);
     
    533502        psS64 exp_id = psDBLastInsertID(config->dbh);
    534503
    535         // insert new all pzDoneImfile for the exp into newImfile
     504        // insert newImfiles
    536505        {
    537506            char *query =
     
    539508                "   SElECT"
    540509                "       %" PRId64 "," // exp_id
    541                 "       pzDoneImfile.class_id," // tmp_class_id
    542                 "       pzDoneImfile.uri" // uri
    543                 "   FROM pzDoneImfile"
     510                "       pzDownloadImfile.class_id," // tmp_class_id
     511                "       pzDownloadImfile.uri" // uri
     512                "   FROM pzDownloadImfile"
    544513                "   WHERE"
    545                 "       pzDoneImfile.exp_name = '%s'"
    546                 "       AND pzDoneImfile.camera = '%s'"
    547                 "       AND pzDoneImfile.telescope = '%s'";
     514                "       pzDownloadImfile.exp_name = '%s'"
     515                "       AND pzDownloadImfile.camera = '%s'"
     516                "       AND pzDownloadImfile.telescope = '%s'";
    548517
    549518            if (!p_psDBRunQuery(config->dbh, query, exp_id, doneExp->exp_name, doneExp->camera, doneExp->telescope)) {
    550                 // rollback
    551                 if (!psDBRollback(config->dbh)) {
    552                     psError(PS_ERR_UNKNOWN, false, "database error");
    553                 }
    554519                psError(PS_ERR_UNKNOWN, false, "database error");
    555520                psFree(doneExp);
     
    561526            psU64 affected = psDBAffectedRows(config->dbh);
    562527            if (psDBAffectedRows(config->dbh) < 1) {
    563                 // rollback
    564                 if (!psDBRollback(config->dbh)) {
    565                     psError(PS_ERR_UNKNOWN, false, "database error");
    566                 }
    567528                psError(PS_ERR_UNKNOWN, false, "should have affected at least 1 row but %" PRIu64 " rows were modified", affected);
    568529                psFree(doneExp);
     
    572533        }
    573534
     535        // set pzDownloadExp.state to 'stop'
     536        if (!pzDownloadExpSetState(config, doneExp->exp_name, doneExp->camera, doneExp->telescope, "stop")) {
     537            psError(PS_ERR_UNKNOWN, false, "failed to change pzDownloadExp.state for %s:%s:%s", doneExp->exp_name, doneExp->camera, doneExp->telescope);
     538            psFree(doneExp);
     539            psFree(output);
     540            return false;
     541        }
     542
    574543        psFree(doneExp);
    575544    }
     
    577546    psFree(output);
    578547
    579     if (!psDBCommit(config->dbh)) {
    580         psError(PS_ERR_UNKNOWN, false, "database error");
    581         return false;
    582     }
    583 
    584548    return true;
    585549}
     
    588552{
    589553    // get a list of cameras we've seen exps for
    590     if (!p_psDBRunQuery(config->dbh, "SELECT DISTINCT camera FROM pzPendingImfile")) {
     554    if (!p_psDBRunQuery(config->dbh, "SELECT DISTINCT camera FROM pzDownloadExp")) {
    591555        psError(PS_ERR_UNKNOWN, false, "database error");
    592556        return false;
     
    666630    PXOPT_LOOKUP_BOOL(faulted, config->args, "-faulted", false);
    667631
    668     psString query = psStringCopy("SELECT * FROM pzDoneImfile");
     632    psString query = psStringCopy("SELECT * FROM pzDownloadImfile");
    669633
    670634    if (faulted) {
    671635        // list only faulted rows
    672         psStringAppend(&query, " %s", "WHERE pzDoneImfile.fault != 0");
     636        psStringAppend(&query, " %s", "WHERE pzDownloadImfile.fault != 0");
    673637    } else {
    674638        // don't list faulted rows
    675         psStringAppend(&query, " %s", "WHERE pzDoneImfile.fault = 0");
     639        psStringAppend(&query, " %s", "WHERE pzDownloadImfile.fault = 0");
    676640    }
    677641
    678642    if (psListLength(where->list)) {
    679         psString whereClause = psDBGenerateWhereConditionSQL(where, "pzDoneImfile");
     643        psString whereClause = psDBGenerateWhereConditionSQL(where, "pzDownloadImfile");
    680644        psStringAppend(&query, " AND %s", whereClause);
    681645        psFree(whereClause);
     
    727691    PXOPT_LOOKUP_S16(code, config->args, "-code", true, false);
    728692
    729     if (!pxSetFaultCode(config->dbh, "pzDoneImfile", config->where, code)) {
     693    if (!pxSetFaultCode(config->dbh, "pzDownloadImfile", config->where, code)) {
    730694        psError(PS_ERR_UNKNOWN, false, "failed to set set fault flag");
    731695        return false;
     
    747711
    748712    if (config->where) {
    749         psString whereClause = psDBGenerateWhereConditionSQL(config->where, "pzDoneImfile");
     713        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "pzDownloadImfile");
    750714        psStringAppend(&query, " AND %s", whereClause);
    751715        psFree(whereClause);
     
    768732
    769733
     734static bool pzDownloadExpSetState(pxConfig *config, const char *exp_name, const char *camera, const char *telescope, const char *state)
     735{
     736    PS_ASSERT_PTR_NON_NULL(state, false);
     737
     738    // check that state is a valid string value
     739    if (!(
     740            (strncmp(state, "run", 4) == 0)
     741            || (strncmp(state, "stop", 5) == 0)
     742            || (strncmp(state, "reg", 4) == 0)
     743        )
     744    ) {
     745        psError(PS_ERR_UNKNOWN, false,
     746                "invalid pzDownloadExp state: %s", state);
     747        return false;
     748    }
     749
     750    char *query = "UPDATE pzDownloadExp SET state = '%s' WHERE exp_name = '%s' and camera = '%s' and telescope = '%s'";
     751    if (!p_psDBRunQuery(config->dbh, query, state, exp_name, camera, telescope)) {
     752        psError(PS_ERR_UNKNOWN, false, "failed to change state for %s:%s:%s", exp_name, camera, telescope);
     753        return false;
     754    }
     755
     756    return true;
     757}
     758
     759
    770760#if 0
    771761static psArray *pzArrayAddArray(psArray *array, psArray *input)
Note: See TracChangeset for help on using the changeset viewer.