# daemon-transcoding
daemon-transcoding

### Installation

The installation is very straightforward, but make sure you are using at least
python 3.6. If you use a Mac, you can install it using `brew`. If you use
Windows, just ask for a remote VM that has it installed for you.

```bash
$ cd daemon-transcoding
$ pyvenv env
(env) $ . env/bin/activate
(env) $ pip install -r requirements.txt
(env) $ pip install -r requirements-dev.txt
```

### Permissions

In PROD and QA environments, to accept and authorize incoming and outgoing
requests, make sure you have the right DynamoDB permissions, as highlighted
in the corresponding [tech design](https://docs.google.com/document/d/1eHoI_BddTFMi15yCaHS6KvhSSoTrMEd3WwJINIpgNpM/edit).

### Running Locally

Run daemon-transcoding inside a dev container. Before spinning up the container, you first need to:

1. Ensure you have [ECR access](https://www.notion.so/AWS-Access-f841b9dd815d4443a80e96a86c92cd2f#7a6624c58c6642abaecd804e5f25c820) set up.
2. Enable AWS access to the dev or prod account with [awsume](https://www.notion.so/AWS-Access-f841b9dd815d4443a80e96a86c92cd2f#beaec95488ad4473899a26a2b8cf603f).

Then, spin up the dev container:

```bash
$ docker compose run --build --rm daemon
```

This will spin up a dev container, install/copy all required dependencies and app code inside the dev container, and start the application.

By using the development server, you will have access to specific features that
are not necessarily available in production, such as the exception tracer.

### Using Localstack

With help of Localstack, you can test SQS and S3 interactions locally without needing to deploy to AWS.

Localstack will start automatically when you will start a daemon container.

When you run localstack it automatically runs bootstrap scripts located in `localstack/init` directory.

Bootstrap script located [01-bootstrap.sh](localstack/init/01-bootstrap.sh) will create the SQS queue and S3 buckets that can be used for local testing.

- S3 buckets: transcoding-in, transcoding-out
- SQS queue: transcoding-dev

To trigger daemon-transcoding processing, you can use the AWS CLI to send messages to the local SQS queue:

```bash
docker compose exec localstack awslocal sqs send-message \
  --queue-url http://localhost:4566/000000000000/transcoding-dev \
  --message-body '{"transcoding_job_id":2,
  "input_bucket":"transcoding-in",
  "input_key":"in.wav",
  "output_bucket":"transcoding-out",
  "output_key":"out.wav",
  "container": "wave",
  "channels": 2,
  "codec": "pcm",
  "sample_rate": 44100,
  "bit_rate": 2116800,
  "bit_depth": 24}'
```

Pass through example:
```bash
docker compose exec localstack awslocal sqs send-message \
  --queue-url http://localhost:4566/000000000000/transcoding-dev \
  --message-body '{"transcoding_job_id":2,
  "input_bucket":"transcoding-in",
  "input_key":"in.wav",
  "output_bucket":"transcoding-out",
  "output_key":"out.wav",
  "pass_thru":true}'
```

Input key must exist in the input S3 bucket for the message to be processed. You can upload files to the local S3 bucket using the AWS CLI as well:

```bash
docker compose cp in.wav localstack:/tmp/in.wav
docker compose exec localstack awslocal s3 cp /tmp/in.wav s3://transcoding-in/in.wav
````
This script copies file to the container and then uploads it to the local S3 bucket, since aws cli is hosted inside the localstack container.

Also note that sent `transcoding_job_id` in the message must be present in `ows-transcoding`.

### Testing

To run tests within a Docker container, you first must ensure steps 1 and 2 in `Running Locally` are complete, and ensure you are on the VPN.

To run unit tests and linting, run:

```bash
docker compose run --build --rm unit-lint
```

