App1(cocaine::framework::dispatch_t &d) {
        m_log = d.service_manager()->get_system_logger();
        m_storage = d.service_manager()->get_service<cocaine::framework::storage_service_t>("storage");

        d.on<on_event1>("event1", this);
        d.on("event2", this, &App1::on_event2);

        COCAINE_LOG_WARNING(m_log, "test log");
    }
void
subscribe_action_t::watch_event(int /* type */, int /* state */, zookeeper::path_t) {
    try {
        ctx.zk.get(path, *this, *this);
    } catch(const std::system_error& e)  {
        result->abort(e.code());
        COCAINE_LOG_WARNING(ctx.log, "failure during subscription(watch): {}", e.what());
    }
}
void
subscribe_action_t::stat_event(int rc, zookeeper::node_stat const&) {
    // Someone created a node in a gap between
    // we received nonode and issued exists
    if(rc == ZOK) {
        try {
            ctx.zk.get(path, *this, *this);
        } catch(const std::system_error& e)  {
            COCAINE_LOG_WARNING(ctx.log, "failure during subscription(stat): {}", e.what());
            result->abort(e.code());
        }
    }
}
void
subscribe_action_t::data_event(int rc, std::string value, const zookeeper::node_stat& stat) {
    if(rc == ZNONODE) {
        if(last_version != min_version && last_version != not_existing_version) {
            auto code = cocaine::error::make_error_code(static_cast<cocaine::error::zookeeper_errors>(rc));
            result->abort(code);
        } else {
            // Write that node is not exist to client only first time.
            // After that set a watch to see when it will appear
            if(last_version == min_version) {
                std::lock_guard<std::mutex> guard(write_lock);
                if (not_existing_version > last_version) {
                    result->write(versioned_value_t(value_t(), not_existing_version));
                }
            }
            try {
                ctx.zk.exists(path, *this, *this);
            } catch(const std::system_error& e) {
                COCAINE_LOG_WARNING(ctx.log, "failure during subscription(get): {}", e.what());
                result->abort(e.code());
            }
        }
    } else if (rc != 0) {
        auto code = cocaine::error::make_error_code(static_cast<cocaine::error::zookeeper_errors>(rc));
        result->abort(code);
    } else if (stat.numChildren != 0) {
        result->abort(cocaine::error::child_not_allowed);
    } else {
        version_t new_version(stat.version);
        std::lock_guard<std::mutex> guard(write_lock);
        if (new_version > last_version) {
            last_version = new_version;
            value_t val;
            try {
                result->write(versioned_value_t(unserialize(value), new_version));
            } catch(const std::system_error& e) {
                result->abort(e.code());
            }
        }
    }
}
void mastermind_t::data::deserialize() {
	std::string file;
	{
		std::ifstream input(m_cache_path.c_str());
		if (input.is_open() == false) {
			return;
		}
		typedef std::istreambuf_iterator<char> it_t;
		file.assign(it_t(input), it_t());
	}

	try {
		msgpack::unpacked msg;
		msgpack::unpack(&msg, file.data(), file.size());
		msgpack::object object = msg.get();

		kora::dynamic_t raw_cache;

		cocaine::io::type_traits<kora::dynamic_t>::unpack(object, raw_cache);

		auto &raw_cache_object = raw_cache.as_object();

#define TRY_UNPACK_CACHE(cache) \
		do { \
			try { \
				cache.set(cache##_t::cache_type(raw_cache_object[#cache].as_object() \
							, std::bind(&data::create_##cache, this \
								, std::placeholders::_1, std::placeholders::_2) \
							, #cache)); \
			} catch (const std::exception &ex) { \
				COCAINE_LOG_ERROR(m_logger, "libmastermind: cannot deserialize cache %s: %s" \
						, #cache, ex.what()); \
			} \
		} while (false)

#if 0
		TRY_UNPACK_CACHE(cache_groups);
#endif
		TRY_UNPACK_CACHE(elliptics_remotes);

#undef TRY_UNPACK_CACHE

		{
			const auto &raw_namespaces_states = raw_cache_object["namespaces_states"];
			const auto &raw_namespaces_states_object = raw_namespaces_states.as_object();

			for (auto it = raw_namespaces_states_object.begin()
					, end = raw_namespaces_states_object.end();
					it != end; ++it) {
				const auto &name = it->first;

				try {
					namespaces_states.set(name, namespaces_states_t::cache_type(
								it->second.as_object()
								, std::bind(&data::create_namespaces_states, this
									, std::placeholders::_1, std::placeholders::_2)
								, name));
				} catch (const std::exception &ex) {
					COCAINE_LOG_ERROR(m_logger
							, "libmastermind: cannot update namespace_state for %s: %s"
							, name.c_str(), ex.what());
				}
			}
		}

		cache_expire();
		generate_fake_caches();
	} catch (const std::exception &ex) {
		COCAINE_LOG_WARNING(m_logger
				, "libmastermind: cannot deserialize libmastermind cache: %s"
				, ex.what());
	} catch (...) {
		COCAINE_LOG_WARNING(m_logger
				, "libmastermind: cannot deserialize libmastermind cache");
	}
}