worker.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011-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. /*
  38. @note This header is internal, and should not be used by external programs.
  39. */
  40. #pragma once
  41. #include <stdexcept>
  42. namespace {
  43. inline void all_worker_PRINTER(const char *line, gearman_verbose_t verbose, void*)
  44. {
  45. fprintf(stderr, "%s:%d WORKER %s(%s)\n", __FILE__, __LINE__, gearman_verbose_name(verbose), line);
  46. }
  47. }
  48. namespace org { namespace gearmand { namespace libgearman {
  49. class Worker {
  50. public:
  51. Worker()
  52. {
  53. _worker= gearman_worker_create(NULL);
  54. if (_worker == NULL)
  55. {
  56. throw std::runtime_error("gearman_worker_create() failed");
  57. }
  58. enable_logging();
  59. enable_ssl();
  60. }
  61. Worker(in_port_t arg)
  62. {
  63. _worker= gearman_worker_create(NULL);
  64. if (_worker == NULL)
  65. {
  66. throw std::runtime_error("gearman_worker_create() failed");
  67. }
  68. enable_logging();
  69. enable_ssl();
  70. gearman_worker_add_server(_worker, "localhost", arg);
  71. }
  72. Worker(const Worker& worker_)
  73. {
  74. _worker= gearman_worker_clone(NULL, &worker_);
  75. if (_worker == NULL)
  76. {
  77. throw std::runtime_error("gearman_worker_create() failed");
  78. }
  79. enable_logging();
  80. enable_ssl();
  81. }
  82. void operator=(const Worker& worker_)
  83. {
  84. gearman_worker_free(_worker);
  85. _worker= gearman_worker_clone(NULL, &worker_);
  86. if (_worker == NULL)
  87. {
  88. throw std::runtime_error("gearman_worker_create() failed");
  89. }
  90. enable_logging();
  91. enable_ssl();
  92. }
  93. gearman_worker_st* operator&() const
  94. {
  95. return _worker;
  96. }
  97. gearman_worker_st* operator->() const
  98. {
  99. return _worker;
  100. }
  101. ~Worker()
  102. {
  103. gearman_worker_free(_worker);
  104. }
  105. void enable_logging()
  106. {
  107. if (getenv("YATL_WORKER_LOGGING"))
  108. {
  109. gearman_log_fn *func= all_worker_PRINTER;
  110. gearman_worker_set_log_fn(_worker, func, NULL, GEARMAN_VERBOSE_ERROR);
  111. }
  112. }
  113. void enable_ssl()
  114. {
  115. if (getenv("GEARMAND_CA_CERTIFICATE"))
  116. {
  117. gearman_worker_add_options(_worker, GEARMAN_WORKER_SSL);
  118. }
  119. }
  120. private:
  121. gearman_worker_st *_worker;
  122. };
  123. } /* namespace libgearman */ } /* namespace gearmand */ } /* namespace org */