Exemplo n.º 1
0
/* create the gearman worker */
int create_worker( char ** server_list, gearman_worker_st *worker ) {
    int x = 0;

    gearman_return_t ret;
    signal(SIGPIPE, SIG_IGN);

    worker = gearman_worker_create( worker );
    if ( worker == NULL ) {
        gm_log( GM_LOG_ERROR, "Memory allocation failure on worker creation\n" );
        return GM_ERROR;
    }

    while ( server_list[x] != NULL ) {
        char * server   = strdup( server_list[x] );
        char * server_c = server;
        char * host     = strsep( &server, ":" );
        char * port_val = strsep( &server, "\x0" );
        in_port_t port  = GM_SERVER_DEFAULT_PORT;
        if(port_val != NULL) {
            port  = ( in_port_t ) atoi( port_val );
        }
        ret = gearman_worker_add_server( worker, host, port );
        if ( ret != GEARMAN_SUCCESS ) {
            gm_log( GM_LOG_ERROR, "worker error: %s\n", gearman_worker_error( worker ) );
            free(server_c);
            return GM_ERROR;
        }
        free(server_c);
        x++;
    }
    assert(x != 0);

    return GM_OK;
}
Exemplo n.º 2
0
/* callback for task completed */
void *result_worker( void * data ) {
    gearman_worker_st worker;
    int *worker_num = (int*)data;

    gm_log( GM_LOG_TRACE, "worker %d started\n", *worker_num );

    pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL);
    pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);

    set_worker(&worker);

    pthread_cleanup_push ( cancel_worker_thread, (void*) &worker);

    while ( 1 ) {
        gearman_return_t ret;
        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 );

            sleep(1);
            set_worker(&worker );
        }
    }

    pthread_cleanup_pop(0);
    return NULL;
}
Exemplo n.º 3
0
int main(void){
    gearman_worker_st worker;
	
	//Create the worker
	gearman_worker_create(&worker);
    
	//Connect the worker to the server and chech it worked
    gearman_return_t ret= gearman_worker_add_server(&worker, "localhost", 4730);
    if (gearman_failed(ret)){
        return EXIT_FAILURE;
    }
	
	//register function, (worker, job_queue_name, timeout, function_to_do_work, context)
    ret = gearman_worker_add_function(&worker,"image",0,doing_work,NULL);
	if(gearman_failed(ret)){
		return EXIT_FAILURE;
	}
	
	//Infinite loop to keep watching
    while(1){
		//Does the work
        ret = gearman_worker_work(&worker);
        if ( ret != GEARMAN_SUCCESS ) {
            fprintf(stderr,"worker error: %s\n", gearman_worker_error( &worker ) );
            gearman_job_free_all( &worker );
            gearman_worker_free( &worker );
            /* sleep on error to avoid cpu intensive infinite loops */
            sleep(5);
        }
    }
    return EXIT_SUCCESS;
}
Exemplo n.º 4
0
/* register function on worker */
int worker_add_function( gearman_worker_st * worker, char * queue, gearman_worker_fn *function) {
    gearman_return_t ret;
    ret = gearman_worker_add_function( worker, queue, 0, function, NULL );
    if ( ret != GEARMAN_SUCCESS ) {
        gm_log( GM_LOG_ERROR, "worker error: %s\n", gearman_worker_error( worker ) );
        return GM_ERROR;
    }

    return GM_OK;
}
Exemplo n.º 5
0
int main() {
	gearman_return_t ret;
	gearman_worker_st *worker = gearman_worker_create(NULL);
	gearman_worker_add_server(worker,"127.0.0.1",0);

	gearman_worker_add_function(worker,"syncToRedis",30, syncToRedis, NULL);
	gearman_worker_add_function(worker,"sync_UserInfo",30, sync_UserInfo, NULL);
	gearman_worker_add_function(worker,"sync_UserAndDeviceRelationShip",30, sync_UserAndDeviceRelationShip, NULL);

	while(1) {
		ret = gearman_worker_work(worker);
		if (ret != GEARMAN_SUCCESS) {
			printf("%s\n", gearman_worker_error(worker));
			break;
		}
	}
}
Exemplo n.º 6
0
/* 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;
}
Exemplo n.º 7
0
/* create the gearman worker */
int create_worker( gm_server_t * server_list[GM_LISTSIZE], gearman_worker_st *worker ) {
    int x = 0;

    gearman_return_t ret;
    signal(SIGPIPE, SIG_IGN);

    gearman_worker_create( worker );
    if ( worker == NULL ) {
        gm_log( GM_LOG_ERROR, "Memory allocation failure on worker creation\n" );
        return GM_ERROR;
    }

    while ( server_list[x] != NULL ) {
        ret = gearman_worker_add_server( worker, server_list[x]->host, server_list[x]->port );
        if ( ret != GEARMAN_SUCCESS ) {
            gm_log( GM_LOG_ERROR, "worker error: %s\n", gearman_worker_error( worker ) );
            return GM_ERROR;
        }
        x++;
    }
    assert(x != 0);

    return GM_OK;
}
Exemplo n.º 8
0
int main(int argc, char *argv[])
{
  gearman_benchmark_st benchmark;
  int c;
  char *host= NULL;
  in_port_t port= 0;
  char *function= NULL;
  uint32_t count= 0;
  gearman_return_t ret;
  gearman_worker_st worker;

  benchmark_init(&benchmark);

  if (gearman_worker_create(&worker) == NULL)
  {
    fprintf(stderr, "Memory allocation failure on worker creation\n");
    exit(1);
  }

  while ((c = getopt(argc, argv, "c:f:h:p:v")) != -1)
  {
    switch(c)
    {
    case 'c':
      count= (uint32_t)atoi(optarg);
      break;

    case 'f':
      function= optarg;
      ret= gearman_worker_add_function(&worker, function, 0, worker_fn,
                                       &benchmark);
      if (ret != GEARMAN_SUCCESS)
      {
        fprintf(stderr, "%s\n", gearman_worker_error(&worker));
        exit(1);
      }
      break;

    case 'h':
      host= optarg;
      ret= gearman_worker_add_server(&worker, host, port);
      if (ret != GEARMAN_SUCCESS)
      {
        fprintf(stderr, "%s\n", gearman_worker_error(&worker));
        exit(1);
      }
      break;

    case 'p':
      port= (in_port_t)atoi(optarg);
      break;

    case 'v':
      benchmark.verbose++;
      break;

    default:
      usage(argv[0]);
      exit(1);
    }
  }

  if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
  {
    fprintf(stderr, "signal:%d\n", errno);
    exit(1);
  }

  if (host == NULL)
  {
    ret= gearman_worker_add_server(&worker, NULL, port);
    if (ret != GEARMAN_SUCCESS)
    {
      fprintf(stderr, "%s\n", gearman_worker_error(&worker));
      exit(1);
    }
  }

  if (function == NULL)
  {
    ret= gearman_worker_add_function(&worker,
                                     GEARMAN_BENCHMARK_DEFAULT_FUNCTION, 0,
                                     worker_fn, &benchmark);
    if (ret != GEARMAN_SUCCESS)
    {
      fprintf(stderr, "%s\n", gearman_worker_error(&worker));
      exit(1);
    }
  }

  while (1)
  {
    ret= gearman_worker_work(&worker);
    if (ret != GEARMAN_SUCCESS)
    {
      fprintf(stderr, "%s\n", gearman_worker_error(&worker));
      break;
    }

    if (count > 0)
    {
      count--;
      if (count == 0)
        break;
    }
  }

  gearman_worker_free(&worker);

  return 0;
}
Exemplo n.º 9
0
int main()
{
  if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
  {
    std::cerr << "signal:" << strerror(errno) << std::endl;
    return EXIT_FAILURE;
  }
  gearman_worker_st *worker;
  if ((worker= gearman_worker_create(NULL)) == NULL)
  {
    std::cerr << "Memory allocation failure on worker creation." << std::endl;
    return EXIT_FAILURE;
  }

  //if (unique)
  //{
    gearman_worker_add_options(worker, GEARMAN_WORKER_GRAB_UNIQ);
  //}

  int timeout=-1;
  //
  if (timeout >= 0)
  {
    gearman_worker_set_timeout(worker, timeout);
  }

  if (gearman_failed(gearman_worker_add_server(worker, "localhost", GEARMAN_DEFAULT_TCP_PORT)))
  {
    std::cerr << gearman_worker_error(worker) << std::endl;
    return EXIT_FAILURE;
  }
  if (gearman_failed(gearman_worker_set_identifier(worker, "worker1", strlen("worker1"))))
  {
    std::cerr << gearman_worker_error(worker) << std::endl;
    return EXIT_FAILURE;
  }
  gearman_function_t worker_fn= gearman_function_create(worker_function);
  if (gearman_failed(gearman_worker_define_function(worker,
                                                    gearman_literal_param("make_call"),
                                                    worker_fn,
                                                    0, 
                                                    NULL/*&options*/)))
  {
    std::cerr << gearman_worker_error(worker) << std::endl;
    return EXIT_FAILURE;
  }

  int limit=0;
  // Add one if count is not zero
  if (limit != 0)
  {
    limit++;
  }

  while (--limit)
  {
    if (gearman_failed(gearman_worker_work(worker)))
    {
      std::cerr << gearman_worker_error(worker) << std::endl;
      break;
    }
  }

  gearman_worker_free(worker);

  return EXIT_SUCCESS;

}