~mm-yuhu/gearmand/server-funcs

25 by Eric Day
A few bug fixes, added background example.
1
/* Gearman server and library
2
 * Copyright (C) 2008 Brian Aker, Eric Day
39 by Eric Day
Switched to BSD license, started cleaning up test suite.
3
 * All rights reserved.
4
 *
5
 * Use and distribution licensed under the BSD license.  See
6
 * the COPYING file in the parent directory for full text.
25 by Eric Day
A few bug fixes, added background example.
7
 */
8
38 by Eric Day
Updated docs.
9
/**
10
 * @file
11
 * @brief Example Background Client
12
 */
13
25 by Eric Day
A few bug fixes, added background example.
14
#include <stdio.h>
15
#include <stdlib.h>
16
#include <string.h>
17
#include <unistd.h>
18
19
#include <libgearman/gearman.h>
20
21
static void usage(char *name);
22
23
int main(int argc, char *argv[])
24
{
25
  char c;
26
  char *host= NULL;
27
  unsigned short port= 0;
29 by Eric Day
Fixed enum types to have _t suffix.
28
  gearman_return_t ret;
25 by Eric Day
A few bug fixes, added background example.
29
  gearman_client_st client;
30
  char job_handle[GEARMAN_JOB_HANDLE_SIZE];
31
  bool is_known;
32
  bool is_running;
33
  uint32_t numerator;
34
  uint32_t denominator;
35
19.1.6 by Brian Aker
Merge of Eric's work (small fixes in style)
36
  while ((c = getopt(argc, argv, "h:p:")) != EOF)
25 by Eric Day
A few bug fixes, added background example.
37
  {
38
    switch(c)
39
    {
40
    case 'h':
41
      host= optarg;
42
      break;
43
44
    case 'p':
45
      port= atoi(optarg);
46
      break;
47
48
    default:
49
      usage(argv[0]);
50
      exit(1);
51
    }
52
  }
53
54
  if(argc != (optind + 1))
55
  {
56
    usage(argv[0]);
57
    exit(1);
58
  }
59
33 by Eric Day
Merged changes from Brian, removed gearman_st from create and clone functions.
60
  if (gearman_client_create(&client) == NULL)
61
  {
62
    fprintf(stderr, "Memory allocation failure on client creation\n");
63
    exit(1);
64
  }
25 by Eric Day
A few bug fixes, added background example.
65
66
  ret= gearman_client_add_server(&client, host, port);
67
  if (ret != GEARMAN_SUCCESS)
68
  {
69
    fprintf(stderr, "%s\n", gearman_client_error(&client));
70
    exit(1);
71
  }
72
55 by Eric Day
Added unique option to client do/task functions.
73
  ret= gearman_client_do_background(&client, "reverse", NULL,
74
                                    (void *)argv[optind],
19.1.4 by Brian Aker
Cleanup tests/interfaces
75
                                    (size_t)strlen(argv[optind]), job_handle);
25 by Eric Day
A few bug fixes, added background example.
76
  if (ret != GEARMAN_SUCCESS)
77
  {
78
    fprintf(stderr, "%s\n", gearman_client_error(&client));
79
    exit(1);
80
  }
81
82
  printf("Background Job Handle=%s\n", job_handle);
83
84
  while (1)
85
  {
61 by Eric Day
Added NULL workload test, user supplied malloc/free call for workload, other cleanup.
86
    ret= gearman_client_job_status(&client, job_handle, &is_known, &is_running,
87
                                   &numerator, &denominator);
25 by Eric Day
A few bug fixes, added background example.
88
    if (ret != GEARMAN_SUCCESS)
89
    {
90
      fprintf(stderr, "%s\n", gearman_client_error(&client));
91
      exit(1);
92
    }
93
94
    printf("Known=%s, Running=%s, Percent Complete=%u/%u\n",
95
            is_known ? "true" : "false", is_running ? "true" : "false",
96
            numerator, denominator);
97
98
    if (!is_known)
99
      break;
100
101
    sleep(1);
102
  }
103
104
  gearman_client_free(&client);
105
106
  return 0;
107
}
108
109
static void usage(char *name)
110
{
111
  printf("\nusage: %s [-h <host>] [-p <port>] <string>\n", name);
112
  printf("\t-h <host> - job server host\n");
113
  printf("\t-p <port> - job server port\n");
114
}