Fixed updating of user/channel lists to not repeat forever and rate limit itself

This commit is contained in:
Jacob Henry 2019-02-22 06:03:59 -05:00
parent a79581413e
commit 5058ae0085
3 changed files with 21 additions and 18 deletions

View File

@ -45,7 +45,7 @@ def main() -> None:
# wrap.add_passive(periodicals.ItsTenPM()) # wrap.add_passive(periodicals.ItsTenPM())
# Add automatic updating of users # Add automatic updating of users
wrap.add_passive(periodicals.Updatinator(wrap, 60)) wrap.add_passive(periodicals.Updatinator(wrap, 120))
# Add nagloop # Add nagloop
wrap.add_passive(periodicals.NotifyJobs()) wrap.add_passive(periodicals.NotifyJobs())

View File

@ -121,12 +121,16 @@ class RemindJobs(slack_util.Passive, JobNotifier):
class Updatinator(slack_util.Passive): class Updatinator(slack_util.Passive):
"""
Periodically updates the channels and users in the slack
"""
def __init__(self, wrapper_to_update: slack_util.ClientWrapper, interval_seconds: int): def __init__(self, wrapper_to_update: slack_util.ClientWrapper, interval_seconds: int):
self.wrapper_target = wrapper_to_update self.wrapper_target = wrapper_to_update
self.interval = interval_seconds self.interval = interval_seconds
async def run(self): async def run(self):
# Give time to warmup
while True: while True:
await asyncio.sleep(self.interval)
self.wrapper_target.update_channels() self.wrapper_target.update_channels()
self.wrapper_target.update_users() self.wrapper_target.update_users()
await asyncio.sleep(self.interval)

View File

@ -169,8 +169,6 @@ class ClientWrapper(object):
# Cache users and channels # Cache users and channels
self.users: dict = {} self.users: dict = {}
self.channels: dict = {} self.channels: dict = {}
self.update_users()
self.update_channels()
# Scheduled events handling # Scheduled events handling
def add_passive(self, per: Passive) -> None: def add_passive(self, per: Passive) -> None:
@ -298,7 +296,7 @@ class ClientWrapper(object):
if cursor: if cursor:
args["cursor"] = cursor args["cursor"] = cursor
channel_dicts = self.api_call("https://slack.com/api/conversations.list", **args) channel_dicts = self.api_call("conversations.list", **args)
# If the response is good, put its results to the dict # If the response is good, put its results to the dict
if channel_dicts["ok"]: if channel_dicts["ok"]:
@ -307,15 +305,16 @@ class ClientWrapper(object):
name=channel_dict["name"]) name=channel_dict["name"])
new_dict[new_channel.id] = new_channel new_dict[new_channel.id] = new_channel
# If no new channels, just give it up # Fetch the cursor
if len(channel_dicts["channels"]) == 0:
break
# Otherwise, fetch the cursor
cursor = channel_dicts.get("response_metadata").get("next_cursor") cursor = channel_dicts.get("response_metadata").get("next_cursor")
# If cursor is blank, we're done new channels, just give it up
if cursor == "":
break
else: else:
print("Warning: failed to retrieve channels") print("Warning: failed to retrieve channels. Message: {}".format(channel_dicts))
break break
self.channels = new_dict self.channels = new_dict
@ -332,7 +331,7 @@ class ClientWrapper(object):
if cursor: if cursor:
args["cursor"] = cursor args["cursor"] = cursor
user_dicts = self.api_call("https://slack.com/api/users.list", **args) user_dicts = self.api_call("users.list", **args)
# Make a new dict to use # Make a new dict to use
new_dict = {} new_dict = {}
@ -346,15 +345,15 @@ class ClientWrapper(object):
email=user_dict.get("profile").get("email")) email=user_dict.get("profile").get("email"))
new_dict[new_user.id] = new_user new_dict[new_user.id] = new_user
# If no new channels, just give it up # Fetch the cursor
if len(user_dicts["channels"]) == 0:
break
# Otherwise, fetch the cursor
cursor = user_dicts.get("response_metadata").get("next_cursor") cursor = user_dicts.get("response_metadata").get("next_cursor")
# If cursor is blank, we're done new channels, just give it up
if cursor == "":
break
else: else:
print("Warning: failed to retrieve channels") print("Warning: failed to retrieve users")
break break
self.users = new_dict self.users = new_dict