aboutsummaryrefslogtreecommitdiff
path: root/mod/database-module.hxx
blob: 298afbf949059b31e209d34ecf49de8efe3dd69d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// file      : mod/database-module.hxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifndef MOD_DATABASE_MODULE_HXX
#define MOD_DATABASE_MODULE_HXX

#include <odb/forward.hxx> // odb::core::database, odb::core::connection_ptr

#include <libbrep/types.hxx>
#include <libbrep/utility.hxx>

#include <mod/module.hxx>
#include <mod/module-options.hxx>

namespace brep
{
  struct tenant_service;

  // A handler that utilises the database. Specifically, it will retry the
  // request in the face of recoverable database failures (deadlock, loss of
  // connection, etc) up to a certain number of times.
  //
  class database_module: public handler
  {
  protected:
    database_module () = default;

    // Create a shallow copy (handling instance) if initialized and a deep
    // copy (context exemplar) otherwise.
    //
    explicit
    database_module (const database_module&);

    // Required to avoid getting warning from clang that
    // database_module::init() hides handler::init() virtual functions. This
    // way all functions get to the same scope and become overloaded set.
    //
    using handler::init;

    // Initialize the package database instance. Throw odb::exception on
    // failure.
    //
    void
    init (const options::package_db&, size_t retry);

    // Initialize the build database instance. Throw odb::exception on
    // database failure.
    //
    void
    init (const options::build_db&, size_t retry);

    virtual bool
    handle (request&, response&) = 0;

    // Helpers.
    //

    // Update the tenant-associated service state if the specified
    // notification callback-returned function (expected to be not NULL)
    // returns the new state data. Return the service state data, if updated,
    // and nullopt otherwise.
    //
    // Specifically, start the database transaction, query the service state,
    // and call the callback-returned function on this state. If this call
    // returns the data string (rather than nullopt), then update the service
    // state with this data and persist the change. Repeat all the above steps
    // on the recoverable database failures (deadlocks, etc).
    //
    optional<string>
    update_tenant_service_state (
      const odb::core::connection_ptr&,
      const string& tid,
      const function<optional<string> (const tenant_service&)>&);

  protected:
    size_t retry_ = 0; // Max of all retries.

    shared_ptr<odb::core::database> package_db_;
    shared_ptr<odb::core::database> build_db_;   // NULL if not building.

  private:
    virtual bool
    handle (request&, response&, log&);
  };
}

#endif // MOD_DATABASE_MODULE_HXX