0

現在、無料トライアルモードでGoogleクラウドプロジェクトを進めています。私は、データベンダーからデータを取り出してデータストアに格納するためのcronジョブを持っています。私は数週間前にデータのカップルを取得するコードを書いたが、すべて正常に動作していたが、突然、私はエラー "DeadlineExceededError:HTTPリクエストに応答するための全体的な締め切りを超えた"最後の2日間。私はcronの仕事は60分後に私はなぜエラーを取得している任意のアイデア後にタイムアウトすることになっていると思う?私は希望cron job throwing DeadlineExceededError

クーロン・タスク

def run(): 
    try: 
    config = cron.config 
    actual_data_source = config['xxx']['xxxx'] 
    original_data_source = actual_data_source 

    company_list = cron.rest_client.load(config, "companies", '') 

    if not company_list: 
     logging.info("Company list is empty") 
     return "Ok" 

    for row in company_list: 
     company_repository.save(row,original_data_source, actual_data_source) 

    return "OK" 

リポジトリコード

def save(dto, org_ds , act_dp): 
    try: 
    key = 'FIN/%s' % (dto['ticker']) 
    company = CompanyInfo(id=key) 
    company.stock_code = key 
    company.ticker = dto['ticker'] 
    company.name = dto['name'] 
    company.original_data_source = org_ds 
    company.actual_data_provider = act_dp 
    company.put() 
    return company 
    except Exception: 
    logging.exception("company_repository: error occurred saving the company 
    record ") 
    raise 

RestClient

def load(config, resource, filter): 
    try: 
    username = config['xxxx']['xxxx'] 
    password = config['xxxx']['xxxx'] 
    headers = {"Authorization": "Basic %s" % base64.b64encode(username + ":" 
    + password)} 

    if filter: 
     from_date = filter['from'] 
     to_date = filter['to'] 
     ticker = filter['ticker'] 
     start_date = datetime.strptime(from_date, '%Y%m%d').strftime("%Y-%m-%d") 
     end_date = datetime.strptime(to_date, '%Y%m%d').strftime("%Y-%m-%d") 

    current_page = 1 
    data = [] 

    while True: 

     if (filter): 
     url = config['xxxx']["endpoints"][resource] % (ticker, current_page, start_date, end_date) 
     else: 
     url = config['xxxx']["endpoints"][resource] % (current_page) 

     response = urlfetch.fetch(
      url=url, 
      deadline=60, 
      method=urlfetch.GET, 
      headers=headers, 
      follow_redirects=False, 

     ) 
     if response.status_code != 200: 
      logging.error("xxxx GET received status code %d!" % (response.status_code)) 
      logging.error("error happend for url: %s with headers %s", url, headers) 
      return 'Sorry, xxxx API request failed', 500 

     db = json.loads(response.content) 

     if not db['data']: 
      break 

     data.extend(db['data']) 

     if db['total_pages'] == current_page: 
      break 

     current_page += 1 

    return data 
except Exception: 
    logging.exception("Error occured with xxxx API request") 
    raise 
+0

あなたがブロックまたはレート制限されていないと仮定すると@momusが示唆するように、それぞれのセーブを実行するタスクをディスパッチ考えます'load'関数の' while'ループの繰り返しです。そうすれば、データストアの更新を開始する前に 'load'が完了するまで待つ必要はありません。また、それぞれのインスタンスで 'put()'を呼び出すのではなく、 'ndb.put_multi'を使うことも考えられます。 – snakecharmerb

+0

関連(はい、私はそれが本当に別の質問であることを理解しています):https:// stackoverflow。com/questions/45594018/deadlineexceedederror-the-overall-for-the-http-request-for-the-http-request-w –

+0

これらのcron要求を処理するサービスには、どのようなスケーリングとインスタンスタイプを使用しますか? –

答えて

0

これをコメントとして書きますが、私はそれを行うにはもっと評判が必要です。

  1. の代わりに実際のデータフェッチを直接実行するとどうなりますか?
  2. ジョブの開始から終了までの時間デルタを測定しようとしましたか?
  3. 検索される企業の数が劇的に増加しましたか?
  4. 株式相場のアグリゲーションを行っているようです - プロバイダがあなたをブロックし始めている可能性がありますか? ですか?
1

私はこれを推測しているが、このように同じ質問ですが、今より多くのコードで: DeadlineExceededError: The overall deadline for responding to the HTTP request was exceeded

私は、各UrlFetchの後にデータベースに書き込むようにコードを修正しました。より多くのページがある場合、遅延タスクでそれ自体を再起動します。これは、10分のタイムアウトよりもずっと前です。

遅延タスク内のキャッチされない例外は、再試行するため、注意が必要です。

actual_data_source & original_data_sourceがうまくいかなかったのですが、その部分を修正できるはずです。

crontask

def run(current_page=0): 
    try: 
    config = cron.config 
    actual_data_source = config['xxx']['xxxx'] 
    original_data_source = actual_data_source 

    data, more = cron.rest_client.load(config, "companies", '', current_page) 

    for row in data: 
      company_repository.save(row, original_data_source, actual_data_source) 

    # fetch the rest 
    if more: 
     deferred.defer(run, current_page + 1) 
    except Exception as e: 
    logging.exception("run() experienced an error: %s" % e) 

RestClient

def load(config, resource, filter, current_page): 
    try: 
     username = config['xxxx']['xxxx'] 
     password = config['xxxx']['xxxx'] 
     headers = {"Authorization": "Basic %s" % base64.b64encode(username + ":" 
     + password)} 

     if filter: 
      from_date = filter['from'] 
      to_date = filter['to'] 
      ticker = filter['ticker'] 
      start_date = datetime.strptime(from_date, '%Y%m%d').strftime("%Y-%m-%d") 
      end_date = datetime.strptime(to_date, '%Y%m%d').strftime("%Y-%m-%d") 

      url = config['xxxx']["endpoints"][resource] % (ticker, current_page, start_date, end_date) 
     else: 
      url = config['xxxx']["endpoints"][resource] % (current_page) 

     response = urlfetch.fetch(
       url=url, 
       deadline=60, 
       method=urlfetch.GET, 
       headers=headers, 
       follow_redirects=False, 

     ) 
     if response.status_code != 200: 
       logging.error("xxxx GET received status code %d!" % (response.status_code)) 
       logging.error("error happend for url: %s with headers %s", url, headers) 
       return [], False 

     db = json.loads(response.content) 

     return db['data'], (db['total_pages'] != current_page) 


    except Exception as e: 
     logging.exception("Error occured with xxxx API request: %s" % e) 
     return [], False 
関連する問題