actions.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011 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. #include "gear_config.h"
  38. #include <libgearman/common.h>
  39. #include <libgearman/unique.hpp>
  40. #include <libgearman/result.hpp>
  41. #include "libgearman/assert.hpp"
  42. #include "libgearman/vector.h"
  43. #include <memory>
  44. struct gearman_result_st;
  45. static gearman_return_t _client_pause_data(gearman_task_st* shell)
  46. {
  47. Task* task= shell->impl();
  48. if (task->options.is_paused)
  49. {
  50. task->options.is_paused= false;
  51. return GEARMAN_SUCCESS;
  52. }
  53. if (gearman_task_data_size(shell))
  54. {
  55. if (gearman_task_result(shell))
  56. {
  57. gearman_task_result(shell)->clear();
  58. gearman_task_result(shell)->reserve(gearman_task_data_size(shell));
  59. }
  60. else
  61. {
  62. if (task->create_result(gearman_task_data_size(shell)) == false)
  63. {
  64. return gearman_error(task->client->universal, GEARMAN_MEMORY_ALLOCATION_FAILURE, "Failed to create result_st");
  65. }
  66. }
  67. assert_msg(task->result(), "programmer error, result_ptr has not been allocated for task");
  68. gearman_string_append(gearman_task_mutable_result(shell)->mutable_string(), static_cast<const char*>(gearman_task_data(shell)), gearman_task_data_size(shell));
  69. }
  70. if (task->recv->command == GEARMAN_COMMAND_WORK_DATA)
  71. { }
  72. else if (task->recv->command == GEARMAN_COMMAND_WORK_WARNING)
  73. { }
  74. else // GEARMAN_COMMAND_WORK_COMPLETE or GEARMAN_COMMAND_WORK_EXCEPTION
  75. {
  76. return GEARMAN_SUCCESS;
  77. }
  78. task->options.is_paused= true;
  79. return GEARMAN_PAUSE;
  80. }
  81. static gearman_return_t _client_pause_complete(gearman_task_st *task)
  82. {
  83. return _client_pause_data(task);
  84. }
  85. static gearman_return_t _client_pause_status(gearman_task_st* shell)
  86. {
  87. Task* task= shell->impl();
  88. assert_msg(task->recv->command == GEARMAN_COMMAND_WORK_STATUS or
  89. task->recv->command == GEARMAN_COMMAND_STATUS_RES_UNIQUE or
  90. task->recv->command == GEARMAN_COMMAND_STATUS_RES, "status has been called out of order for task, or was registered to run on non-status callback, see gearman_actions_t(3)");
  91. if (task->options.is_paused)
  92. {
  93. task->options.is_paused= false;
  94. return GEARMAN_SUCCESS;
  95. }
  96. task->options.is_paused= true;
  97. return GEARMAN_PAUSE;
  98. }
  99. static gearman_return_t _client_pause_fail(gearman_task_st* shell)
  100. {
  101. Task* task= shell->impl();
  102. assert_msg(task->recv->command == GEARMAN_COMMAND_WORK_FAIL,
  103. "fail callback has been called out of order for task, or was registered to run on non-fail callback, see gearman_actions_t(3)");
  104. if (task->options.is_paused)
  105. {
  106. task->options.is_paused= false;
  107. return GEARMAN_SUCCESS;
  108. }
  109. task->options.is_paused= true;
  110. return GEARMAN_PAUSE;
  111. }
  112. static gearman_return_t _client_do_data(gearman_task_st* shell)
  113. {
  114. Task* task= shell->impl();
  115. if (gearman_task_data_size(shell))
  116. {
  117. if (gearman_task_result(shell) == NULL)
  118. {
  119. if (task->create_result(gearman_task_data_size(shell)) == false)
  120. {
  121. return gearman_error(task->client->universal, GEARMAN_MEMORY_ALLOCATION_FAILURE, "Failed to create result_st");
  122. }
  123. }
  124. assert(gearman_task_mutable_result(shell));
  125. gearman_task_mutable_result(shell)->mutable_string()->append(static_cast<const char*>(gearman_task_data(shell)), gearman_task_data_size(shell));
  126. }
  127. return GEARMAN_SUCCESS;
  128. }
  129. static gearman_return_t _client_do_complete(gearman_task_st *shell)
  130. {
  131. Task* task= shell->impl();
  132. if (gearman_task_data_size(shell))
  133. {
  134. if (gearman_task_result(shell) == NULL)
  135. {
  136. if (task->create_result(gearman_task_data_size(shell)) == false)
  137. {
  138. return gearman_error(task->client->universal, GEARMAN_MEMORY_ALLOCATION_FAILURE, "Failed to create result_st");
  139. }
  140. }
  141. gearman_string_append(gearman_task_mutable_result(shell)->mutable_string(), static_cast<const char*>(gearman_task_data(shell)), gearman_task_data_size(shell));
  142. }
  143. task->error_code(GEARMAN_SUCCESS);
  144. return GEARMAN_SUCCESS;
  145. }
  146. const gearman_actions_t &gearman_actions_default(void)
  147. {
  148. static gearman_actions_t default_actions= { 0, 0, 0, 0, 0, 0, 0, 0 };
  149. return default_actions;
  150. }
  151. const gearman_actions_t &gearman_actions_do_default(void)
  152. {
  153. static gearman_actions_t default_actions= { 0, 0, _client_do_data, 0, 0, _client_do_complete, 0, 0 };
  154. return default_actions;
  155. }
  156. const gearman_actions_t &gearman_actions_execute_defaults(void)
  157. {
  158. static gearman_actions_t default_actions= { 0, 0, _client_do_data, 0, 0, _client_do_complete, 0, 0 };
  159. return default_actions;
  160. }
  161. const gearman_actions_t &gearman_actions_pause(void)
  162. {
  163. static gearman_actions_t default_actions= { 0, 0,
  164. _client_pause_data, // gearman_data_fn
  165. _client_pause_data, // gearman_warning_fn
  166. _client_pause_status, // gearman_universal_status_fn
  167. _client_pause_complete, // gearman_complete_fn
  168. _client_pause_data, // gearman_exception_fn
  169. _client_pause_fail }; // gearman_fail_fn
  170. return default_actions;
  171. }