Skip to content

Commit

Permalink
Use asyncio to fetch from JIRA
Browse files Browse the repository at this point in the history
  • Loading branch information
dandavison committed Sep 12, 2018
1 parent ea473a5 commit 18436e4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
19 changes: 15 additions & 4 deletions facet/cli.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import asyncio
import json
import os
import shutil
import subprocess
import sys

import json
import aiohttp

from facet import settings
from facet.cli_dispatch import Dispatcher
Expand Down Expand Up @@ -186,9 +188,18 @@ def fetch(self, options):
else:
include_inactive = options.get('--all')
facets = Facet.get_all(include_inactive)
for facet in facets:
facet.fetch()
print(facet.format())

async def fetch_all_facets():
conn = aiohttp.TCPConnector(ssl=False)
async with aiohttp.ClientSession(connector=conn) as session:
coros = [facet._fetch_async(session) for facet in facets]
await asyncio.wait(coros)

event_loop = asyncio.new_event_loop()
try:
event_loop.run_until_complete(fetch_all_facets())
finally:
event_loop.close()

def follow(self, options):
"""
Expand Down
19 changes: 17 additions & 2 deletions facet/core.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# -*- coding: utf-8 -*-
import asyncio
import json
import subprocess
from os import listdir
from os import path

import aiohttp
import requests
import yaml

Expand Down Expand Up @@ -77,14 +79,27 @@ def apply_patch(self, patch):
self.write_config(config)

def fetch(self):
async def _fetch():
conn = aiohttp.TCPConnector(ssl=False)
async with aiohttp.ClientSession(connector=conn) as session:
await self._fetch_async(session)

event_loop = asyncio.new_event_loop()
try:
event_loop.run_until_complete(_fetch())
finally:
event_loop.close()

async def _fetch_async(self, session):
if not self.jira:
return
resp = requests.get(self.jira_json_url)
resp = await session.get(self.jira_json_url)
resp.raise_for_status()
_json = resp.json()
_json = await resp.json()
assert _json
with open(self.jira_data_file, 'w') as fp:
dump_json(_json, fp)
print(self.format())

def format(self):
if self.jira:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
],
},
install_requires=[
'aiohttp',
'clint',
'docopt',
'pyyaml',
'requests',
],
)

0 comments on commit 18436e4

Please sign in to comment.