2017-12-28 6 views
0

私はサードパーティのサービスでアカウントを作成し、そのサービスからの関連データを私のprofileオブジェクトに保存するタスクを実行しています。タスクが実行されており、データは正しく返されていますが、オブジェクトに正しく保存されていません。応答データをインスタンスのフィールドに保存できません。同じブロック内の他のデータは正しく保存されます(Django/Celery)

関連するコード:

# payload is generated in helper method. if that was the point of 
    # failure, i'd see an error 
    result = self.third_party_client.post(self.creation_endpoint, payload) 

    json_result = result.json() 

    if json_result.get('Error') != 'SUCCESS': 
     # Account Creation Failed. error is handled. 
    else: 
     # Currently I log this on our staging server to make sure 
     # the data's coming back properly; it is returned as expected 
     self.logger.error('CoreCard Account Created. Data: {}'.format(json_result)) 
     profile = user.profile 
     profile.account_number = json_result.get('AccountNumber') 
     profile.card_number_last_four = int(json_result.get('CardNumber')[12:]) 
     profile.customer_id = json_result.get('CustomerID') 
     profile.account_creation_datetime = datetime.datetime.utcnow() 
     profile.save() 

私は、このprofileインスタンスを照会するのであれば、期待どおりにaccount_creation_datetime値を持っていますが、他のフィールドは空白になっています。私はログをチェックして、json_resultの値がすべて正しいです。

ここはちょっと変わったところです。実際には失敗していると思うところは、このコードを実行するユーティリティを呼び出すセロリのタスクです。このユーティリティを手動で実行すると、フィールドが正しく保存されます。通常のワークフロー(非同期タスクと呼ばれる)の一部として実行すると、このコードが実行されていることがわかるので、アカウントはサードパーティのサービスで作成されますが、profileフィールド(account_creation_datetimeを除く)は空白です。タスクは、次のようになります。このワークフローで

@shared_task(bind=True, max_retries=3) 
def create_account_task(self, guid): 
    profile = Profile.objects.get(historical_id=guid) 
    profile.account_creation_worker_id = current_task.request.id # Per http://docs.celeryproject.org/en/latest/reference/celery.html#celery.current_task 
    profile.save() 

    # Sanity Check 
    if profile.approval_date is not None: 
     util = CoreCardUtility() 
     util.create_corecard_account(profile.user) 
     return True 
    return False 

profile.account_creation_worker_idは、アカウントは、サードパーティのサービス上で作成されたので、私はタスクが実行知っていても、またが正しく保存されないです。

私は少し紛失しています。セロリの作業者やスレッディングに問題がありますか?

ありがとうございました!

答えて

0

profile.account_creation_worker_id = create_account_task.request.id 

代わりの

profile.account_creation_worker_id = current_task.request.id 

おそらく、(2.2.0以上セロリのバージョン用)のタスクIDを取得するには、あなたのタスクが実行されていないと、エラーをスローする必要があります。セロリログをチェックしましたか?

+0

こんにちはAshish、ご意見ありがとうございます!私はこれがエラーだとは思わない。タスクが実行されていない場合、アカウントはサードパーティのサービスで作成されません。 Celeryドキュメントごとに 'current_task'パターンを使用します。それはどこにでもうまくいきました。 http://docs.celeryproject.org/en/latest/reference/celery.html#celery.current_task –

関連する問題