Skip to content

Instantly share code, notes, and snippets.

@IuryAlves
Created December 5, 2016 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IuryAlves/51eb5ed0f16f7d967e607105eb73b25b to your computer and use it in GitHub Desktop.
Save IuryAlves/51eb5ed0f16f7d967e607105eb73b25b to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# django command to check if a url return a status != 2xx
import sys
import json
import aiohttp
import asyncio
from django.core.management.base import NoArgsCommand
from django.conf import settings
from photos.models import Image
@asyncio.coroutine
def fetch_image(image_id, url):
result = None
response = yield from aiohttp.get('https://{0}/{1}'.format(settings.NEW_CDN, url))
if response.status > 299:
result = url
response.close()
return result
class Command(NoArgsCommand):
def handle_noargs(self, **options):
tasks = [
fetch_image(image.id, image.url) for image in Image.objects.iterator()
]
out = []
exit_status = 0
if tasks:
loop = asyncio.get_event_loop()
done, _ = loop.run_until_complete(asyncio.wait(tasks))
loop.close()
out = [future.result() for future in done if future.result() is not None]
if out:
json.dump(out, sys.stdout)
exit_status = 1
sys.exit(exit_status)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment