# Trigger Step Function (SFN) Content Match Audio

## What causes this lambda to run?

A message published on the Kafka topic [`stream.asset.mezzanine.audio.flac`](https://prod-akhq.theorchard.io/ui/prod-cdc/topic/stream.asset.mezzanine.audio.flac/data?sort=Newest&partition=All).

Example:

```json
{
    "ASSET_FINAL_ID": 26493667,
    "ASSET_UPLOAD_ID": 16820942,
    "ASSET_TYPE": "FLAC",
    "ASSET_SUBTYPE": "none",
    "BUCKET": "prod-orcd-mezzanine-assets",
    "FILENAME": "08c3714a_5fee_4fb1_9773_da0b8dd177d7.flac",
    "METADATA": null,
    "DURATION_MS": 546460
}
```

## What does this lambda do?

1. `StartExecution` of the AWS Step Functions State Machine: [`sfn-content-match-audio`](https://github.com/theorchard/terraform-infra/blob/master/prod/lambda-content/match-audio/sfn-content-match-audio.json) with payload containing `asset_final_id`, `bucket`, `duration_ms`, and `filename`.

Example:

```json
{
      "asset_final_id": 26493667,
      "bucket": "prod-orcd-mezzanine-assets",
      "filename": "08c3714a_5fee_4fb1_9773_da0b8dd177d7.flac",
      "duration_ms": 1234
}
```


## How is this lambda deployed?
### Deploy Configuration
#### QA
https://github.com/theorchard/terraform-infra/tree/master/qa/lambda-content
#### Prod
https://github.com/theorchard/terraform-infra/tree/master/prod/lambda-content
### PR Build
https://pipeline.theorchard.io/job/lambda-content-pull-request/
### Deploy Pipeline
https://pipeline.theorchard.io/job/lambda-content-pipeline/

## Usage

### Install

To install a development environment, you can run:

```sh
make pip_dev
```

### Requirements

- python3.11

Direct dependencies are listed in `requirements-to-freeze.txt` and locked with
their own dependencies in `requirements.txt`. `requirements.txt` is imported into
`requirements-test.txt` and testing dependencies are listed. To bump versions
of dependencies, update the version in `requirements-to-freeze.txt` and run
the command below to regenerate `requirements.txt`.

```sh
make pip_lock_versions
```

### Development

```sh
# Run the function locally using a test event and print results
make dev

# Run linting
make lint

# Run tests
make test

# Build the docker image, launch it, and run linting and tests against it
make docker_test

# Rebuild your local env from scratch
make clean dev
```

## Docker Stack

The docker stack offers an environment like the one on AWS for developing against.

```sh
# Bring the function up in a docker-compose stack. The container will mount
# src dir so it will update immediately as you develop.
make docker_up

# Send a mock event to the function
make docker_event

# Bring the stack down
make docker_down
```

> Note that this requires docker to be running locally and for you to grant docker
> permission to access your local directory.
