Heroku caching api responses [django/restframework]



Heroku 接缝是缓存来自我的 Django/RestFramework 应用程序的 api 响应。我没有在 restframework 上启用缓存机制。

查看日志,我可以看到由未找到的对象引起的错误响应 (400(,并且就在 python 堆栈跟踪之前。然后我填充对象表,这里发生了问题。在下一个请求中,que错误不再发生,但响应与以前完全相同的400!

最终,我得到了替代的 200/400 回复。

编辑1:当我收到400响应时,不仅是代码,还有正文。我的代码没有抛出错误,但响应是代码 400 和第一条错误消息。

编辑2:添加了我的视图和序列化程序,省略了不相关的部分

有错误的日志:

2018-06-29T11:34:45.991094+00:00 app[web.1]: INFO 2018-06-29 08:34:45,990 dados: 1 [1/1] 6.34 Kb AGUARDANDO: importação síncrona iniciada
2018-06-29T11:34:46.044547+00:00 app[web.1]: UF Destino AC
2018-06-29T11:34:46.048998+00:00 app[web.1]: ERROR 2018-06-29 08:34:46,046 importacao_erp: Arquivo "1 [1/1] 6.34 Kb PROCESSANDO": icms[0]: objeto referenciado não existe (Uf matching query does not exist.)
2018-06-29T11:34:46.049002+00:00 app[web.1]: Traceback (most recent call last):
2018-06-29T11:34:46.049004+00:00 app[web.1]: File "/app/core/importacao_erp.py", line 123, in importacao
2018-06-29T11:34:46.049006+00:00 app[web.1]: method_save(dict(dados))
2018-06-29T11:34:46.049008+00:00 app[web.1]: File "/app/core/importacao_erp.py", line 282, in save_icms
2018-06-29T11:34:46.049009+00:00 app[web.1]: uf_destino=Uf.objects.get(sigla=dados['uf_destino']),
2018-06-29T11:34:46.049011+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
2018-06-29T11:34:46.049013+00:00 app[web.1]: return getattr(self.get_queryset(), name)(*args, **kwargs)
2018-06-29T11:34:46.049015+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 403, in get
2018-06-29T11:34:46.049016+00:00 app[web.1]: self.model._meta.object_name
2018-06-29T11:34:46.049022+00:00 app[web.1]: core.models.uf.DoesNotExist: Uf matching query does not exist.
2018-06-29T11:34:46.311674+00:00 app[web.1]: ERROR 2018-06-29 08:34:46,311 importacao_erp: Arquivo "1 [1/1] 6.34 Kb PROCESSANDO": clientes[0]: objeto referenciado não existe (Uf matching query does not exist.)
2018-06-29T11:34:46.311687+00:00 app[web.1]: Traceback (most recent call last):
2018-06-29T11:34:46.311690+00:00 app[web.1]: File "/app/core/importacao_erp.py", line 123, in importacao
2018-06-29T11:34:46.311693+00:00 app[web.1]: method_save(dict(dados))
2018-06-29T11:34:46.311695+00:00 app[web.1]: File "/app/core/importacao_erp.py", line 393, in save_clientes
2018-06-29T11:34:46.311696+00:00 app[web.1]: endereco['uf'] = Uf.objects.get(sigla=endereco['uf'])
2018-06-29T11:34:46.311698+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
2018-06-29T11:34:46.311700+00:00 app[web.1]: return getattr(self.get_queryset(), name)(*args, **kwargs)
2018-06-29T11:34:46.311702+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 403, in get
2018-06-29T11:34:46.311704+00:00 app[web.1]: self.model._meta.object_name
2018-06-29T11:34:46.311711+00:00 app[web.1]: core.models.uf.DoesNotExist: Uf matching query does not exist.
2018-06-29T11:34:46.412053+00:00 app[web.1]: 10.123.227.73 - - [29/Jun/2018:08:34:46 -0300] "POST /api/erp/dados/ HTTP/1.1" 400 548 "-" "insomnia/5.16.2"
2018-06-29T11:34:46.412316+00:00 heroku[router]: at=info method=POST path="/api/erp/dados/" host=solumobi-gweb.herokuapp.com request_id=99ef2ab0-a6a5-4a3a-a767-157d9acfc355 fwd="177.156.123.248" dyno=web.1 connect=1ms service=447ms status=400 bytes=794 protocol=https

然后,没有错误,但仍然是 400:

2018-06-29T11:35:31.299633+00:00 app[web.1]: INFO 2018-06-29 08:35:31,299 dados: 3 [1/1] 6.34 Kb AGUARDANDO: importação síncrona iniciada
2018-06-29T11:35:31.342317+00:00 app[web.1]: UF Destino AC
2018-06-29T11:35:31.721914+00:00 heroku[router]: at=info method=POST path="/api/erp/dados/" host=solumobi-gweb.herokuapp.com request_id=55e90b74-4c37-42ef-872b-66a8f64bb9cc fwd="177.156.123.248" dyno=web.1 connect=0ms service=437ms status=400 bytes=794 protocol=https
2018-06-29T11:35:31.721356+00:00 app[web.1]: 10.123.227.73 - - [29/Jun/2018:08:35:31 -0300] "POST /api/erp/dados/ HTTP/1.1" 400 548 "-" "insomnia/5.16.2"
2018-06-29T11:35:40.270332+00:00 app[web.1]: INFO 2018-06-29 08:35:40,270 dados: 4 [1/1] 6.34 Kb AGUARDANDO: importação síncrona iniciada
2018-06-29T11:35:40.328652+00:00 app[web.1]: UF Destino AC
2018-06-29T11:35:40.960751+00:00 heroku[router]: at=info method=POST path="/api/erp/dados/" host=solumobi-gweb.herokuapp.com request_id=cb358bca-3301-4c1e-8468-6387ae7aaaea fwd="177.156.123.248" dyno=web.1 connect=0ms service=726ms status=400 bytes=794 protocol=https
2018-06-29T11:35:40.960283+00:00 app[web.1]: 10.123.227.73 - - [29/Jun/2018:08:35:40 -0300] "POST /api/erp/dados/ HTTP/1.1" 400 548 "-" "insomnia/5.16.2"
2018-06-29T11:35:48.349108+00:00 app[web.1]: INFO 2018-06-29 08:35:48,348 dados: 5 [1/1] 6.34 Kb AGUARDANDO: importação síncrona iniciada
2018-06-29T11:35:48.418827+00:00 app[web.1]: UF Destino AC
2018-06-29T11:35:48.869927+00:00 heroku[router]: at=info method=POST path="/api/erp/dados/" host=solumobi-gweb.herokuapp.com request_id=9a5e8746-57b2-4e89-b5d6-8f40a89d1eee fwd="177.156.123.248" dyno=web.1 connect=0ms service=541ms status=400 bytes=794 protocol=https

查看代码,省略不相关的部分:

router.register('dados', DadosViewSet, base_name="api-erp-dados")
class DadosViewSet(viewsets.ViewSet):           
def create(self, request):
arquivo = ArquivoImportacao()
arquivo.dados = dados
arquivo.save()    
ImportadorERP(arquivo).importar()
status = 201 if arquivo.status == STATUS_IMPORTADO else 400   
s = ArquivoImportacaoSerializer(arquivo)              
return Response(s.data, status)

class ArquivoImportacaoSerializer(serializers.ModelSerializer):
log = ArquivoImportacaoLogSerializer(source="arquivoimportacaolog_set", many=True, read_only=True)
tamanho = serializers.CharField(source="tamanho_fmt", read_only=True)
class Meta:
model = ArquivoImportacao
fields = ['codigo', 'data', 'tamanho', 'status', 'log']

我想通了。上面的类 ImportadorERP 在错误的位置声明了它的日志列表,所以它变成了静态的,保存着来自它进程的多个请求的日志。

很抱歉浪费你的时间。谢谢你的帮助。

最新更新