diff --git a/pkgs/matrix-bot/matrix_bot/__init__.py b/pkgs/matrix-bot/matrix_bot/__init__.py index d481161..d624126 100644 --- a/pkgs/matrix-bot/matrix_bot/__init__.py +++ b/pkgs/matrix-bot/matrix_bot/__init__.py @@ -120,6 +120,13 @@ def create_parser(prog: str | None = None) -> argparse.ArgumentParser: type=Path, ) + parser.add_argument( + "--poll-frequency", + help="The frequency to poll for new reviews in minutes", + default=10, + type=float, + ) + return parser @@ -161,6 +168,7 @@ def main() -> None: owner=args.repo_owner, repo=args.repo_name, access_token=os.getenv("GITEA_ACCESS_TOKEN"), + poll_frequency=args.poll_frequency, ) args.data_dir.mkdir(parents=True, exist_ok=True) diff --git a/pkgs/matrix-bot/matrix_bot/changelog_bot.py b/pkgs/matrix-bot/matrix_bot/changelog_bot.py index 41d7f70..94411dc 100644 --- a/pkgs/matrix-bot/matrix_bot/changelog_bot.py +++ b/pkgs/matrix-bot/matrix_bot/changelog_bot.py @@ -109,31 +109,32 @@ async def changelog_bot( last_run_path = data_dir / "last_changelog_run.json" last_run = read_locked_file(last_run_path) + today = datetime.datetime.now() + today_weekday = today.strftime("%A") + + if today_weekday != matrix.publish_day: + log.debug(f"Changelog not due yet. Due on {matrix.publish_day}") + return + if last_run == {}: - fromdate, todate = last_ndays_to_today(matrix.changelog_frequency) - last_run = { - "fromdate": fromdate, - "todate": todate, - "ndays": matrix.changelog_frequency, - } log.debug(f"First run. Setting last_run to {last_run}") - today = datetime.datetime.now() - today_weekday = today.strftime("%A") - if today_weekday != matrix.publish_day: - log.debug(f"Changelog not due yet. Due on {matrix.publish_day}") - return else: last_date = datetime.datetime.strptime(last_run["todate"], "%Y-%m-%d") - today = datetime.datetime.now() - today_weekday = today.strftime("%A") - delta = datetime.timedelta(days=matrix.changelog_frequency) - if today - last_date <= delta: - log.debug(f"Changelog not due yet. Due in {delta.days} days") - return - elif today_weekday != matrix.publish_day: - log.debug(f"Changelog not due yet. Due on {matrix.publish_day}") + upper_bound = datetime.timedelta(days=matrix.changelog_frequency) + delta = today - last_date + if delta <= upper_bound: + log.debug( + f"Changelog not due yet. Due in {upper_bound.days - delta.days} days" + ) return + fromdate, todate = last_ndays_to_today(matrix.changelog_frequency) + last_run = { + "fromdate": fromdate, + "todate": todate, + "ndays": matrix.changelog_frequency, + } + # If you made a new room and haven't joined as that user, you can use room: JoinResponse = await client.join(matrix.changelog_room) @@ -161,6 +162,11 @@ async def changelog_bot( fromdate, todate = last_ndays_to_today(matrix.changelog_frequency) log.info(f"Generating changelog from {fromdate} to {todate}") + # Write the last run to the file before processing the changelog + # This ensures that the changelog is only generated once per period + # even if openai fails + write_locked_file(last_run_path, last_run) + system_prompt = f""" Create a concise changelog Follow these guidelines: @@ -216,8 +222,6 @@ For the last {matrix.changelog_frequency} days from {fromdate} to {todate} all_changelogs.append(changelog) full_changelog = "\n\n".join(all_changelogs) - # Write the last run to the file - write_locked_file(last_run_path, last_run) log.info(f"Changelog generated:\n{full_changelog}") await send_message(client, room, full_changelog) diff --git a/pkgs/matrix-bot/matrix_bot/gitea.py b/pkgs/matrix-bot/matrix_bot/gitea.py index 470ba80..d1c7250 100644 --- a/pkgs/matrix-bot/matrix_bot/gitea.py +++ b/pkgs/matrix-bot/matrix_bot/gitea.py @@ -14,7 +14,8 @@ class GiteaData: url: str owner: str repo: str - access_token: str | None = None + poll_frequency: float + access_token: str | None def endpoint_url(gitea: GiteaData, endpoint: str) -> str: diff --git a/pkgs/matrix-bot/matrix_bot/main.py b/pkgs/matrix-bot/matrix_bot/main.py index 17ba040..6c677f2 100644 --- a/pkgs/matrix-bot/matrix_bot/main.py +++ b/pkgs/matrix-bot/matrix_bot/main.py @@ -59,7 +59,8 @@ async def bot_main( client, matrix, f"Review requested bot failed: {e}" ) - await asyncio.sleep(60 * 5) + log.debug(f"Sleeping for {60 * gitea.poll_frequency / 60} minutes") + await asyncio.sleep(60 * gitea.poll_frequency) except Exception as e: log.exception(e) finally: