-
Notifications
You must be signed in to change notification settings - Fork 1
Reindex touched dois daily #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ee9f736
add new `events_queue_reindex_touched_dois` lambda
bklaing2 4b16fc1
add docker-compose and bash script to test lambdas locally with datac…
bklaing2 17091ef
set the date as yesterday instead of today
bklaing2 24b9b24
add lambda to trigger daily events reindex to the build script
bklaing2 db474b6
simplify the `run_lambda` script
bklaing2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| AWS_ACCESS_KEY_ID=test | ||
| AWS_SECRET_ACCESS_KEY=test | ||
| AWS_DEFAULT_REGION=us-east-1 | ||
| AWS_ENDPOINT_URL=http://localstack:4566 | ||
|
|
||
| QUEUE_NAME=development_events_reindex_daily |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| services: | ||
| lambda: | ||
| image: python:3.14.6-alpine | ||
| environment: | ||
| - AWS_ACCESS_KEY_ID=test | ||
| - AWS_SECRET_ACCESS_KEY=test | ||
| - AWS_DEFAULT_REGION=us-east-1 | ||
| - AWS_ENDPOINT_URL=http://localstack:4566 | ||
| - QUEUE_NAME | ||
| networks: | ||
| - localstack_network | ||
| volumes: | ||
| - ${LAMBDA_DIR}:/var/task | ||
| working_dir: /var/task | ||
| command: sh -c "pip install --quiet -r requirements.txt && python ${LAMBDA_PATH}" | ||
|
|
||
| networks: | ||
| localstack_network: | ||
| external: true |
43 changes: 43 additions & 0 deletions
43
lambdas/events_queue_reindex_touched_dois/events_queue_reindex_touched_dois_runner.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| """ | ||
| This lambda function is triggered by a CloudWatch event rule on a schedule. | ||
| It add a message to the queue with the current date, to be processed by Events. | ||
| Events will call the `reindex_touched_dois` method to reindex the DOIs touched today. | ||
| """ | ||
|
|
||
| import os | ||
| import json | ||
| import datetime | ||
| import boto3 | ||
|
|
||
| # Lambda handler | ||
| def lambda_handler(event, context): | ||
| """Lambda process handler""" | ||
| print("Starting reindex_touched_dois_runner lambda function") | ||
|
|
||
| # Configuration | ||
| queue_name = os.getenv('QUEUE_NAME') | ||
|
|
||
| # Calculate the date range for the reindex | ||
| yesterday = datetime.datetime.now(datetime.UTC) - datetime.timedelta(days=1) | ||
| yesterday = yesterday.strftime("%Y-%m-%d") | ||
|
|
||
| message = { | ||
| 'Name': 'shoryuken_class', | ||
| 'Type': 'String', | ||
| 'Value': 'ReindexTouchedDoisWorker', | ||
|
|
||
| 'date': yesterday, | ||
| } | ||
|
|
||
| # Queue a task for each repository | ||
| sqs = boto3.resource('sqs') | ||
| queue = sqs.get_queue_by_name(QueueName=queue_name) | ||
| queue.send_message(MessageBody=json.dumps(message)) | ||
|
|
||
| return("Queued reindex for dois touched on {}".format(yesterday)) | ||
|
|
||
| if __name__ == '__main__': | ||
| # For local testing fake the arguments to lambda handler function | ||
| event = {} | ||
| context = [] | ||
| lambda_handler(event, context) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| boto3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| usage() { | ||
| cat <<'EOF' | ||
| Usage: | ||
| ./run_lambda.sh <path/to/lambda_runner.py> | ||
|
|
||
| Additional environment variables need to be added to docker-compose.yml | ||
| EOF | ||
| } | ||
|
|
||
| [[ $# -eq 1 ]] || { usage; exit 1; } | ||
|
|
||
| SCRIPT_PATH="$1" | ||
| [[ -f "$SCRIPT_PATH" ]] || { echo "Error: file not found: $SCRIPT_PATH" >&2; exit 1; } | ||
|
|
||
| LAMBDA_DIR="$(dirname "$(realpath "$SCRIPT_PATH")")" | ||
| LAMBDA_PATH="$(basename "$SCRIPT_PATH")" | ||
| export LAMBDA_DIR LAMBDA_PATH | ||
|
|
||
| docker compose run --rm lambda |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While fine, this seems a little more complicated than needed.
You could specify the AWS localstack env variables in a .env and load this in the docker compose.
The only KEY being passed through to running is QUEUE_NAME, which again is probably always same for local testing and can be defined in the .env
So instead of running this SH, you could just run the docker compose run directly.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree it was a bit overkill. I've simplified the script, but for the moment it is still necessary to extract the
LAMBDA_DIR/PATHvariables needed indocker-compose.yml.