actions.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 <memory>
  43. struct gearman_result_st;
  44. static gearman_return_t _client_pause_data(gearman_task_st *task)
  45. {
  46. if (task->options.is_paused)
  47. {
  48. task->options.is_paused= false;
  49. return GEARMAN_SUCCESS;
  50. }
  51. if (gearman_task_data_size(task))
  52. {
  53. if (gearman_task_result(task))
  54. {
  55. gearman_task_result(task)->clear();
  56. }
  57. else
  58. {
  59. task->result_ptr= new (std::nothrow) gearman_result_st(gearman_task_data_size(task));
  60. }
  61. assert_msg(task->result_ptr, "programmer error, result_ptr has not been allocated for task");
  62. gearman_string_append(gearman_task_mutable_result(task)->mutable_string(), static_cast<const char*>(gearman_task_data(task)), gearman_task_data_size(task));
  63. }
  64. if (task->recv->command == GEARMAN_COMMAND_WORK_DATA)
  65. { }
  66. else if (task->recv->command == GEARMAN_COMMAND_WORK_WARNING)
  67. { }
  68. else if (task->recv->command == GEARMAN_COMMAND_WORK_EXCEPTION)
  69. { }
  70. else // GEARMAN_COMMAND_WORK_COMPLETE
  71. {
  72. return GEARMAN_SUCCESS;
  73. }
  74. task->options.is_paused= true;
  75. return GEARMAN_PAUSE;
  76. }
  77. static gearman_return_t _client_pause_complete(gearman_task_st *task)
  78. {
  79. return _client_pause_data(task);
  80. }
  81. static gearman_return_t _client_pause_status(gearman_task_st *task)
  82. {
  83. assert_msg(task->recv->command == GEARMAN_COMMAND_WORK_STATUS or
  84. task->recv->command == GEARMAN_COMMAND_STATUS_RES_UNIQUE or
  85. 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)");
  86. if (task->options.is_paused)
  87. {
  88. task->options.is_paused= false;
  89. return GEARMAN_SUCCESS;
  90. }
  91. task->options.is_paused= true;
  92. return GEARMAN_PAUSE;
  93. }
  94. static gearman_return_t _client_pause_fail(gearman_task_st *task)
  95. {
  96. assert_msg(task->recv->command == GEARMAN_COMMAND_WORK_FAIL,
  97. "fail callback has been called out of order for task, or was registered to run on non-fail callback, see gearman_actions_t(3)");
  98. if (task->options.is_paused)
  99. {
  100. task->options.is_paused= false;
  101. return GEARMAN_SUCCESS;
  102. }
  103. task->options.is_paused= true;
  104. return GEARMAN_PAUSE;
  105. }
  106. static gearman_return_t _client_do_data(gearman_task_st *task)
  107. {
  108. if (gearman_task_data_size(task))
  109. {
  110. if (gearman_task_result(task) == NULL)
  111. {
  112. task->result_ptr= new (std::nothrow) gearman_result_st(gearman_task_data_size(task));
  113. if (task->result_ptr == NULL)
  114. {
  115. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  116. }
  117. }
  118. gearman_string_append(gearman_task_mutable_result(task)->mutable_string(), static_cast<const char*>(gearman_task_data(task)), gearman_task_data_size(task));
  119. }
  120. return GEARMAN_SUCCESS;
  121. }
  122. static gearman_return_t _client_do_complete(gearman_task_st *task)
  123. {
  124. if (gearman_task_data_size(task))
  125. {
  126. if (gearman_task_result(task) == NULL)
  127. {
  128. task->result_ptr= new (std::nothrow) gearman_result_st(gearman_task_data_size(task));
  129. if (task->result_ptr == NULL)
  130. {
  131. return GEARMAN_MEMORY_ALLOCATION_FAILURE;
  132. }
  133. }
  134. gearman_string_append(gearman_task_mutable_result(task)->mutable_string(), static_cast<const char*>(gearman_task_data(task)), gearman_task_data_size(task));
  135. }
  136. task->result_rc= GEARMAN_SUCCESS;
  137. return GEARMAN_SUCCESS;
  138. }
  139. const gearman_actions_t &gearman_actions_default(void)
  140. {
  141. static gearman_actions_t default_actions= { 0, 0, 0, 0, 0, 0, 0, 0 };
  142. return default_actions;
  143. }
  144. const gearman_actions_t &gearman_actions_do_default(void)
  145. {
  146. static gearman_actions_t default_actions= { 0, 0, _client_do_data, 0, 0, _client_do_complete, 0, 0 };
  147. return default_actions;
  148. }
  149. const gearman_actions_t &gearman_actions_execute_defaults(void)
  150. {
  151. static gearman_actions_t default_actions= { 0, 0, _client_do_data, 0, 0, _client_do_complete, 0, 0 };
  152. return default_actions;
  153. }
  154. const gearman_actions_t &gearman_actions_pause(void)
  155. {
  156. static gearman_actions_t default_actions= { 0, 0,
  157. _client_pause_data, // gearman_data_fn
  158. _client_pause_data, // gearman_warning_fn
  159. _client_pause_status, // gearman_universal_status_fn
  160. _client_pause_complete, // gearman_complete_fn
  161. _client_pause_data, // gearman_exception_fn
  162. _client_pause_fail }; // gearman_fail_fn
  163. return default_actions;
  164. }