From a84a0a55bce6cec628b10835e831d8b8f5f885f9 Mon Sep 17 00:00:00 2001 From: Kishore Nallan Date: Thu, 21 Dec 2017 07:35:32 +0530 Subject: [PATCH] Improve info and error messages shown in boot sequence. Also, if the data directory does not exist, say so without crashing. --- TODO.md | 1 + include/typesense_version.h | 1 + src/http_server.cpp | 9 ++++++--- src/main/typesense_server.cpp | 23 ++++++++++++++++++++--- 4 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 include/typesense_version.h diff --git a/TODO.md b/TODO.md index c86207f1..74aecf53 100644 --- a/TODO.md +++ b/TODO.md @@ -78,6 +78,7 @@ - ~~Export collection~~ - ~~get collection should show schema~~ - ~~API key should be allowed as a GET parameter also (for JSONP)~~ +- ~~Don't crash when the data directory is not found~~ - handle hyphens (replace them) - clean special chars before indexing - NOT operator support diff --git a/include/typesense_version.h b/include/typesense_version.h new file mode 100644 index 00000000..6a35bff3 --- /dev/null +++ b/include/typesense_version.h @@ -0,0 +1 @@ +#define TYPESENSE_VERSION "0.6.1" \ No newline at end of file diff --git a/src/http_server.cpp b/src/http_server.cpp index 277511b6..6bd9bd46 100644 --- a/src/http_server.cpp +++ b/src/http_server.cpp @@ -99,6 +99,8 @@ int HttpServer::create_listener(void) { setup_ssl(ssl_cert_path.c_str(), ssl_cert_key_path.c_str()); } + ctx.globalconf->server_name = h2o_strdup(NULL, "", SIZE_MAX); + accept_ctx->ctx = &ctx; accept_ctx->hosts = config.hosts; @@ -114,7 +116,6 @@ int HttpServer::create_listener(void) { return -1; } - ctx.globalconf->server_name = h2o_strdup(NULL, "", SIZE_MAX); listener_socket = h2o_evloop_socket_create(ctx.loop, fd, H2O_SOCKET_FLAG_DONT_READ); listener_socket->data = this; h2o_socket_read_start(listener_socket, on_accept); @@ -131,9 +132,11 @@ int HttpServer::run() { h2o_multithread_register_receiver(message_queue, message_receiver, on_message); if (create_listener() != 0) { - std::cerr << "Failed to listen on " << listen_address << ":" << listen_port << std::endl - << "Error: " << strerror(errno) << std::endl; + std::cerr << "Failed to listen on " << listen_address << ":" << listen_port << " - " + << strerror(errno) << std::endl; return 1; + } else { + std::cout << "Server has started. Ready to accept requests on port " << listen_port << std::endl; } on(STOP_SERVER_MESSAGE, HttpServer::on_stop_server); diff --git a/src/main/typesense_server.cpp b/src/main/typesense_server.cpp index eda0e0b0..6f678339 100644 --- a/src/main/typesense_server.cpp +++ b/src/main/typesense_server.cpp @@ -3,11 +3,14 @@ #include #include #include +#include +#include #include #include "http_server.h" #include "api.h" #include "string_utils.h" #include "replicator.h" +#include "typesense_version.h" HttpServer* server; @@ -17,6 +20,11 @@ void catch_interrupt(int sig) { server->stop(); } +bool directory_exists(const std::string & dir_path) { + struct stat info; + return stat(dir_path.c_str(), &info) == 0 && (info.st_mode & S_IFDIR); +} + int main(int argc, char **argv) { cmdline::parser options; options.add("data-dir", 'd', "Directory where data will be stored.", true); @@ -35,16 +43,25 @@ int main(int argc, char **argv) { signal(SIGINT, catch_interrupt); + std::cout << "Typesense version " << TYPESENSE_VERSION << std::endl; + + if(!directory_exists(options.get("data-dir"))) { + std::cerr << "Typesense failed to start. " << "Data directory " << options.get("data-dir") + << " does not exist." << std::endl; + return 1; + } + Store store(options.get("data-dir")); CollectionManager & collectionManager = CollectionManager::get_instance(); Option init_op = collectionManager.init(&store, options.get("api-key"), options.get("search-only-api-key")); if(init_op.ok()) { - std::cout << "Finished restoring all collections from disk." << std::endl; + std::cout << "Finished loading collections from disk." << std::endl; } else { - std::cerr << "Failed initializing collections from store..." << std::endl; - std::cerr << init_op.error() << std::endl; + std::cerr << "Typesense failed to start. " << "Could not load collections from disk: " + << init_op.error() << std::endl; + return 1; } server = new HttpServer(