queue.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
  6. * Copyright (C) 2009 Cory Bennett
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are
  11. * met:
  12. *
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * * Redistributions in binary form must reproduce the above
  17. * copyright notice, this list of conditions and the following disclaimer
  18. * in the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * * The names of its contributors may not be used to endorse or
  22. * promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. /**
  39. * @file
  40. * @brief libsqlite3 Queue Storage Definitions
  41. */
  42. #include <gear_config.h>
  43. #include <libgearman-server/common.h>
  44. #include <libgearman-server/plugins/queue/sqlite/queue.h>
  45. #include <libgearman-server/plugins/queue/base.h>
  46. #include "libgearman-server/plugins/queue/sqlite/instance.hpp"
  47. /** Default values.
  48. */
  49. #define GEARMAND_QUEUE_SQLITE_DEFAULT_TABLE "gearman_queue"
  50. namespace gearmand {
  51. namespace plugins {
  52. namespace queue {
  53. class Sqlite : public gearmand::plugins::Queue
  54. {
  55. public:
  56. Sqlite();
  57. ~Sqlite();
  58. gearmand_error_t initialize();
  59. std::string schema;
  60. std::string table;
  61. private:
  62. bool _store_on_shutdown;
  63. };
  64. Sqlite::Sqlite() :
  65. Queue("libsqlite3")
  66. {
  67. command_line_options().add_options()
  68. ("libsqlite3-db", boost::program_options::value(&schema), "Database file to use.")
  69. ("store-queue-on-shutdown", boost::program_options::bool_switch(&_store_on_shutdown)->default_value(false), "Store queue on shutdown only.")
  70. ("libsqlite3-table", boost::program_options::value(&table)->default_value(GEARMAND_QUEUE_SQLITE_DEFAULT_TABLE), "Table to use.")
  71. ;
  72. }
  73. Sqlite::~Sqlite()
  74. {
  75. }
  76. gearmand_error_t Sqlite::initialize()
  77. {
  78. gearmand::queue::Instance* exec_queue;
  79. try
  80. {
  81. exec_queue= new gearmand::queue::Instance { schema, table };
  82. }
  83. catch (std::bad_alloc &)
  84. {
  85. return GEARMAND_MEMORY_ALLOCATION_FAILURE;
  86. }
  87. exec_queue->store_on_shutdown(_store_on_shutdown);
  88. gearmand_error_t rc{};
  89. if ((rc= exec_queue->init()) != GEARMAND_SUCCESS)
  90. {
  91. delete exec_queue;
  92. return rc;
  93. }
  94. gearman_server_set_queue(Gearmand()->server, exec_queue);
  95. return rc;
  96. }
  97. void initialize_sqlite()
  98. {
  99. static Sqlite local_instance{};
  100. }
  101. } // namespace queue
  102. } // namespace plugins
  103. } // namespace gearmand