Skip to content

Commit

Permalink
Implement caching for slack channels
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrasband committed Jun 23, 2019
1 parent a4eef5e commit 08985f7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 32 deletions.
2 changes: 0 additions & 2 deletions pyslackersweb/website/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ def app_factory() -> web.Application:
website.update( # pylint: disable=no-member
redis_uri=settings.REDIS_URL,
slack_invite_token=settings.SLACK_INVITE_TOKEN,
slack_timezones={},
slack_token=settings.SLACK_TOKEN,
slack_user_count=0,
)

# aiohttp_jinja2 requires this values to be set. Sadly it does not work with subapplication.
Expand Down
3 changes: 2 additions & 1 deletion pyslackersweb/website/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ async def background_jobs(app: web.Application) -> None:
)

scheduler.add_job(
tasks.sync_slack_channels(app),
tasks.sync_slack_channels,
"cron",
minute=15,
jitter=30,
next_run_time=datetime.utcnow() + timedelta(seconds=60),
args=(app["slack_client"], app["redis"]),
)

yield
Expand Down
54 changes: 25 additions & 29 deletions pyslackersweb/website/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import List

import slack
from aiohttp import ClientSession, web
from aiohttp import ClientSession
from aioredis.abc import AbcConnection
from slack.io.abc import SlackAPI

Expand All @@ -16,6 +16,8 @@

GITHUB_REPO_CACHE_KEY = "github:repos"

SLACK_CHANNEL_CACHE_KEY = "slack:channels"

SLACK_COUNT_CACHE_KEY = "slack:user:count"

SLACK_TZ_CACHE_KEY = "slack:user:timezones"
Expand Down Expand Up @@ -107,36 +109,30 @@ async def sync_slack_users(
return


def sync_slack_channels(app: web.Application):
client = app["slack_client"]

async def _sync_slack_channel():
logger.debug("Refreshing slack channels cache.")
oauth_token = app["slack_token"]

if oauth_token is None:
logger.error("No slack oauth token set, unable to sync slack channels.")
return

try:
channels = []
async for channel in client.iter(slack.methods.CHANNELS_LIST):
channels.append(
Channel(
id=channel["id"],
name=channel["name"],
topic=channel["topic"]["value"],
purpose=channel["purpose"]["value"],
members=channel["num_members"],
)
async def sync_slack_channels(
slack_client: SlackAPI, redis: AbcConnection, *, cache_key: str = SLACK_CHANNEL_CACHE_KEY
):
logger.debug("Refreshing slack channels cache.")

try:
channels = []
async for channel in slack_client.iter(slack.methods.CHANNELS_LIST):
channels.append(
Channel(
id=channel["id"],
name=channel["name"],
topic=channel["topic"]["value"],
purpose=channel["purpose"]["value"],
members=channel["num_members"],
)
)

logger.debug("Found %s slack channels", len(channels))
channels.sort(key=lambda c: c.name)

app.update(slack_channels=sorted(channels, key=lambda c: c.name))
logger.debug("Found %s slack channels", len(channels))

except Exception: # pylint: disable=broad-except
logger.exception("Error refreshing slack channels cache")
return
await redis.set(cache_key, json.dumps([x.__dict__ for x in channels]))

return _sync_slack_channel
except Exception: # pylint: disable=broad-except
logger.exception("Error refreshing slack channels cache")
return

0 comments on commit 08985f7

Please sign in to comment.