Exemple #1
0
pool_ptr pool(context_t& context, const std::string& name) {
    auto pool = context.config().component_group("postgres").get(name);
    if(!pool) {
        throw error_t(std::errc::argument_out_of_domain, "postgres component with name '{}' not found", name);
    }
    return context.repository().get<pool_t>("postgres", context, name, pool->args());
}
Exemple #2
0
/**
 * Unicorn trait for service creation.
 * Trait to create unicorn service by name. All instances are cached by name as it is done in storage.
 */
unicorn_ptr
unicorn(context_t& context, const std::string& name) {
    auto unicorn = context.config().unicorns().get(name);
    if(!unicorn) {
        throw error_t(error::component_not_found, "unicorn component \"{}\" not found in the config", name);
    }
    return context.repository().get<unicorn_t>(unicorn->type(), context, name, unicorn->args());
}
Exemple #3
0
manifest_t::manifest_t(context_t& context, const std::string& name_):
    cached<dynamic_t>(context, "manifests", name_),
    name(name_)
{
    endpoint = cocaine::format("{}/{}.{}", context.config().path().runtime(), name, ::getpid());

    try {
        environment = as_object().at("environment", dynamic_t::empty_object)
            .to<std::map<std::string, std::string>>();
    } catch (const boost::bad_get&) {
        throw cocaine::error_t("environment should be a map of string -> string");
    }

    if(as_object().find("slave") != as_object().end()) {
        executable = as_object().at("slave").as_string();
    } else {
        throw cocaine::error_t("runnable object has not been specified");
    }
}
Exemple #4
0
uniresis_t::uniresis_t(context_t& context, asio::io_service& loop, const std::string& name, const dynamic_t& args) :
    api::service_t(context, loop, name, args),
    dispatch<io::uniresis_tag>(name),
    uuid(context.uuid()),
    resources(),
    updater(nullptr),
    log(context.log("uniresis"))
{
    if (resources.cpu == 0) {
        throw std::system_error(uniresis::uniresis_errc::failed_calculate_cpu_count);
    }

    if (resources.mem == 0) {
        throw std::system_error(uniresis::uniresis_errc::failed_calculate_system_memory);
    }

    auto restrictions = args.as_object().at("restrictions", dynamic_t::empty_object).as_object();

    auto cpu_restricted = std::min(
        resources.cpu,
        static_cast<uint>(restrictions.at("cpu", resources.cpu).as_uint())
    );

    if (resources.cpu != cpu_restricted) {
        resources.cpu = cpu_restricted;
        COCAINE_LOG_INFO(log, "restricted available CPU count to {}", resources.cpu);
    }

    auto mem_restricted = std::min(
        resources.mem,
        static_cast<std::uint64_t>(restrictions.at("mem", resources.mem).as_uint())
    );

    if (resources.mem != mem_restricted) {
        resources.mem = mem_restricted;
        COCAINE_LOG_INFO(log, "restricted available system memory to {}", resources.mem);
    }

    auto prefix = args.as_object().at("prefix", defaults::resources_path).as_string();
    auto path = format("{}/{}", prefix, uuid);

    auto hostname = context.config().network().hostname();
    auto endpoints = resolve(hostname);
    dynamic_t::object_t extra;
    if (auto locator = context.config().services().get("locator")) {
        extra = dynamic_converter<dynamic_t::object_t>::convert(
            locator->args().as_object().at("extra_param", dynamic_t::empty_object)
        );
    }
    auto unicorn = api::unicorn(context, args.as_object().at("unicorn", defaults::unicorn_name).as_string());
    updater = std::make_shared<updater_t>(
        std::move(path),
        std::move(hostname),
        std::move(endpoints),
        std::move(extra),
        resources,
        std::move(unicorn),
        log
    );
    updater->notify();

    on<io::uniresis::cpu_count>([&] {
        return resources.cpu;
    });

    on<io::uniresis::memory_count>([&] {
        return resources.mem;
    });

    on<io::uniresis::uuid>([&] {
        return uuid;
    });
}