|
8 | 8 | #ifndef GRAPHLAB_LAMBDA_PYLAMBDA_WORKER_H_ |
9 | 9 | #define GRAPHLAB_LAMBDA_PYLAMBDA_WORKER_H_ |
10 | 10 |
|
11 | | -#include <cppipc/server/comm_server.hpp> |
12 | | -#include <lambda/pylambda.hpp> |
13 | | -#include <shmipc/shmipc.hpp> |
14 | | -#include <lambda/graph_pylambda.hpp> |
15 | | -#include <logger/logger.hpp> |
16 | | -#include <process/process_util.hpp> |
17 | | -#include <util/try_finally.hpp> |
| 11 | +#include <string> |
18 | 12 |
|
19 | 13 | namespace graphlab { namespace lambda { |
20 | 14 |
|
21 | 15 | /** The main function to be called from the python ctypes library to |
22 | 16 | * create a pylambda worker process. |
23 | | - * |
24 | | - * Different error routes produce different error codes of 101 and |
25 | | - * above. |
26 | 17 | */ |
27 | | -static int pylambda_worker_main(const char* _root_path, const char* _server_address, int loglevel) { |
28 | | - |
29 | | - /** Set up the debug configuration. |
30 | | - * |
31 | | - * By default, all LOG_ERROR and LOG_FATAL messages are sent to |
32 | | - * stderr, and all messages above loglevel are sent to stdout. |
33 | | - * |
34 | | - * If GRAPHLAB_LAMBDA_WORKER_LOG_FILE is set and is non-empty, then |
35 | | - * all log messages are sent to the file instead of the stdout and |
36 | | - * stderr. In this case, the only errors logged to stderr/stdout |
37 | | - * concern opening the log file. |
38 | | - * |
39 | | - * If GRAPHLAB_LAMBDA_WORKER_DEBUG_MODE is set, then the default |
40 | | - * log level is set to LOG_DEBUG. If a log file is set, then all |
41 | | - * log messages are sent there, otherwise they are sent to stderr. |
42 | | - */ |
43 | | - boost::optional<std::string> debug_mode_str = graphlab::getenv_str("GRAPHLAB_LAMBDA_WORKER_DEBUG_MODE"); |
44 | | - boost::optional<std::string> debug_mode_file_str = graphlab::getenv_str("GRAPHLAB_LAMBDA_WORKER_LOG_FILE"); |
45 | | - |
46 | | - std::string log_file_string = debug_mode_file_str ? *debug_mode_file_str : ""; |
47 | | - bool log_to_file = (!log_file_string.empty()); |
48 | | - |
49 | | - bool debug_mode = (bool)(debug_mode_str); |
50 | | - |
51 | | - global_logger().set_log_level(loglevel); |
52 | | - global_logger().set_log_to_console(true); |
53 | | - |
54 | | - // Logging using the LOG_DEBUG_WITH_PID macro requires this_pid to be set. |
55 | | - size_t this_pid = get_my_pid(); |
56 | | - global_logger().set_pid(this_pid); |
57 | | - |
58 | | - // Set up the logging to file if needed. |
59 | | - if(log_to_file) { |
60 | | - // Set up the logging to the file, with any errors being fully logged. |
61 | | - global_logger().set_log_to_console(true, true); |
62 | | - global_logger().set_log_file(log_file_string); |
63 | | - LOG_DEBUG_WITH_PID("Logging lambda worker logs to " << log_file_string); |
64 | | - global_logger().set_log_to_console(false); |
65 | | - } |
66 | | - |
67 | | - // Now, set the log mode for debug |
68 | | - if(debug_mode) { |
69 | | - global_logger().set_log_level(LOG_DEBUG); |
70 | | - if(!log_to_file) { |
71 | | - // Set logging to console, with everything logged to stderr. |
72 | | - global_logger().set_log_to_console(true, true); |
73 | | - } |
74 | | - } |
75 | | - |
76 | | - // Log the basic information about parameters. |
77 | | - std::string server_address = _server_address; |
78 | | - std::string root_path = _root_path; |
79 | | - size_t parent_pid = get_parent_pid(); |
80 | | - |
81 | | - LOG_DEBUG_WITH_PID("root_path = '" << root_path << "'"); |
82 | | - LOG_DEBUG_WITH_PID("server_address = '" << server_address << "'"); |
83 | | - LOG_DEBUG_WITH_PID("parend pid = " << parent_pid); |
84 | | - |
85 | | - try { |
86 | | - |
87 | | - LOG_DEBUG_WITH_PID("Library function entered successfully."); |
88 | | - |
89 | | - if(server_address == "debug") { |
90 | | - logstream(LOG_INFO) << "Exiting dry run." << std::endl; |
91 | | - return 1; |
92 | | - } |
93 | | - |
94 | | - graphlab::shmipc::server shm_comm_server; |
95 | | - bool has_shm = shm_comm_server.bind(); |
96 | | - |
97 | | - LOG_DEBUG_WITH_PID("shm_comm_server bind: has_shm=" << has_shm); |
98 | | - |
99 | | - // construct the server |
100 | | - cppipc::comm_server server(std::vector<std::string>(), "", server_address); |
101 | | - |
102 | | - server.register_type<graphlab::lambda::lambda_evaluator_interface>([&](){ |
103 | | - if (has_shm) { |
104 | | - auto n = new graphlab::lambda::pylambda_evaluator(&shm_comm_server); |
105 | | - LOG_DEBUG_WITH_PID("creation of pylambda_evaluator with SHM complete."); |
106 | | - return n; |
107 | | - } else { |
108 | | - auto n = new graphlab::lambda::pylambda_evaluator(); |
109 | | - LOG_DEBUG_WITH_PID("creation of pylambda_evaluator without SHM complete."); |
110 | | - return n; |
111 | | - } |
112 | | - }); |
113 | | - server.register_type<graphlab::lambda::graph_lambda_evaluator_interface>([&](){ |
114 | | - auto n = new graphlab::lambda::graph_pylambda_evaluator(); |
115 | | - LOG_DEBUG_WITH_PID("creation of graph_pylambda_evaluator complete."); |
116 | | - return n; |
117 | | - }); |
118 | | - |
119 | | - LOG_DEBUG_WITH_PID("Starting server."); |
120 | | - server.start(); |
121 | | - |
122 | | - wait_for_parent_exit(parent_pid); |
123 | | - |
124 | | - return 0; |
125 | | - |
126 | | - /** Any exceptions happening? |
127 | | - */ |
128 | | - } catch (const std::string& error) { |
129 | | - logstream(LOG_ERROR) << "Internal PyLambda Error: " << error << std::endl; |
130 | | - return 103; |
131 | | - } catch (const std::exception& error) { |
132 | | - logstream(LOG_ERROR) << "PyLambda C++ Error: " << error.what() << std::endl; |
133 | | - return 104; |
134 | | - } catch (...) { |
135 | | - logstream(LOG_ERROR) << "Unknown PyLambda Error." << std::endl; |
136 | | - return 105; |
137 | | - } |
138 | | -} |
| 18 | +int pylambda_worker_main(const std::string& root_path, |
| 19 | + const std::string& server_address, int loglevel); |
139 | 20 |
|
140 | 21 | }} |
141 | 22 |
|
|
0 commit comments