# swf-activity-detector

This package can be used to create activities which will be triggered from the scheduler.

## Installation
To install the package, first clone this repository in your current directory.

```sh
$ git clone git@github.com:theorchard/swf-activity-detector.git
```

Then create a virtual environment `env` into which to install the package and activate it:

```sh
$ python -m venv env
$ source env/bin/activate
```

Next install the dependencies, then the package itself:

```sh
$ pip install -r requirements-test.txt
$ pip install -r requirements.txt
$ pip install -e .
```
The additional requirements file `requirements-test.txt` contains dependencies for testing and linting.
Using the `-e` option when installing the package allows changes you make to the package to take effect
immediately, without having to reinstall.

Copy the environment shadow file `.env.shadow` to a different file (e.g., `.env`) and fill in
the various values corresponding to your AWS and Snowflake credentials.

To set them, run:

```sh
$ source .env
```

## Run an activity

Installing the package gives you access to the command-line utility `garcon`, which is used to
run the flow to create views:

```sh
$ garcon [decider | worker | exec]
```

To see how this works, in one terminal window, run `garcon decider {activity_name}` to set up a decider daemon to
handle activity orchestration for the workflow. Then in another terminal, run `garcon worker {activity_name}`,
which is used to actually process the activities. Finally, kick off the job with `garcon exec {activity_name}`.

Passing context to the activity execution:

```sh
$ garcon exec {activity_name} --context "{context_data}"
```

To learn more about Garcon (a lightweight library for AWS SWF) on which swf-activity-detector is based,
please see the [garcon repository](https://github.com/theorchard/ows-garcon).

## Tests

To run the tests, first make sure the test dependencies have been installed (i.e., you've run
`pip install -r requirements-test.txt`). Then run:

```sh
$ py.test tests/
```

## Linting Checks

To run linting checks, make sure the test dependencies are installed and run:

```sh
$ flake8 activity_detector/ tests/
```


# Docker Instructions

## Building the Docker Image
To build the Docker image for the `swfactivitydetector` application, use the following command:

docker build -t swfactivitydetector .

This command will create a Docker image with a tag (ex: `swfactivitydetector`) based on the Dockerfile in the current directory.

## Running the Docker Container
There are multiple ways to run the `swfactivitydetector` Docker container, depending on how you want to configure the environment variables.

### Method 1: Individual Environment Variables
To run the container with individual environment variables passed as command-line arguments, use the following command:

```sh
docker run \
  -e DD_ENV=value1 \
  -e AR_DB_URL=value2 \
  swfactivitydetector
```

Replace `DD_ENV` and `AR_DB_URL` with the desired environment variable names, and `value1` and `value2`, etc with the corresponding values you want to set.
Change `swfactivitydetector` if you want to use a different tag for the Docker image.

### Method 2: Environment Variables from a File
If you prefer using your existing .env file you have been using to run the project locally, you can use the `--env-file` option to pass the environment variables to the container.

To do so, use the following command:
```sh
docker run \
  -p 9001:9001 \
  --env-file ./.env \
  --entrypoint "/usr/bin/supervisord -c /var/app/conf/activity_detector_supervisor.conf" \
  swfactivitydetector
```

You can replace ./.env with another custom path to your .env file.
