/* work starts here */ int main (int argc, char **argv) { int rc; /* * allocate options structure * and parse command line */ if(parse_arguments(argc, argv) != GM_OK) { print_usage(); exit( STATE_UNKNOWN ); } /* set logging */ mod_gm_opt->debug_level = GM_LOG_INFO; mod_gm_opt->logmode = GM_LOG_MODE_TOOLS; /* init crypto functions */ if(mod_gm_opt->encryption == GM_ENABLED) { mod_gm_crypt_init(mod_gm_opt->crypt_key); } else { mod_gm_opt->transportmode = GM_ENCODE_ONLY; } /* create client */ if ( create_client( mod_gm_opt->server_list, &client ) != GM_OK ) { printf( "send_multi UNKNOWN: cannot start client\n" ); exit( STATE_UNKNOWN ); } /* create duplicate client */ if ( create_client_dup( mod_gm_opt->dupserver_list, &client_dup ) != GM_OK ) { printf( "send_multi UNKNOWN: cannot start client for duplicate server\n" ); exit( STATE_UNKNOWN ); } /* send result message */ signal(SIGALRM, alarm_sighandler); rc = read_multi_stream(stdin); /* if rc > 0, it contains the number of checks being submitted, otherwise its an error code (-1 - WARNING, -2 - CRITICAL, -3 - UNKNOWN) */ if (rc == 0) { printf( "send_multi UNKNOWN: %d check_multi child checks submitted\n", rc ); rc=STATE_UNKNOWN; } else if (rc > 0) { printf( "send_multi OK: %d check_multi child check%s submitted\n", rc, (rc>1)?"s":"" ); rc=STATE_OK; } else { rc*=-1; } gearman_client_free( &client ); if( mod_gm_opt->dupserver_num ) gearman_client_free( &client_dup ); mod_gm_free_opt(mod_gm_opt); exit( rc ); }
void worker_client(int worker_mode, int indx, int shid, char **env) { #else void worker_client(int worker_mode, int indx, int shid) { #endif gm_log( GM_LOG_TRACE, "%s worker client started\n", (worker_mode == GM_WORKER_STATUS ? "status" : "job" )); /* set signal handlers for a clean exit */ signal(SIGINT, clean_worker_exit); signal(SIGTERM,clean_worker_exit); worker_run_mode = worker_mode; shm_index = indx; shmid = shid; current_pid = getpid(); gethostname(hostname, GM_BUFFERSIZE-1); /* create worker */ if(set_worker(&worker) != GM_OK) { gm_log( GM_LOG_ERROR, "cannot start worker\n" ); clean_worker_exit(0); _exit( EXIT_FAILURE ); } /* create client */ if ( create_client( mod_gm_opt->server_list, &client ) != GM_OK ) { gm_log( GM_LOG_ERROR, "cannot start client\n" ); clean_worker_exit(0); _exit( EXIT_FAILURE ); } /* create duplicate client */ if( mod_gm_opt->dupserver_num ) { if ( create_client_dup( mod_gm_opt->dupserver_list, &client_dup ) != GM_OK ) { gm_log( GM_LOG_ERROR, "cannot start client for duplicate server\n" ); _exit( EXIT_FAILURE ); } } #ifdef EMBEDDEDPERL if(init_embedded_perl(env) == GM_ERROR) { _exit( EXIT_FAILURE ); } #endif worker_loop(); return; }
/* work starts here */ int main (int argc, char **argv) { int rc; /* * allocate options structure * and parse command line */ if(parse_arguments(argc, argv) != GM_OK) { print_usage(); exit( STATE_UNKNOWN ); } /* set logging */ mod_gm_opt->debug_level = GM_LOG_INFO; mod_gm_opt->logmode = GM_LOG_MODE_TOOLS; /* init crypto functions */ if(mod_gm_opt->encryption == GM_ENABLED) { mod_gm_crypt_init(mod_gm_opt->crypt_key); } else { mod_gm_opt->transportmode = GM_ENCODE_ONLY; } /* create client */ if ( create_client( mod_gm_opt->server_list, &client ) != GM_OK ) { printf( "send_gearman UNKNOWN: cannot start client\n" ); exit( STATE_UNKNOWN ); } /* create duplicate client */ if ( create_client_dup( mod_gm_opt->dupserver_list, &client_dup ) != GM_OK ) { printf( "send_gearman UNKNOWN: cannot start client for duplicate server\n" ); exit( STATE_UNKNOWN ); } /* send result message */ signal(SIGALRM, alarm_sighandler); rc = send_result(); gearman_client_free( &client ); if( mod_gm_opt->dupserver_num ) gearman_client_free( &client_dup ); mod_gm_free_opt(mod_gm_opt); exit( rc ); }
/* main loop of jobs */ void worker_loop() { while ( 1 ) { gearman_return_t ret; /* wait for a job, otherwise exit when hit the idle timeout */ if(mod_gm_opt->idle_timeout > 0 && ( worker_run_mode == GM_WORKER_MULTI || worker_run_mode == GM_WORKER_STATUS )) { signal(SIGALRM, idle_sighandler); alarm(mod_gm_opt->idle_timeout); } signal(SIGPIPE, SIG_IGN); ret = gearman_worker_work( &worker ); if ( ret != GEARMAN_SUCCESS ) { gm_log( GM_LOG_ERROR, "worker error: %s\n", gearman_worker_error( &worker ) ); gearman_job_free_all( &worker ); gearman_worker_free( &worker ); gearman_client_free( &client ); if( mod_gm_opt->dupserver_num ) gearman_client_free( &client_dup ); /* sleep on error to avoid cpu intensive infinite loops */ sleep(sleep_time_after_error); sleep_time_after_error += 3; if(sleep_time_after_error > 60) sleep_time_after_error = 60; /* create new connections */ set_worker( &worker ); create_client( mod_gm_opt->server_list, &client ); if( mod_gm_opt->dupserver_num ) create_client_dup( mod_gm_opt->dupserver_list, &client_dup ); } } return; }