Seastar
High performance C++ framework for concurrent servers
app-template.hh
1 /*
2  * This file is open source software, licensed to you under the terms
3  * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4  * distributed with this work for additional information regarding copyright
5  * ownership. You may not use this file except in compliance with the License.
6  *
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an
13  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14  * KIND, either express or implied. See the License for the
15  * specific language governing permissions and limitations
16  * under the License.
17  */
18 /*
19  * Copyright (C) 2014 Cloudius Systems, Ltd.
20  */
21 #pragma once
22 
23 #ifndef SEASTAR_MODULE
24 #include <boost/program_options.hpp>
25 #include <functional>
26 #include <chrono>
27 #endif
28 #include <seastar/core/future.hh>
29 #include <seastar/core/smp.hh>
31 #include <seastar/core/sstring.hh>
32 #include <seastar/util/program-options.hh>
34 #include <seastar/core/scollectd.hh>
35 #include <seastar/util/log-cli.hh>
36 #include <seastar/util/modules.hh>
37 
38 namespace seastar {
39 
40 namespace alien {
41 
42 class instance;
43 
44 }
45 
46 SEASTAR_MODULE_EXPORT
47 class app_template {
48 public:
49  struct config {
55  sstring name = "App";
60  sstring description = "";
61  std::chrono::duration<double> default_task_quota = std::chrono::microseconds(500);
82  config() {}
83  };
84 
92  sstring name = "App";
97  sstring description = "";
119 
120  seastar_options();
121  };
122 
123  using configuration_reader = std::function<void (boost::program_options::variables_map&)>;
124 private:
125  // unique_ptr to avoid pulling in alien.hh.
126  std::unique_ptr<alien::instance> _alien;
127  // reactor destruction is asynchronous, so we must let the last reactor
128  // destroy the smp instance
129  std::shared_ptr<smp> _smp;
130  seastar_options _opts;
131  boost::program_options::options_description _app_opts;
132  boost::program_options::options_description _seastar_opts;
133  boost::program_options::options_description _opts_conf_file;
134  boost::program_options::positional_options_description _pos_opts;
135  std::optional<boost::program_options::variables_map> _configuration;
136  configuration_reader _conf_reader;
137 
138  configuration_reader get_default_configuration_reader();
139 public:
141  const char* name;
142  const boost::program_options::value_semantic* value_semantic;
143  const char* help;
144  int max_count;
145  };
146 public:
147  explicit app_template(seastar_options opts);
148  explicit app_template(config cfg = config());
149  ~app_template();
150 
151  const seastar_options& options() const;
152 
153  boost::program_options::options_description& get_options_description();
154  boost::program_options::options_description& get_conf_file_options_description();
155  boost::program_options::options_description_easy_init add_options();
156  void add_positional_options(std::initializer_list<positional_option> options);
157  boost::program_options::variables_map& configuration();
158  int run_deprecated(int ac, char ** av, std::function<void ()>&& func) noexcept;
159 
160  void set_configuration_reader(configuration_reader conf_reader);
161 
164  alien::instance& alien() { return *_alien; }
165 
166  // Runs given function and terminates the application when the future it
167  // returns resolves. The value with which the future resolves will be
168  // returned by this function.
169  int run(int ac, char ** av, std::function<future<int> ()>&& func) noexcept;
170 
171  // Like run() which takes std::function<future<int>()>, but returns
172  // with exit code 0 when the future returned by func resolves
173  // successfully.
174  int run(int ac, char ** av, std::function<future<> ()>&& func) noexcept;
175 };
176 
177 }
Definition: alien.hh:120
Definition: app-template.hh:47
alien::instance & alien()
Definition: app-template.hh:164
Definition: app-template.hh:140
A representation of a possibly not-yet-computed value.
Definition: future.hh:1238
Definition: program-options.hh:290
header file for metric API layer (like prometheus or collectd)
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
Definition: app-template.hh:49
bool auto_handle_sigint_sigterm
Handle SIGINT/SIGTERM by calling reactor::stop()
Definition: app-template.hh:72
sstring name
Definition: app-template.hh:55
size_t reserve_additional_memory_per_shard
Definition: app-template.hh:81
unsigned max_networking_aio_io_control_blocks
Definition: app-template.hh:75
sstring description
Definition: app-template.hh:60
Seastar configuration options.
Definition: app-template.hh:86
sstring description
Definition: app-template.hh:97
metrics::options metrics_opts
Configuration for the metrics sub-system.
Definition: app-template.hh:112
scollectd::options scollectd_opts
Configuration for the scollectd sub-system.
Definition: app-template.hh:116
reactor_options reactor_opts
Configuration options for the reactor.
Definition: app-template.hh:110
bool auto_handle_sigint_sigterm
Handle SIGINT/SIGTERM by calling reactor::stop()
Definition: app-template.hh:108
sstring name
Definition: app-template.hh:92
log_cli::options log_opts
Configuration for the logging sub-system.
Definition: app-template.hh:118
smp_options smp_opts
Configuration options for the smp aspect of seastar.
Definition: app-template.hh:114
Logging configuration.
Definition: log-cli.hh:54
Metrics configuration options.
Definition: metrics_api.hh:424
Configuration for the reactor.
Definition: reactor_config.hh:45
Definition: scollectd.hh:354
Configuration for the multicore aspect of seastar.
Definition: smp_options.hh:47