Skip to content

Commit

Permalink
use ./cleanexit file to ensure gb doesn't restart
Browse files Browse the repository at this point in the history
after a graceful exit in the bash keep alive loop.
  • Loading branch information
gigablast committed Mar 16, 2016
1 parent 7396e57 commit a2e8a3a
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 9 deletions.
12 changes: 12 additions & 0 deletions File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,18 @@ time_t File::getLastModifiedTime ( ) {
return 0;
}

bool doesFileExist ( char *filename ) {
// allow the substitution of another filename
struct stat stats;
// return true if it exists
if ( stat ( filename , &stats ) == 0 ) return true;
// return 0 if it just does not exist and reset g_errno
if ( errno == ENOENT ) return false;
// resource temporarily unavailable (for newer libc)
if ( errno == EAGAIN ) return false;
// error
return false;
}

// . returns -1 on error
// . returns 0 if does not exist
Expand Down
2 changes: 2 additions & 0 deletions File.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include "Loop.h" // for g_loop.setNonBlocking(int fd)
#include "SafeBuf.h"

bool doesFileExist ( char *filename ) ;

int64_t getFileSize ( char *filename ) ;

int64_t getFileSize_cygwin ( char *filename ) ;
Expand Down
25 changes: 24 additions & 1 deletion Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1777,11 +1777,34 @@ bool Process::shutdown2 ( ) {
log(LOG_INFO,"gb: still has hdtemp thread");


log("gb. EXITING.");
log("gb. EXITING GRACEFULLY.");

// from main.cpp:
// extern SafeBuf g_pidFileName;
// extern bool g_createdPidFile;
// // first remove the pid file on graceful exit
// // remove pid file if we created it
// // take from main.cpp
// if ( g_createdPidFile && g_pidFileName.length() )
// ::unlink ( g_pidFileName.getBufStart() );

// make a file called 'cleanexit' so bash keep alive loop will stop
// because bash does not get the correct exit code, 0 in this case,
// even though we explicitly say 'exit(0)' !!!! poop
char tmp[128];
SafeBuf cleanFileName(tmp,128);
cleanFileName.safePrintf("%s/cleanexit",g_hostdb.m_dir);
SafeBuf nothing;
// returns # of bytes written, -1 if could not create file
if ( nothing.save ( cleanFileName.getBufStart() ) == -1 )
log("gb: could not create %s",cleanFileName.getBufStart());


// exit abruptly
exit(0);

// let's return control to Loop.cpp?

// keep compiler happy
return true;
}
Expand Down
71 changes: 63 additions & 8 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,22 @@ void stack_test(){

int main2 ( int argc , char *argv[] ) ;

// SafeBuf g_pidFileName;
// bool g_createdPidFile = false;

int main ( int argc , char *argv[] ) {

//fprintf(stderr,"Starting gb.\n");

int ret = main2 ( argc , argv );

if ( ret ) fprintf(stderr,"Failed to start gb. Exiting.\n");
// returns 1 if failed, 0 on successful/graceful exit
if ( ret )
fprintf(stderr,"Failed to start gb. Exiting.\n");

// remove pid file if we created it
// if ( g_createdPidFile && ret == 0 && g_pidFileName.length() )
// ::unlink ( g_pidFileName.getBufStart() );
}

int main2 ( int argc , char *argv[] ) {
Expand Down Expand Up @@ -3089,6 +3098,10 @@ int main2 ( int argc , char *argv[] ) {
return 1;

int32_t *ips;
// char tmp[64];
// SafeBuf pidFile(tmp,64);
char tmp[128];
SafeBuf cleanFileName(tmp,128);

//if ( strcmp ( cmd , "gendbs" ) == 0 ) goto jump;
//if ( strcmp ( cmd , "gentfndb" ) == 0 ) goto jump;
Expand All @@ -3097,6 +3110,35 @@ int main2 ( int argc , char *argv[] ) {
// if ( cmd && ! is_digit(cmd[0]) ) goto printHelp;


// if pid file is there then do not start up
// g_pidFileName.safePrintf("%spidfile",g_hostdb.m_dir );
// if ( doesFileExist ( g_pidFileName.getBufStart() ) ) {
// fprintf(stderr,"pidfile %s exists. Either another gb "
// "is already running in this directory or "
// "it exited uncleanly. Can not start up if that "
// "file exists.",
// g_pidFileName.getBufStart() );
// // if we return 0 then main() should not delete the pidfile
// return 0;
// }
// // make a new pidfile
// pidFile.safePrintf("%i\n",getpid());
// if ( ! pidFile.save ( g_pidFileName.getBufStart() ) ) {
// log("db: could not save %s",g_pidFileName.getBufStart());
// return 1;
// }
// // ok, now if we exit SUCCESSFULLY then delete it. we return an
// // exit status of 0
// g_createdPidFile = true;


// remove the file called 'cleanexit' so if we get killed suddenly
// the bashloop will know we did not exit cleanly
cleanFileName.safePrintf("%s/cleanexit",g_hostdb.m_dir);
::unlink ( cleanFileName.getBufStart() );



log("db: Logging to file %s.",
g_hostdb.m_logFilename );

Expand Down Expand Up @@ -5212,7 +5254,11 @@ int install ( install_flag_konst_t installFlag , int32_t hostId , char *dir ,
// execute it
system ( tmp );
}
else if ( installFlag == ifk_kstart ) {
else if ( installFlag == ifk_kstart ||
installFlag == ifk_dstart ) {
char *extraBreak = "";
if ( installFlag == ifk_dstart )
extraBreak = "break;";
//keepalive
// . save old log now, too
//char tmp2[1024];
Expand All @@ -5235,9 +5281,10 @@ int install ( install_flag_konst_t installFlag , int32_t hostId , char *dir ,
"cp -f gb gb.oldsave ; "
"ADDARGS='' "
"INC=1 "
"EXITSTATUS=1 ; "
"while [ \\$EXITSTATUS != 0 ]; do "
"{ "
//"EXITSTATUS=1 "
" ; "
"while true; do "
//"{ "

// if gb still running, then do not try to
// run it again. we
Expand Down Expand Up @@ -5272,10 +5319,16 @@ int install ( install_flag_konst_t installFlag , int32_t hostId , char *dir ,
//" >& ./log%03"INT32""
" ;"

"EXITSTATUS=\\$? ; "
// this doesn't always work so use
// the cleanexit file approach
//"EXITSTATUS=\\$? ; "

// stop if ./cleanexit is there
"if [ -f \"./cleanexit\" ]; then break; fi;"
"%s"
"ADDARGS='-r'\\$INC ; "
"INC=\\$((INC+1));"
"} "
//"} "
"done >& /dev/null & \" %s",
//"done & \" %s",
//"\" %s",
Expand All @@ -5291,7 +5344,7 @@ int install ( install_flag_konst_t installFlag , int32_t hostId , char *dir ,
h2->m_hostId ,

//h2->m_dir ,

extraBreak ,
// hostid is now inferred from path
//h2->m_hostId ,
amp );
Expand All @@ -5302,6 +5355,7 @@ int install ( install_flag_konst_t installFlag , int32_t hostId , char *dir ,
// execute it
system ( tmp );
}
/*
else if ( installFlag == ifk_dstart ) {
//keepalive
// . save old log now, too
Expand Down Expand Up @@ -5364,6 +5418,7 @@ int install ( install_flag_konst_t installFlag , int32_t hostId , char *dir ,
// execute it
system ( tmp );
}
*/
/*
else if ( installFlag == ifk_gendbs ) {
// . save old log now, too
Expand Down

0 comments on commit a2e8a3a

Please sign in to comment.