Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .env.example
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
1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ packagePythonLambda 'check_links'
packagePythonLambda 'analytics_worker'
packagePythonLambda 'analytics_queue_reports'
packagePythonLambda 'datafile_generator'
packagePythonLambda 'events_queue_reindex_touched_dois'
19 changes: 19 additions & 0 deletions docker-compose.yml
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
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)
1 change: 1 addition & 0 deletions lambdas/events_queue_reindex_touched_dois/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
boto3
22 changes: 22 additions & 0 deletions run_lambda.sh

Copy link
Copy Markdown
Contributor

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.

@bklaing2 bklaing2 Jul 8, 2026

Copy link
Copy Markdown
Member Author

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/PATH variables needed in docker-compose.yml.

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