From c587e0907aceb6506f235f1103c68f44e5e090b5 Mon Sep 17 00:00:00 2001 From: nanos Date: Tue, 21 Mar 2023 14:42:31 +0000 Subject: [PATCH] finalise callbacks --- .github/workflows/get_context.yml | 2 +- README.md | 4 +++- find_posts.py | 40 +++++++++++++++++++++---------- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/.github/workflows/get_context.yml b/.github/workflows/get_context.yml index 13b0122..3b3bbd7 100644 --- a/.github/workflows/get_context.yml +++ b/.github/workflows/get_context.yml @@ -32,7 +32,7 @@ jobs: path: artifacts - name: Get Directory structure run: ls -lR - - run: python find_posts.py --lock-hours=0 --access-token=${{ secrets.ACCESS_TOKEN }} --server=${{ vars.MASTODON_SERVER }} --reply-interval-in-hours=${{ vars.REPLY_INTERVAL_IN_HOURS || 0 }} --home-timeline-length=${{ vars.HOME_TIMELINE_LENGTH || 0 }} --max-followings=${{ vars.MAX_FOLLOWINGS || 0 }} --user=${{ vars.USER }} --max-followers=${{ vars.MAX_FOLLOWERS || 0 }} --http-timeout=${{ vars.HTTP_TIMEOUT || 5 }} --max-follow-requests=${{ vars.MAX_FOLLOW_REQUESTS || 0 }} --finished-callback=${{ vars.FINISHED_CALLBACK }} --started-callback=${{ vars.STARTED_CALLBACK }} + - run: python find_posts.py --lock-hours=0 --access-token=${{ secrets.ACCESS_TOKEN }} --server=${{ vars.MASTODON_SERVER }} --reply-interval-in-hours=${{ vars.REPLY_INTERVAL_IN_HOURS || 0 }} --home-timeline-length=${{ vars.HOME_TIMELINE_LENGTH || 0 }} --max-followings=${{ vars.MAX_FOLLOWINGS || 0 }} --user=${{ vars.USER }} --max-followers=${{ vars.MAX_FOLLOWERS || 0 }} --http-timeout=${{ vars.HTTP_TIMEOUT || 5 }} --max-follow-requests=${{ vars.MAX_FOLLOW_REQUESTS || 0 }} --on-fail=${{ vars.ON_FAIL }} --on-start=${{ vars.ON_START }} --on-done=${{ vars.ON_DONE }} - name: Upload artifacts uses: actions/upload-artifact@v3 with: diff --git a/README.md b/README.md index b35e263..0c3701e 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,9 @@ Please see below for a list of configuration options. | `MAX_FOLLOW_REQUESTS` | `--max-follow-requests` | No | Provide to backfill profiles for the API key owner's most recent pending follow requests. Determines how many of your last follow requests you want to backfill. (An integer number, e.g. `80`.). Requires an access token with `read:follows` scope. | `HTTP_TIMEOUT` | `--http-timeout` | No | The timeout for any HTTP requests to the Mastodon API in seconds. Defaults to `5`. | -- | `--lock-hours` | No | Determines after how many hours a lock file should be discarded. Not relevant when running the script as GitHub Action, as concurrency is prevented using a different mechanism. -| `FINISHED_CALLBACK` | `--finished-callback` | No | Optionally provide a callback URL that will be called when processing is finished. This can be used for 'dead man switch' monitoring of cron jobs, for example via healthchecks.io. +| `ON_START` | `--on-start` | No | Optionally provide a callback URL that will be pinged when processing is starting. A query parameter `rid={uuid}` will automatically be appended to uniquely identify each execution. This can be used to monitor your script using a service such as healthchecks.io. +| `ON_DONE` | `--on-done` | No | Optionally provide a callback URL that will be called when processing is finished. A query parameter `rid={uuid}` will automatically be appended to uniquely identify each execution. This can be used to monitor your script using a service such as healthchecks.io. +| `ON_FAIL` | `--on-fail` | No | Optionally provide a callback URL that will be called when processing has failed. A query parameter `rid={uuid}` will automatically be appended to uniquely identify each execution. This can be used to monitor your script using a service such as healthchecks.io. #### Required Access Token Scopes diff --git a/find_posts.py b/find_posts.py index 1960a25..03c96a0 100644 --- a/find_posts.py +++ b/find_posts.py @@ -24,8 +24,9 @@ argparser.add_argument('--max-followers', required = False, type=int, default=0, argparser.add_argument('--max-follow-requests', required = False, type=int, default=0, help="Backfill posts of the API key owners pending follow requests. We'll backfill at most this many requester's posts") argparser.add_argument('--http-timeout', required = False, type=int, default=5, help="The timeout for any HTTP requests to your own, or other instances.") argparser.add_argument('--lock-hours', required = False, type=int, default=24, help="The lock timeout in hours.") -argparser.add_argument('--finished-callback', required = False, default=None, help="Provide a callback url that will be pinged when processing is complete. You can use this for 'dead man switch' monitoring of your task") -argparser.add_argument('--started-callback', required = False, default=None, help="Provide a callback url that will be pinged when processing is starting. You can use this for 'dead man switch' monitoring of your task") +argparser.add_argument('--on-done', required = False, default=None, help="Provide a url that will be pinged when processing has completed. You can use this for 'dead man switch' monitoring of your task") +argparser.add_argument('--on-start', required = False, default=None, help="Provide a url that will be pinged when processing is starting. You can use this for 'dead man switch' monitoring of your task") +argparser.add_argument('--on-fail', required = False, default=None, help="Provide a url that will be pinged when processing has failed. You can use this for 'dead man switch' monitoring of your task") def pull_context( server, @@ -692,6 +693,14 @@ if __name__ == "__main__": arguments = argparser.parse_args() + runId = uuid.uuid4() + + if(arguments.on_start != None): + try: + get(f"{arguments.on_start}?rid={runId}") + except Exception as ex: + log(f"Error getting callback url: {ex}") + LOCK_FILE = 'artifacts/lock.lock' if( os.path.exists(LOCK_FILE)): @@ -706,20 +715,22 @@ if __name__ == "__main__": log(f"Lock file has expired. Removed lock file.") else: log(f"Lock file age is {datetime.now() - lock_time} - below --lock-hours={arguments.lock_hours} provided.") + if(arguments.on_fail != None): + try: + get(f"{arguments.on_fail}?rid={runId}") + except Exception as ex: + log(f"Error getting callback url: {ex}") sys.exit(1) except Exception: log(f"Cannot read logfile age - aborting.") + if(arguments.on_fail != None): + try: + get(f"{arguments.on_fail}?rid={runId}") + except Exception as ex: + log(f"Error getting callback url: {ex}") sys.exit(1) - runId = uuid.uuid4() - - if(arguments.started_callback != None): - try: - get(f"{arguments.started_callback}?rid={runId}") - except Exception as ex: - log(f"Error getting callback url: {ex}") - with open(LOCK_FILE, "w", encoding="utf-8") as f: f.write(f"{datetime.now()}") @@ -770,9 +781,9 @@ if __name__ == "__main__": os.remove(LOCK_FILE) - if(arguments.finished_callback != None): + if(arguments.on_done != None): try: - get(f"{arguments.finished_callback}?rid={runId}") + get(f"{arguments.on_done}?rid={runId}") except Exception as ex: log(f"Error getting callback url: {ex}") @@ -781,4 +792,9 @@ if __name__ == "__main__": except Exception as ex: os.remove(LOCK_FILE) log(f"Job failed after {datetime.now() - start}.") + if(arguments.on_fail != None): + try: + get(f"{arguments.on_fail}?rid={runId}") + except Exception as ex: + log(f"Error getting callback url: {ex}") raise