def new_fn(*args, **kwargs): _wait_exponential_multiplier = 1 if wait_exponential_multiplier is None else wait_exponential_multiplier _wait_exponential_max = 1073741823 if wait_exponential_max is None else wait_exponential_max def exponential_sleep(previous_attempt_number): exp = 2**previous_attempt_number result = _wait_exponential_multiplier * exp if result > _wait_exponential_max: result = _wait_exponential_max if result < 0: result = 0 return result / 1000.0 attempt = 0 ret = False while attempt < times: attempt += 1 try: ret = func(*args, **kwargs) if ret: return ret except exceptions as e: logger.error(e) # Wait. if wait_exponential_multiplier: seconds = exponential_sleep(attempt - 1) logger.info(f"Sleep {seconds} seconds before retrying.") sleep(seconds) return ret
def disable_user(connection, user: User): cursor = connection.cursor() logger.info(f"Disable user: {user}") sql = "SELECT * " \ "FROM users AS u " \ "INNER JOIN teams AS t ON t.id = u.team_id " \ "WHERE u.google_id = %s" cursor.execute(sql, (user.google_id, )) ret = cursor.fetchall() if ret: sql = "UPDATE users " \ "SET active = %s " \ "WHERE google_id = %s" cursor.execute(sql, (False, user.google_id))
def add_user(connection, user: User): cursor = connection.cursor() logger.info(f"Add/update user: {user}") sql = "SELECT * " \ "FROM users AS u " \ "INNER JOIN teams AS t ON t.id = u.team_id " \ "WHERE u.google_id = %s" cursor.execute(sql, (user.google_id, )) ret = cursor.fetchall() if ret: logger.info(f"Update user: {user}") sql = "UPDATE users " \ "SET name = %s, email = %s, avatar_url = %s, space = %s, active = %s " \ "WHERE google_id = %s" cursor.execute(sql, (user.name, user.email, user.avatar_url, user.space, True, user.google_id)) else: logger.info(f"Add new user: {user}") sql = "INSERT INTO users (google_id, name, email, avatar_url, space, active) " \ "VALUES (%s, %s, %s, %s, %s, %s) " \ "RETURNING id" cursor.execute(sql, (user.google_id, user.name, user.email, user.avatar_url, user.space, True)) ret = cursor.fetchone() user_id, = ret time = '09:00:00' sql = "INSERT INTO schedules AS s (user_id, day, time, enabled) " \ "VALUES (%s, %s, %s, True), " \ " (%s, %s, %s, True), " \ " (%s, %s, %s, True), " \ " (%s, %s, %s, True), " \ " (%s, %s, %s, True), " \ " (%s, %s, %s, False), " \ " (%s, %s, %s, False)" values = () for day in Weekdays: values += (user_id, day, time) cursor.execute(sql, values)
import bot.utils.Questions as Questions from bot.utils.Logger import logger, setup_logger if __name__ == '__main__': setup_logger(True, '') # Get current time and weekday. now = datetime.now() time_str = now.strftime("%H:%M:%S") schedule_day = now.strftime("%A") # Get the users with an active schedule, which was not yet triggered. users = Database.get_users_with_schedule(day=schedule_day, time=time_str) if not users: exit(0) # Initialize the chat service. chat = Chat.get_chat_service() # Send the standup message. for name, google_id, space in users: if not space: continue logger.info(f"Trigger for user: {name}, {google_id}, {space}") Database.reset_standup(google_id=google_id) response = chat.spaces().messages().create( parent=space, body={'text': Questions.get_standup_question(name, '0_na')} ).execute() logger.debug(f"Response: {response}")
def update(connection): cursor = connection.cursor() def get_db_version() -> int: version = 0 try: sql = "SELECT * FROM db_version" cursor.execute(sql) if cursor.rowcount == 1: version, = cursor.fetchone() except psycopg2.DatabaseError as e: logger.error(f"Version table does not exist.") connection.rollback() return version logger.info("Update the database.") db_version = get_db_version() logger.info(f"Database version: {db_version}") while True: db_version = get_db_version() if db_version == 0: logger.info(f"Initialize the database the first time.") cursor.execute(f""" CREATE TYPE "day_type" AS ENUM ( 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ); CREATE TYPE "question_type" AS ENUM ( '0_na', '1_retrospect', '2_outlook', '3_blocking' ); CREATE TABLE "db_version" ( "version" int NOT NULL ); CREATE TABLE "teams" ( "id" SERIAL PRIMARY KEY, "name" varchar UNIQUE, "space" varchar ); CREATE TABLE "users" ( "id" SERIAL PRIMARY KEY, "google_id" varchar UNIQUE, "space" varchar UNIQUE, "name" varchar, "email" varchar UNIQUE, "avatar_url" varchar, "team_id" int, "active" boolean DEFAULT True ); CREATE TABLE "standups" ( "id" SERIAL PRIMARY KEY, "user_id" int, "question_type" question_type DEFAULT '0_na', "answer" varchar, "added" timestamp DEFAULT NOW(), "message_id" varchar ); CREATE TABLE "schedules" ( "id" SERIAL PRIMARY KEY, "user_id" int, "day" day_type, "enabled" boolean DEFAULT True, "time" time DEFAULT '09:00:00' ); ALTER TABLE "users" ADD FOREIGN KEY ("team_id") REFERENCES "teams" ("id"); ALTER TABLE "standups" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id"); ALTER TABLE "schedules" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id"); CREATE UNIQUE INDEX ON "schedules" ("user_id", "day"); INSERT INTO db_version VALUES({DB_VERSION}); """) connection.commit() if db_version >= DB_VERSION: break