/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include // @manual namespace thrift { namespace python { namespace { void do_import() { if (0 != import_thrift__python__server()) { throw std::runtime_error("import_thrift__python__server failed"); } } } // namespace enum class LifecycleFunc { ON_START_SERVING = 0, ON_STOP_REQUESTED = 1, }; std::string getLifecycleFuncName(LifecycleFunc func) { switch (func) { case LifecycleFunc::ON_START_SERVING: { return "onStartServing"; } case LifecycleFunc::ON_STOP_REQUESTED: { return "onStopRequested"; } } } folly::SemiFuture PythonAsyncProcessorFactory::semifuture_onStartServing() { return callLifecycle(LifecycleFunc::ON_START_SERVING); } folly::SemiFuture PythonAsyncProcessorFactory::semifuture_onStopRequested() { return callLifecycle(LifecycleFunc::ON_STOP_REQUESTED); } folly::SemiFuture PythonAsyncProcessorFactory::callLifecycle( LifecycleFunc funcType) { std::size_t ind = (std::size_t)funcType; if (ind < lifecycleFuncs_.size()) { if (auto func = lifecycleFuncs_[ind]) { auto [promise, future] = folly::makePromiseContract(); [[maybe_unused]] static bool done = (do_import(), false); handleLifecycleCallback( func, getLifecycleFuncName(funcType), std::move(promise)); return std::move(future); } } return folly::makeSemiFuture(); } } // namespace python } // namespace thrift