worker.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2012 Data Differential, http://datadifferential.com/
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following disclaimer
  17. * in the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * The names of its contributors may not be used to endorse or
  21. * promote products derived from this software without specific prior
  22. * written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. */
  37. #pragma once
  38. #include "libgearman/interface/universal.hpp"
  39. #include "libgearman/interface/packet.hpp"
  40. struct Worker
  41. {
  42. struct Options {
  43. bool non_blocking;
  44. bool packet_init;
  45. bool change;
  46. bool grab_uniq;
  47. bool grab_all;
  48. bool timeout_return;
  49. bool _in_work;
  50. Options() :
  51. non_blocking(false),
  52. packet_init(false),
  53. change(false),
  54. grab_uniq(true),
  55. grab_all(true),
  56. timeout_return(false),
  57. _in_work(false)
  58. { }
  59. } options;
  60. enum gearman_worker_state_t state;
  61. enum gearman_worker_universal_t work_state;
  62. uint32_t function_count;
  63. uint32_t job_count;
  64. size_t work_result_size;
  65. void *context;
  66. gearman_connection_st *con;
  67. Job *_job;
  68. Job *job_list;
  69. struct _worker_function_st *function;
  70. struct _worker_function_st *function_list;
  71. struct _worker_function_st *work_function;
  72. void *work_result;
  73. struct gearman_universal_st universal;
  74. gearman_packet_st grab_job;
  75. gearman_packet_st pre_sleep;
  76. Job *_work_job;
  77. Worker(gearman_worker_st* shell_) :
  78. state(GEARMAN_WORKER_STATE_START),
  79. work_state(GEARMAN_WORKER_WORK_UNIVERSAL_GRAB_JOB),
  80. function_count(0),
  81. job_count(0),
  82. work_result_size(0),
  83. context(NULL),
  84. con(NULL),
  85. _job(NULL),
  86. job_list(NULL),
  87. function(NULL),
  88. function_list(NULL),
  89. work_function(NULL),
  90. work_result(NULL),
  91. _work_job(NULL),
  92. _shell(shell_)
  93. {
  94. if (shell_)
  95. {
  96. gearman_set_allocated(_shell, false);
  97. }
  98. else
  99. {
  100. _shell= &_owned_shell;
  101. gearman_set_allocated(_shell, true);
  102. }
  103. _shell->impl(this);
  104. gearman_set_initialized(_shell, true);
  105. }
  106. ~Worker()
  107. {
  108. }
  109. gearman_worker_st* shell()
  110. {
  111. return _shell;
  112. }
  113. gearman_job_st* job();
  114. gearman_job_st* take_job();
  115. void job(gearman_job_st* job_);
  116. gearman_job_st* work_job();
  117. bool has_work_job() const
  118. {
  119. return bool(_work_job);
  120. }
  121. void work_job(gearman_job_st* work_job_);
  122. bool ssl() const
  123. {
  124. return universal.options._ssl;
  125. }
  126. void ssl(bool ssl_)
  127. {
  128. universal.options._ssl= ssl_;
  129. }
  130. bool in_work() const
  131. {
  132. return options._in_work;
  133. }
  134. void in_work(bool in_work_)
  135. {
  136. options._in_work= in_work_;
  137. }
  138. bool has_identifier() const
  139. {
  140. return universal.has_identifier();
  141. }
  142. const char* error() const
  143. {
  144. return universal.error();
  145. }
  146. gearman_return_t error_code() const
  147. {
  148. return universal.error_code();
  149. }
  150. private:
  151. gearman_worker_st* _shell;
  152. gearman_worker_st _owned_shell;
  153. };