使用Django-RQ (v2.3.0)
查询(PostgreSQL)
数据库仅在完成两次后才有效。这是设置:
models.py
:
class AnalysisResult(models.Model):
user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
public_id = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
tasks.py
:
@job(os.getenv("REDIS_QUEUE_NAME", "default"))
def update_progress_in_db(result_id: UUID):
# FIXME This query only works on the second try when we run this here inside the rqworker?!
try:
tmp_record = AnalysisResult.objects.get(public_id=result_id)
except (AnalysisResult.DoesNotExist, AnalysisResult.MultipleObjectsReturned) as e:
# Always getting the exception here!
logger.error("Error retrieving tmp_record with id={}: {}.".format(result_id, e))
except Exception as ex:
logger.error("General error while retrieving tmp_record: {}".format(ex))
try:
record = AnalysisResult.objects.get(public_id=result_id)
logger.debug("result record: {}".format(record)) # Working fine.
except (AnalysisResult.DoesNotExist, AnalysisResult.MultipleObjectsReturned) as e:
# Never getting this exception.
logger.error("Error updating progress status with id={}: {}.".format(result_id, e))
except Exception as ex:
logger.error("General error while updating progress status: {}".format(ex))
AnalysisResult.objects.get(public_id=result_id)
在django-admin shell
第一次尝试时总是运行良好。而且我非常不想使用上述尝试查询记录两次的解决方法。就是觉得太脏了。
感谢您提供有关此处可能缺少的内容的指针。