/***********************************************************

DBUpdateJob(): Update a particular job in the DB.

JobId = jq_pk

Possible update modes:

0 = working on it

1 = completed

2 = mark for repeat

3 = failure

***********************************************************/

void DBUpdateJob ( int JobId , int UpdateType , char * Message )

{

int rc ;

char SQL [ MAXCMD ] ;

int Len ;

memset ( SQL , ' \0 ' , MAXCMD ) ;

switch ( UpdateType )

{

case 0 : /* mark the DB entry as work in progress */

snprintf ( SQL , MAXCMD , "UPDATE jobqueue SET jq_starttime = now(), jq_schedinfo ='%s.%d'" , Hostname , getpid ( ) ) ;

break ;

case 1 : /* mark the DB entry as completed */

/* If you see endtime without starttime, then this is the culprit */

snprintf ( SQL , MAXCMD , "UPDATE jobqueue SET jq_endtime = now(), jq_end_bits = jq_end_bits | 1, jq_schedinfo = null" ) ;

break ;

case 3 : /* mark the DB entry as ready to fail */

snprintf ( SQL , MAXCMD , "UPDATE jobqueue SET jq_endtime = now(), jq_end_bits = jq_end_bits | 2, jq_schedinfo = null" ) ;

break ;

case 2 : /* mark the DB entry as ready to try again */

default :

snprintf ( SQL , MAXCMD , "UPDATE jobqueue SET jq_starttime = null, jq_endtime = null, jq_schedinfo = null" ) ;

break ;

}

Len = strlen ( SQL ) ;

if ( Message )

{

snprintf ( SQL + Len , MAXCMD - Len , ", jq_endtext = " ) ;

Len = strlen ( SQL ) ;

switch ( UpdateType )

{

case 0 : snprintf ( SQL + Len , MAXCMD - Len , "'Started: %s'" , Message ) ; break ;

case 1 : snprintf ( SQL + Len , MAXCMD - Len , "'Completed: %s'" , Message ) ; break ;

case 2 : snprintf ( SQL + Len , MAXCMD - Len , "'Restart: %s'" , Message ) ; break ;

case 3 : snprintf ( SQL + Len , MAXCMD - Len , "'Failed: %s'" , Message ) ; break ;

default :

snprintf ( SQL + Len , MAXCMD - Len , "'%s'" , Message ) ;

break ;

}

}

else

{

snprintf ( SQL + Len , MAXCMD - Len , ", jq_endtext = " ) ;

Len = strlen ( SQL ) ;

switch ( UpdateType )

{

case 0 : snprintf ( SQL + Len , MAXCMD - Len , "'Started'" ) ; break ;

case 1 : snprintf ( SQL + Len , MAXCMD - Len , "'Completed'" ) ; break ;

case 2 : snprintf ( SQL + Len , MAXCMD - Len , "'Restart'" ) ; break ;

case 3 : snprintf ( SQL + Len , MAXCMD - Len , "'Failed'" ) ; break ;

default :

break ;

}

}

Len = strlen ( SQL ) ;

snprintf ( SQL + Len , MAXCMD - Len , " WHERE jq_pk = '%d';" , JobId ) ;

if ( Verbose )

{

LogPrint ( "SQL Update: '%s'

" , SQL ) ;

}

rc = DBLockAccess ( DB , SQL ) ;

if ( rc >= 0 ) return ;

/* How to handle a DB error? Right now, they are just logged per agent */

/* TBD: This will be implemented when we have interprocess communication

between the scheduler and UI. */

LogPrint ( "ERROR: Unable to process: '%s'

" , SQL ) ;

return ;