簡體   English   中英

Ruby Google Cloud PubSub 異步拉取返回 GRPC::DeadlineExceeded 錯誤

[英]Ruby Google Cloud PubSub async pull returns GRPC::DeadlineExceeded error

嗨,我在 Google PubSub 中設置了訂閱,我正在嘗試使用“官方” google-cloud-ruby庫異步拉取消息。 這是我的代碼,它將從傳入subscription_name的 rake 任務中執行:

def pull!
  creds = Google::Cloud::PubSub::Credentials.new(
    GCP_CREDENTIALS_KEYFILE_PATH,
    scope: "https://www.googleapis.com/auth/pubsub"
  )
  messages = []
  pubsub = Google::Cloud::PubSub.new(
    project_id: GOOGLE_PROJECT_ID,
    credentials: creds
  )

  subscription = pubsub.subscription(subscription_name)

  subscription.pull(immediate: true).each do |received_message|
    puts "Received message: #{received_message.data}"
    received_message.acknowledge!
    messages.push(received_message)
  end

  # Return the collected messages
  messages
  
rescue => error
  Rails.logger error

  messages.presence
end

Google::Cloud::PubSub::Credentials部分引用了一個有效的密鑰文件。 我知道 JSON 密鑰文件很好,因為我可以使用它來使用oauth2l生成一個有效的承載令牌,並使用 cURL、郵遞員、Net::HTTP 等從 PubSub 中提取。 對單獨的Google::Cloud::Storage使用相同的 JSON 憑據Google::Cloud::Storage服務,也可以正常工作。

但是由於某種原因使用Google::Cloud::PubSub它只是掛起並且不會響應。 大約 60 秒后,我收到以下錯誤:

GRPC::DeadlineExceeded: 4:Deadline Exceeded. debug_error_string:{"created":"@1602610740.445195000","description":"Deadline Exceeded","file":"src/core/ext/filters/deadline/deadline_filter.cc","file_line":69,"grpc_status":4}
/Users/bbulpet/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/grpc-1.32.0-universal-darwin/src/ruby/lib/grpc/generic/active_call.rb:29:in `check_status'
/Users/bbulpet/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/grpc-1.32.0-universal-darwin/src/ruby/lib/grpc/generic/active_call.rb:180:in `attach_status_results_and_complete_call'
/Users/bbulpet/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/grpc-1.32.0-universal-darwin/src/ruby/lib/grpc/generic/active_call.rb:376:in `request_response'
/Users/bbulpet/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/grpc-1.32.0-universal-darwin/src/ruby/lib/grpc/generic/client_stub.rb:172:in `block (2 levels) in request_response'
/Users/bbulpet/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/grpc-1.32.0-universal-darwin/src/ruby/lib/grpc/generic/interceptors.rb:170:in `intercept!'
/Users/bbulpet/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/grpc-1.32.0-universal-darwin/src/ruby/lib/grpc/generic/client_stub.rb:171:in `block in request_response'
/Users/bbulpet/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/gapic-common-0.3.4/lib/gapic/grpc/service_stub/rpc_call.rb:121:in `call'
/Users/bbulpet/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/gapic-common-0.3.4/lib/gapic/grpc/service_stub.rb:156:in `call_rpc'
/Users/bbulpet/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/google-cloud-pubsub-v1-0.1.2/lib/google/cloud/pubsub/v1/subscriber/client.rb:503:in `get_subscription'
/Users/bbulpet/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/google-cloud-pubsub-2.1.0/lib/google/cloud/pubsub/service.rb:154:in `get_subscription'
/Users/bbulpet/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/google-cloud-pubsub-2.1.0/lib/google/cloud/pubsub/project.rb:286:in `subscription'

添加調試器顯示以下行掛起並導致錯誤:

subscription = pubsub.subscription(subscription_name)

我已經根據文檔嘗試了所有我能想到的方法。 更新了所有相關的 gems,甚至出於絕望嘗試使用過時的語法。 如果有人至少知道從哪里開始,那將不勝感激,謝謝!

更新

所以事實證明,由於某種原因,在本地運行它無法連接,但是當運送到部署環境時,上面的代碼完美地連接到 pubsub 並且能夠拉取和確認消息。 此外,在部署環境中建立初始連接后,我現在也可以使用相同的憑據在本地進行連接。

鏈接到 Github 問題對話以獲取有關故障排除過程的上下文以及用於嘗試部署到另一個環境的 Quartusmo 建議。

你還有這個問題嗎? 我只是嘗試使用 Ruby 2.6.5p114、google-cloud-pubsub 2.1.0 和 grpc 1.32.0(與您的版本相同)來重現它,但我無法重現它。 這是我的代碼(稍微修改以在 Minitest 規范上下文中運行)以進行比較:

  GCP_CREDENTIALS_KEYFILE_PATH = "/Users/quartzmo/my-project.json"
  GOOGLE_PROJECT_ID = "my-project-id"

  def pull! topic_name, subscription_name
    creds = Google::Cloud::PubSub::Credentials.new(
      GCP_CREDENTIALS_KEYFILE_PATH,
      scope: "https://www.googleapis.com/auth/pubsub"
    )
    messages = []
    pubsub = Google::Cloud::PubSub.new(
      project_id: GOOGLE_PROJECT_ID,
      credentials: creds
    )
    topic = pubsub.create_topic topic_name
    topic.subscribe subscription_name

    topic.publish "A test message from #{topic_name} to #{subscription_name}"
  
    subscription = pubsub.subscription(subscription_name)
  
    subscription.pull(immediate: true).each do |received_message|
      puts "Received message: #{received_message.data}"
      received_message.acknowledge!
      messages.push(received_message)
    end
  
    # Return the collected messages
    messages
  end

  focus
  it "pull!" do
    topic_name = random_topic_name
    subscription_name = random_subscription_name
    messages = pull! topic_name, subscription_name
    assert_equal 1, messages.count
    assert_equal "A test message from #{topic_name} to #{subscription_name}", messages[0].data
  end

這是輸出:

% bundle exec rake test
Run options: --junit --junit-filename=sponge_log.xml --seed 30984

# Running:

Received message: A test message from ruby-pubsub-samples-test-topic-7cb10bde to ruby-pubsub-samples-test-subscription-f47f2eaa
.

Finished in 6.529219s, 0.1532 runs/s, 0.3063 assertions/s.

1 runs, 2 assertions, 0 failures, 0 errors, 0 skips

更新(2020-10-20):在不同的環境中執行代碼時解決了這個問題,盡管原因未知。 請參閱對 GitHub 問題的評論

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM