This processor listens to Kafka events for Marathon Matches (MM) and updates user ratings based on challenge results. This version has been refactored to use a modern technology stack for better maintainability and scalability.
- Node.js (v18 or newer)
- TypeScript
- PostgreSQL
- Prisma
- Docker & Docker Compose
Configuration is managed via environment variables. This project uses two separate files for clarity:
.env
: For running local commands (likeprisma migrate
).docker.env
: For running the application inside Docker Compose.
DATABASE_URL
: (Required) The connection string for your PostgreSQL database.M2M_TOKEN
: (Required) A valid M2M bearer token for authenticating with the Topcoder API.SUBMISSION_API_URL
: The base URL for the Topcoder Submissions API.KAFKA_URL
: Comma-separated list of Kafka broker URLs.
This step creates the necessary migration files. You only need to do this once.
-
Start Database Service: From the project root, run
docker compose up -d postgres
This starts just the database container in the background, making it available on
localhost:5432
. -
Create
.env
file for local commands:- Copy
.env.example
to a new file named.env
. - The default
DATABASE_URL
(postgresql://user:password@localhost:5432/ratings_db
) is correct for this step.
- Copy
-
Create Migrations: Run the following command locally. This will connect to the running database container and create the initial migration files in a new
prisma/migrations
directory.npx prisma migrate dev --name init
-
Stop the Database: Run
docker compose down
to stop the standalone database container.
-
Create
docker.env
file for the application:- Copy
docker.env.example
to a new file nameddocker.env
. - Open
docker.env
and paste the provided access token into theM2M_TOKEN
variable.
- Copy
-
Build and Run All Services: From the project root, run:
docker compose up --build -d
The
kafka-setup
service will automatically create the necessary Kafka topics before the main application starts. -
Seed the Database: After the containers are running, execute this command to populate the database with test data.
docker compose exec legacy-rating-processor npx prisma db seed
For cases where a valid M2M_TOKEN
is unavailable or expired, you can run the processor against a mock Submission API.
In a separate terminal, start the mock API server from the project's root directory.
# Ensure node dependencies are installed
npm install
# Run the mock server
node mock/mock-api.js
# The mock server will be listening on http://localhost:3001
Edit your docker.env
file and set the SUBMISSION_API_URL
to point to the mock server.
SUBMISSION_API_URL=http://host.docker.internal:3001
Note: host.docker.internal
is a special DNS name that resolves to the host machine's IP address from within a Docker container. This allows the processor running in Docker to connect to the mock API server running on your local machine.
After modifying the docker.env
file, restart the processor to apply the changes.
docker compose up -d --force-recreate legacy-rating-processor
The processor will now use the mock API for its requests.
If you see a connect ECONNREFUSED
error in the logs when the processor tries to connect to host.docker.internal
, it may be because you are running on Linux where this feature can be inconsistent.
To fix this, you need to explicitly tell the Docker container how to find the host. Edit your docker-compose.yml
and add an extra_hosts
entry to the legacy-rating-processor
service:
# In your docker-compose.yml
services:
legacy-rating-processor:
# ... other service configuration
extra_hosts:
- "host.docker.internal:host-gateway"
# ... other service configuration
The host-gateway
value is a special string that resolves to the host's IP address inside the container. After adding this, restart the service with the command from Step 3.
Your application is now fully running. To verify that it's working correctly:
- View Logs:
docker compose logs -f legacy-rating-processor
- Test Functionality: Follow the steps in
Validation.md
.