actions.cc 5.8 KB

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