Skip to content

Commit

Permalink
Avoid running interactions-api container if env vars are not present
Browse files Browse the repository at this point in the history
  • Loading branch information
WRadoslaw committed Mar 26, 2024
1 parent 567c202 commit ee8755d
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 16 deletions.
8 changes: 6 additions & 2 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ GQL_PORT=4350
AUTH_API_PORT=4074
# Recommendation service config
INTERACTIONS_API_PORT=4075
RECOMMENDATION_SERVICE_PRIVATE_KEY=
# Set private key and database name for recommendation service
# If you set only private key, Orion will support only local interactions service (for now at least)
RECOMMENDATION_SERVICE_PRIVATE_KEY=save-interactions-locally
# jsgenesis-dev
RECOMMENDATION_SERVICE_DATABASE=
# this will disable export block check for recommendation system - sync all data from the beginning
FORCE_RECOMMENDATION_DATA_SYNC=false
RECOMMENDATION_SERVICE_DATABASE=jsgenesis-dev


# Archive gateway url
ARCHIVE_GATEWAY_URL=${CUSTOM_ARCHIVE_GATEWAY_URL:-http://localhost:8888/graphql}
Expand Down
15 changes: 11 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,24 @@ typegen:
prepare: install codegen build setup-recommendations

up-squid:
@docker network create joystream_default || true
@docker-compose up -d
@set -a; \
source .env; \
set +a; \
docker network create joystream_default || true; \
if [ -z "$$RECOMMENDATION_SERVICE_PRIVATE_KEY" ] && [ -z "$$RECOMMENDATION_SERVICE_DATABASE"]; then \
docker-compose up -d; \
else \
docker-compose --profile interactions up -d; \
fi \

up-archive:
@docker network create joystream_default || true
@docker network create joystream_default || truee
@docker-compose -f archive/docker-compose.yml up -d

up: up-archive up-squid

down-squid:
@docker-compose down -v
@docker-compose down -v --remove-orphans

down-archive:
@docker-compose -f archive/docker-compose.yml down -v
Expand Down
8 changes: 5 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ services:
image: mongo:latest
container_name: interactions_db
hostname: interactions_db
profiles: ['interactions']
restart: unless-stopped
shm_size: 1g
command: ['mongod', '--port', '${INTERACTIONS_DB_PORT}']
Expand Down Expand Up @@ -94,9 +95,10 @@ services:
ports:
- '${AUTH_API_PORT}:${AUTH_API_PORT}'

orion_interations-api:
container_name: orion_interations-api
hostname: orion_interations-api
orion_interactions-api:
container_name: orion_interactions-api
hostname: orion_interactions-api
profiles: ['interactions']
image: node:18
restart: unless-stopped
env_file:
Expand Down
2 changes: 2 additions & 0 deletions src/interactions-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import swaggerUi, { JsonObject } from 'swagger-ui-express'
import YAML from 'js-yaml'
import fs from 'fs'
import { recommendationServiceManager } from '../utils/RecommendationServiceManager'
import { initInteractionsDb } from './interactionsEm'

recommendationServiceManager.enableSync()
recommendationServiceManager.initBatchLoop()
initInteractionsDb().then(() => undefined)

Check failure on line 19 in src/interactions-server/index.ts

View workflow job for this annotation

GitHub Actions / Local build, linting and formatting (ubuntu-latest, 16.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

export const logger = createLogger('interactions-api')

Expand Down
5 changes: 1 addition & 4 deletions src/interactions-server/interactionsEm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createLogger } from '@subsquid/logger'

const interactionsEmLogger = createLogger('interactionsEm')

async function initInteractionsDb() {
export async function initInteractionsDb() {
let instance: typeof mongoose | null = null
try {
interactionsEmLogger.info(`Initializing interactions database connection...`)
Expand All @@ -23,9 +23,6 @@ async function initInteractionsDb() {
return instance
}

// eslint-disable-next-line no-void
void initInteractionsDb()

const PurchaseModel = mongoose.model(
'Purchase',
new mongoose.Schema({
Expand Down
11 changes: 8 additions & 3 deletions src/utils/RecommendationServiceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class RecommendationServiceManager {
!process.env.RECOMMENDATION_SERVICE_DATABASE
) {
recommendationServiceLogger.error(
'RecommendationServiceManager initalized without required variables'
'RecommendationServiceManager initialized without required variables'
)
return
}
Expand Down Expand Up @@ -212,7 +212,7 @@ export class RecommendationServiceManager {
}

enableSync() {
this._enabled = true
this._enabled = !!this.client
}

private async sendBatchRequest(requests: ClientRequests.Request[]) {
Expand Down Expand Up @@ -348,11 +348,16 @@ export class RecommendationServiceManager {
}

initBatchLoop() {
if (!this.client) {
// Avoid initialization if client is not available
return
}
if (this._loopInitialized) {
throw new Error('update loop was initialized more than once')
}
recommendationServiceLogger.info('Initializing interactions update loop...')
this._loopInitialized = true
this._batchUpdateLoop(30 * 1_000) // 5mins
this._batchUpdateLoop(30 * 1_000) // 5 mins
.then(() => {
/* Do nothing */
})
Expand Down

0 comments on commit ee8755d

Please sign in to comment.