task.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2012-2013 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/client.hpp"
  39. #include "libgearman/interface/packet.hpp"
  40. #define TASK_MAGIC 134
  41. #define TASK_ANTI_MAGIC 157
  42. struct Task
  43. {
  44. struct Options {
  45. bool send_in_use;
  46. bool is_known;
  47. bool is_running;
  48. bool was_reduced;
  49. bool is_paused;
  50. Options() :
  51. send_in_use(false),
  52. is_known(false),
  53. is_running(false),
  54. was_reduced(false),
  55. is_paused(false)
  56. { }
  57. ~Options()
  58. {
  59. }
  60. } options;
  61. enum gearman_task_kind_t type;
  62. enum gearman_task_state_t state;
  63. uint32_t magic_;
  64. uint32_t created_id;
  65. uint32_t numerator;
  66. uint32_t denominator;
  67. uint32_t client_count;
  68. Client *client;
  69. gearman_task_st *next;
  70. gearman_task_st *prev;
  71. void *context;
  72. gearman_connection_st *con;
  73. gearman_packet_st *recv;
  74. gearman_packet_st send;
  75. struct gearman_actions_t func;
  76. struct error_st _error;
  77. struct gearman_result_st *_result_ptr;
  78. char job_handle[GEARMAN_JOB_HANDLE_SIZE];
  79. gearman_vector_st exception;
  80. size_t unique_length;
  81. char unique[GEARMAN_MAX_UNIQUE_SIZE];
  82. gearman_task_st* shell()
  83. {
  84. return _shell;
  85. }
  86. Task(Client* client_, gearman_task_st* shell_) :
  87. type(GEARMAN_TASK_KIND_ADD_TASK),
  88. state(GEARMAN_TASK_STATE_NEW),
  89. magic_(TASK_MAGIC),
  90. created_id(0),
  91. numerator(0),
  92. denominator(0),
  93. client_count(0),
  94. client(client_),
  95. next(NULL),
  96. prev(NULL),
  97. context(NULL),
  98. con(NULL),
  99. recv(NULL),
  100. func(client_->actions),
  101. _error(GEARMAN_UNKNOWN_STATE),
  102. _result_ptr(NULL),
  103. unique_length(0),
  104. _shell(shell_)
  105. {
  106. job_handle[0]= 0;
  107. unique[0]= 0;
  108. if (_shell)
  109. {
  110. gearman_set_allocated(_shell, false);
  111. }
  112. else
  113. {
  114. _shell= &_owned_shell;
  115. gearman_set_allocated(_shell, true);
  116. }
  117. // Add the task to the client
  118. {
  119. if (client_->task_list)
  120. {
  121. client_->task_list->impl()->prev= _shell;
  122. }
  123. next= client_->task_list;
  124. prev= NULL;
  125. client_->task_list= _shell;
  126. client_->task_count++;
  127. }
  128. _shell->impl(this);
  129. }
  130. ~Task();
  131. gearman_result_st* result()
  132. {
  133. return _result_ptr;
  134. }
  135. void result(gearman_result_st* result_);
  136. void free_result()
  137. {
  138. result(NULL);
  139. }
  140. bool create_result(size_t initial_size);
  141. void set_state(const enum gearman_task_state_t state_)
  142. {
  143. state= state_;
  144. }
  145. gearman_return_t error_code(const gearman_return_t rc_)
  146. {
  147. return _error.error_code(rc_);
  148. }
  149. gearman_return_t error_code(const gearman_return_t rc_, const char*)
  150. {
  151. return _error.error_code(rc_);
  152. }
  153. gearman_return_t error_code() const
  154. {
  155. return _error.error_code();
  156. }
  157. const char* error() const
  158. {
  159. return _error.error();
  160. }
  161. bool is_finished() const
  162. {
  163. switch (state)
  164. {
  165. case GEARMAN_TASK_STATE_NEW:
  166. case GEARMAN_TASK_STATE_SUBMIT:
  167. case GEARMAN_TASK_STATE_WORKLOAD:
  168. case GEARMAN_TASK_STATE_WORK:
  169. case GEARMAN_TASK_STATE_CREATED:
  170. case GEARMAN_TASK_STATE_DATA:
  171. case GEARMAN_TASK_STATE_WARNING:
  172. case GEARMAN_TASK_STATE_STATUS:
  173. return false;
  174. case GEARMAN_TASK_STATE_COMPLETE:
  175. case GEARMAN_TASK_STATE_EXCEPTION:
  176. case GEARMAN_TASK_STATE_FAIL:
  177. case GEARMAN_TASK_STATE_FINISHED:
  178. return true;
  179. }
  180. return false;
  181. }
  182. private:
  183. gearman_task_st* _shell;
  184. gearman_task_st _owned_shell;
  185. };