Handle failing download_remote_screenshot task when App was deleted
When a RemoteApp
was added to the repo, but deleted right away, it might happen that there are still screenshots to be downloaded. The download_remote_screenshot
fails with the following error likely because it can't find the App
anymore that the screenshot should be added to.
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/related_descriptors.py", line 178, in __get__
rel_obj = getattr(instance, self.cache_name)
AttributeError: 'Screenshot' object has no attribute '_app_cache'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/background_task/tasks.py", line 44, in bg_runner
func(*args, **kwargs)
File "/var/local/repomaker/maker/tasks.py", line 99, in download_remote_screenshot
screenshot.download(app_id)
File "/var/local/repomaker/maker/models/screenshot.py", line 96, in download
screenshot.file.save(os.path.basename(self.url), BytesIO(r.content), save=True)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/files.py", line 94, in save
name = self.field.generate_filename(self.instance, name)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/files.py", line 328, in generate_filename
filename = self.upload_to(instance, filename)
File "/var/local/repomaker/maker/storage.py", line 50, in get_screenshot_file_path
path = os.path.join(get_repo_path(screenshot.app.repo), screenshot.get_relative_path())
File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/related_descriptors.py", line 184, in __get__
rel_obj = self.get_object(instance)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/related_descriptors.py", line 159, in get_object
return qs.get(self.field.get_reverse_related_filter(instance))
File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 379, in get
self.model._meta.object_name
maker.models.app.DoesNotExist: App matching query does not exist.
Besides fixing this, we should add a test to test_tasks.py
.