module GRPC
  module Kit
    module Queue
      module Worker
        class Runner
          def initialize(params)
            @topic_name        = params[:topic]
            @subscription_name = "#{params[:topic]}.#{params[:worker]}"
            @worker_class      = params[:worker]
          end

          def self.run!(params)
            new(params).run!
          end

          def run!
            if worker.nil?
              GRPC.logger.error("class #{@worker_class} does not exist")
              exit
            end

            subscriber = subscription.listen { |msg| worker.new(msg).call }
            subscriber.start
          end

          private

          def worker
            Object.const_get(@worker_class)
          rescue NameError
            nil
          end

          def subscription
            @subscription ||= topic.subscription(@subscription_name) || topic.subscribe(@subscription_name)
          end

          def topic
            @topic ||= pubsub.topic(@topic_name) || pubsub.create_topic(@topic_name)
          end

          def pubsub
            @pubsub ||= Google::Cloud::Pubsub.new
          end
        end
      end
    end
  end
end

module GRPC
  module Kit
    module Queue
      module Worker
        class Runner
          def initialize(params)
            @topic_name        = params[:topic]
            @subscription_name = "#{params[:topic]}.#{params[:worker]}"
            @worker_class      = params[:worker]
          end

          def self.run!(params)
            new(params).run!
          end

          def run!
            if worker.nil?
              GRPC.logger.error("class #{@worker_class} does not exist")
              exit
            end
            subscription.listen { |msg| worker.new(msg).call }
          end

          private

          def worker
            Object.const_get(@worker_class)
          rescue NameError
            nil
          end

          def subscription
            @subscription ||= topic.subscription(@subscription_name) || topic.subscribe(@subscription_name)
          end

          def topic
            @topic ||= pubsub.topic(@topic_name) || pubsub.create_topic(@topic_name)
          end

          def pubsub
            @pubsub ||= Google::Cloud::Pubsub.new
          end
        end
      end
    end
  end
end

Simplify GRPC::Kit::Queue::Worker::RunnerNERD_tree_1rungit push origin improvement/update-dependencies
Simplify GRPC::Kit::Queue::Worker::RunnerNERD_tree_1rungit push origin improvement/update-dependencies
