void CoroutineServiceHandler::ProcessMessage(StageData<ServiceHandler>* data) {
  ProtocolMessage* message = NULL;
  int wait_time = 1;

  while (true) {
    if (!data->pri_queue->Pop(&message)) {
      if (!data->queue->Pop(&message, wait_time)) {
        break;
      }
    }

    wait_time = -1;

    // if handler is unspecified then this is a new request
    CoroActor* actor = NULL;
    if (message->handler_id == Handler::kUnspecifiedId) {
      Service* service = message->GetPayloadAs<Service*>();
      actor = coro::Spawn<CoroService>(service);
      actor->SendMail(message);
    } else {
      CoroActor* actor = coro::GetCoroutine<CoroActor>(message->GetPayloadAs<uint64_t>());
      if (!actor) {
        LOG_ERROR(logger, "CoroutineServiceHandler::ProcessMessage Coroutine is gone:" 
            << message->GetPayloadAs<uint64_t>());
        continue;
      } 
      actor->SendMail(message);
    }

    coro::Schedule(actor);
  }  
}
void CoroutineServiceHandler::ProcessMessage(StageData<ServiceHandler>* data) {
  ProtocolMessage* message = NULL;
  int wait_time = 1;

  while (true) {
    if (!data->queue->Pop(&message, wait_time)) {
      break;
    }

    wait_time = -1;

    if (UNLIKELY(message->type_id == ade::MessageType::kTimerMessage)) {
      TimerMessage* timer_message = static_cast<TimerMessage*>(message);
      coro::SpawnAndSchedule<CoroTask>(std::move(timer_message->proc));
      MessageFactory::Destroy(message);
      continue;
    }

    // if handler is unspecified then this is a new request
    CoroActor* actor = NULL;
    if (message->handler_id == Handler::kUnspecifiedId) {
      Service* service = message->GetPayloadAs<Service*>();
      actor = coro::Spawn<CoroService>(service);
    } else {
      actor = coro::GetCoroutine<CoroActor>(message->GetPayloadAs<uint64_t>());
      if (UNLIKELY(!actor)) {
        LOG_DEBUG(logger, "CoroutineServiceHandler::ProcessMessage Coroutine is gone" 
            << ", coro_id: " << message->GetPayloadAs<uint64_t>()
            << ", message:" << *message);
      } 
    }

    if (LIKELY(actor && actor->SendMail(message))) {
      coro::Schedule(actor);
    } else {
      MessageFactory::Destroy(message);
    }
  }  
}