worker.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
  6. * Copyright (C) 2008 Brian Aker, Eric Day
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are
  11. * met:
  12. *
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * * Redistributions in binary form must reproduce the above
  17. * copyright notice, this list of conditions and the following disclaimer
  18. * in the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * * The names of its contributors may not be used to endorse or
  22. * promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. /**
  39. * @file
  40. * @brief Server worker definitions
  41. */
  42. #include "gear_config.h"
  43. #include "libgearman-server/common.h"
  44. #include <memory>
  45. static gearman_server_worker_st* gearman_server_worker_create(gearman_server_con_st *con, gearman_server_function_st *function)
  46. {
  47. gearman_server_worker_st *worker;
  48. if (Server->free_worker_count > 0)
  49. {
  50. worker= Server->free_worker_list;
  51. GEARMAND_LIST_DEL(Server->free_worker, worker, con_);
  52. }
  53. else
  54. {
  55. worker= new (std::nothrow) gearman_server_worker_st;
  56. if (worker == NULL)
  57. {
  58. gearmand_merror("new", gearman_server_worker_st, 1);
  59. return NULL;
  60. }
  61. }
  62. worker->job_count= 0;
  63. worker->timeout= -1;
  64. worker->con= con;
  65. GEARMAND_LIST_ADD(con->worker, worker, con_);
  66. worker->function= function;
  67. /* Add worker to the function list, which is a double-linked circular list. */
  68. if (function->worker_list == NULL)
  69. {
  70. function->worker_list= worker;
  71. worker->function_next= worker;
  72. worker->function_prev= worker;
  73. }
  74. else
  75. {
  76. worker->function_next= function->worker_list;
  77. worker->function_prev= function->worker_list->function_prev;
  78. worker->function_next->function_prev= worker;
  79. worker->function_prev->function_next= worker;
  80. }
  81. function->worker_count++;
  82. worker->job_list= NULL;
  83. return worker;
  84. }
  85. /*
  86. * Public definitions
  87. */
  88. gearman_server_worker_st *
  89. gearman_server_worker_add(gearman_server_con_st *con, const char *function_name,
  90. size_t function_name_size, long timeout)
  91. {
  92. gearman_server_function_st *function= gearman_server_function_get(Server, function_name,
  93. function_name_size);
  94. if (function == NULL)
  95. {
  96. return NULL;
  97. }
  98. gearman_server_worker_st* worker= gearman_server_worker_create(con, function);
  99. if (worker == NULL)
  100. {
  101. return NULL;
  102. }
  103. worker->timeout= timeout;
  104. return worker;
  105. }
  106. void gearman_server_worker_free(gearman_server_worker_st *worker)
  107. {
  108. /* If the worker was in the middle of a job, requeue it. */
  109. while (worker->job_list != NULL)
  110. {
  111. gearmand_error_t ret= gearman_server_job_queue(worker->job_list);
  112. if (ret != GEARMAND_SUCCESS)
  113. {
  114. gearmand_gerror_warn("gearman_server_job_queue", ret);
  115. }
  116. }
  117. GEARMAND_LIST_DEL(worker->con->worker, worker, con_);
  118. if (worker == worker->function_next)
  119. {
  120. worker->function->worker_list= NULL;
  121. }
  122. else
  123. {
  124. worker->function_next->function_prev= worker->function_prev;
  125. worker->function_prev->function_next= worker->function_next;
  126. if (worker == worker->function->worker_list)
  127. {
  128. worker->function->worker_list= worker->function_next;
  129. }
  130. }
  131. worker->function->worker_count--;
  132. if (Server->free_worker_count < GEARMAND_MAX_FREE_SERVER_WORKER)
  133. {
  134. GEARMAND_LIST_ADD(Server->free_worker, worker, con_);
  135. }
  136. else
  137. {
  138. gearmand_debug("delete");
  139. delete worker;
  140. }
  141. }