Gearman Public API Documentation

examples/reverse_client_bg.c
Go to the documentation of this file.
00001 /* Gearman server and library
00002  * Copyright (C) 2008 Brian Aker, Eric Day
00003  * All rights reserved.
00004  *
00005  * Use and distribution licensed under the BSD license.  See
00006  * the COPYING file in the parent directory for full text.
00007  */
00008 
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 #include <string.h>
00017 #include <unistd.h>
00018 
00019 #include <libgearman/gearman.h>
00020 
00021 static void usage(char *name);
00022 
00023 int main(int argc, char *argv[])
00024 {
00025   int c;
00026   char *host= NULL;
00027   in_port_t port= 0;
00028   gearman_return_t ret;
00029   gearman_client_st client;
00030   char job_handle[GEARMAN_JOB_HANDLE_SIZE];
00031   bool is_known;
00032   bool is_running;
00033   uint32_t numerator;
00034   uint32_t denominator;
00035 
00036   while ((c = getopt(argc, argv, "h:p:")) != -1)
00037   {
00038     switch(c)
00039     {
00040     case 'h':
00041       host= optarg;
00042       break;
00043 
00044     case 'p':
00045       port= (in_port_t)atoi(optarg);
00046       break;
00047 
00048     default:
00049       usage(argv[0]);
00050       exit(1);
00051     }
00052   }
00053 
00054   if(argc != (optind + 1))
00055   {
00056     usage(argv[0]);
00057     exit(1);
00058   }
00059 
00060   if (gearman_client_create(&client) == NULL)
00061   {
00062     fprintf(stderr, "Memory allocation failure on client creation\n");
00063     exit(1);
00064   }
00065 
00066   ret= gearman_client_add_server(&client, host, port);
00067   if (ret != GEARMAN_SUCCESS)
00068   {
00069     fprintf(stderr, "%s\n", gearman_client_error(&client));
00070     exit(1);
00071   }
00072 
00073   ret= gearman_client_do_background(&client, "reverse", NULL,
00074                                     (void *)argv[optind],
00075                                     (size_t)strlen(argv[optind]), job_handle);
00076   if (ret != GEARMAN_SUCCESS)
00077   {
00078     fprintf(stderr, "%s\n", gearman_client_error(&client));
00079     exit(1);
00080   }
00081 
00082   printf("Background Job Handle=%s\n", job_handle);
00083 
00084   while (1)
00085   {
00086     ret= gearman_client_job_status(&client, job_handle, &is_known, &is_running,
00087                                    &numerator, &denominator);
00088     if (ret != GEARMAN_SUCCESS)
00089     {
00090       fprintf(stderr, "%s\n", gearman_client_error(&client));
00091       exit(1);
00092     }
00093 
00094     printf("Known=%s, Running=%s, Percent Complete=%u/%u\n",
00095             is_known ? "true" : "false", is_running ? "true" : "false",
00096             numerator, denominator);
00097 
00098     if (!is_known)
00099       break;
00100 
00101     sleep(1);
00102   }
00103 
00104   gearman_client_free(&client);
00105 
00106   return 0;
00107 }
00108 
00109 static void usage(char *name)
00110 {
00111   printf("\nusage: %s [-h <host>] [-p <port>] <string>\n", name);
00112   printf("\t-h <host> - job server host\n");
00113   printf("\t-p <port> - job server port\n");
00114 }