2017-10-23 3 views
0

私はルビーSinatra APIを起動する小さなドッカーイメージを実行しています。 APIのThポイントは、画像を受信し、スクリプトを使用してそれを前処理し、Tesseract OCRを使用して画像からテキストを戻すことです。H12ヒロクでリクエストタイムアウト503、その後200成功

私は、503エラーが発生し、テキストで300成功していますが、iosアプリケーションが既に503エラーを受信して​​途中で続行されているため、役に立たないことが問題です。

require 'sinatra' 
require "json" 
require 'sinatra/base' 
require 'sinatra' 
require 'json' 
require 'fileutils' 
require 'tempfile' 
require "base64" 
require 'puma_worker_killer' 

PumaWorkerKiller.enable_rolling_restart 

set :protection, except: [ :json_csrf ] 
port = ENV['PORT'] || 8080 
set :port, port 
set :bind, '0.0.0.0' 

post '/extractText' do 
    begin 
    bas64Image = Base64.decode64(params[:image]) 
    imageFile = Tempfile.new(['image', '.png']) 
    imageFile.write(bas64Image) 
    imageFile.close 
    `textcleaner #{imageFile.path} #{imageFile.path}` 
    # `textdeskew #{imageFile.path} #{imageFile.path}` 
    output = `tesseract #{imageFile.path} --psm 6 --oem 2 stdout` 
    p output 
    rescue 
    status 402 
    return "Error reading image" 
    end 
    status 200 
    return output 
end 

HERESにHerokuのは、私が手を読み出す:

2017-10-23T20:22:00.548029+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/extractText" host=tesseractimageserver.herokuapp.com request_id=7b179829-ef1f-4e18-844b-42b90a5c5c69 fwd="82.32.79.208" dyno=web.1 connect=1ms service=30945ms status=503 bytes=0 protocol=https 
2017-10-23T20:22:42.864098+00:00 app[web.1]: "Returned text from reading image using tesseract" 
2017-10-23T20:22:42.872633+00:00 app[web.1]: 82.32.79.208 - - [23/Oct/2017:20:22:42 +0000] "POST /extractText HTTP/1.1" 200 467 72.2921 

が同上これを回避する方法?

答えて

2

これはどのように動作するのですか? Herokuは、Webリクエストに対して30秒のタイムアウトを設定します。新しい要求がアプリケーションdynoにルーティングされ、この30秒の時間制限内に応答がない場合、Herokuは接続を削除し、H12エラーを報告しています。しかし、あなたのdynoは依然としてWebリクエストを処理しています。クライアントはすでに消えています。このような動作のため、長時間実行されるアクションには何らかの種類のタイムアウトメカニズムを組み込むことをお勧めします。このようなエンドポイントに多くのリクエストを送信すると、アプリケーション全体を簡単に停止させることができます。

この問題の回避策は、バックグラウンドジョブで画像を処理することです。したがって、最初の要求は新しいジョブを登録し、そのIDまたは何らかの種類の識別子を返します。その後、iosアプリはこのIDを使ってサーバーに定期的にpingを送信して、ジョブが終了したかどうかを確認できます。

関連する問題