|
| 1 | +# Topcoder Challenge Resources Processor |
| 2 | + |
| 3 | +## Dependencies |
| 4 | + |
| 5 | +- nodejs https://nodejs.org/en/ (v10+) |
| 6 | +- Kafka |
| 7 | + |
| 8 | +## Configuration |
| 9 | + |
| 10 | +Configuration for the processor is at `config/default.js` and `config/production.js`. |
| 11 | +The following parameters can be set in config files or in env variables: |
| 12 | + |
| 13 | +- DISABLE_LOGGING: whether to disable logging, default is false |
| 14 | +- LOG_LEVEL: the log level, default value: 'debug' |
| 15 | +- AUTH0_URL: AUTH0 URL, used to get M2M token |
| 16 | +- AUTH0_AUDIENCE: AUTH0 audience, used to get M2M token, default value is 'https://www.topcoder-dev.com' |
| 17 | +- TOKEN_CACHE_TIME: AUTH0 token cache time, used to get M2M token |
| 18 | +- AUTH0_PROXY_SERVER_URL: Auth0 proxy server url, used to get TC M2M token |
| 19 | +- AUTH0_CLIENT_ID: AUTH0 client id, used to get M2M token |
| 20 | +- AUTH0_CLIENT_SECRET: AUTH0 client secret, used to get M2M token |
| 21 | +- KAFKA_URL: comma separated Kafka hosts, default value: 'localhost:9092' |
| 22 | +- KAFKA_GROUP_ID: the Kafka group id, default value: 'challenge-resources-processor' |
| 23 | +- KAFKA_CLIENT_CERT: Kafka connection certificate, optional, default value is undefined; |
| 24 | + if not provided, then SSL connection is not used, direct insecure connection is used; |
| 25 | + if provided, it can be either path to certificate file or certificate content |
| 26 | +- KAFKA_CLIENT_CERT_KEY: Kafka connection private key, optional, default value is undefined; |
| 27 | + if not provided, then SSL connection is not used, direct insecure connection is used; |
| 28 | + if provided, it can be either path to private key file or private key content |
| 29 | +- KAFKA_TOPIC: Kafka topic to listen, default value is 'challenge.notification.create' |
| 30 | +- REQUEST_TIMEOUT: superagent request timeout in milliseconds, default value is 20000 |
| 31 | +- RESOURCE_ROLE_ID: the challenge member resource role id |
| 32 | +- GET_PROJECT_API_BASE: get project API base URL, default value is mock API 'http://localhost:4000/v4/projects' |
| 33 | +- SEARCH_MEMBERS_API_BASE: search members API base URL, default value is 'https://api.topcoder.com/v3/members/_search' |
| 34 | +- CREATE_RESOURCE_API: create resource API URL, default value is mock API 'http://localhost:4000/v5/resources' |
| 35 | + |
| 36 | + |
| 37 | +Set the following environment variables so that the app can get TC M2M token (use 'set' insted of 'export' for Windows OS): |
| 38 | + |
| 39 | +- export AUTH0_CLIENT_ID=EkE9qU3Ey6hdJwOsF1X0duwskqcDuElW |
| 40 | +- export AUTH0_CLIENT_SECRET=Iq7REiEacFmepPh0UpKoOmc6u74WjuoJriLayeVnt311qeKNBvhRNBe9BZ8WABYk |
| 41 | +- export AUTH0_URL=https://topcoder-dev.auth0.com/oauth/token |
| 42 | +- export AUTH0_AUDIENCE=https://m2m.topcoder-dev.com/ |
| 43 | +- export TOKEN_CACHE_TIME=90 |
| 44 | + |
| 45 | +Also note that there is a `/health` endpoint that checks for the health of the app. This sets up an expressjs server and listens on the environment variable `PORT`. It's not part of the configuration file and needs to be passed as an environment variable |
| 46 | + |
| 47 | + |
| 48 | +Configuration for the tests is at `config/test.js`, only add such new configurations if different than `config/default.js`: |
| 49 | +- WAIT_TIME: wait time used in test, default is 2000 or 2 seconds |
| 50 | + |
| 51 | +## Local Kafka setup |
| 52 | + |
| 53 | +- `http://kafka.apache.org/quickstart` contains details to setup and manage Kafka server, |
| 54 | + below provides details to setup Kafka server in Mac, Windows will use bat commands in bin/windows instead |
| 55 | +- download kafka at `https://www.apache.org/dyn/closer.cgi?path=/kafka/1.1.0/kafka_2.11-1.1.0.tgz` |
| 56 | +- extract out the doanlowded tgz file |
| 57 | +- go to extracted directory kafka_2.11-0.11.0.1 |
| 58 | +- start ZooKeeper server: |
| 59 | + `bin/zookeeper-server-start.sh config/zookeeper.properties` |
| 60 | +- use another terminal, go to same directory, start the Kafka server: |
| 61 | + `bin/kafka-server-start.sh config/server.properties` |
| 62 | +- note that the zookeeper server is at localhost:2181, and Kafka server is at localhost:9092 |
| 63 | +- use another terminal, go to same directory, create topic: |
| 64 | + `bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic challenge.notification.create` |
| 65 | +- verify that the topics are created: |
| 66 | + `bin/kafka-topics.sh --list --zookeeper localhost:2181`, |
| 67 | + it should list out the created topic |
| 68 | +- run the producer and then write some message into the console to send to the `challenge.notification.create` topic: |
| 69 | + `bin/kafka-console-producer.sh --broker-list localhost:9092 --topic challenge.notification.create` |
| 70 | + in the console, write message, one message per line: |
| 71 | + `{ "topic": "challenge.notification.create", "originator": "challenge-api", "timestamp": "2019-02-16T00:00:00", "mime-type": "application/json", "payload": { "id": "5505f779-b28b-428f-888b-e523b443f3ea", "typeId": "7705f779-b28b-428f-888b-e523b443f3ea", "track": "code", "name": "test", "description": "desc", "timelineTemplateId": "8805f779-b28b-428f-888b-e523b443f3ea", "phases": [{ "id": "8805f779-b28b-428f-888b-e523b443f3eb", "name": "phase", "isActive": true, "duration": 1000 }], "prizeSets": [{ "type": "Challenge prizes", "prizes": [{ "type": "1st", "value": 600 }] }], "reviewType": "community", "tags": ["tag1"], "projectId": 30055214, "startDate": "2019-02-19T00:00:00", "status": "Draft", "created": "2019-02-16T00:00:00", "createdBy": "tester" } }` |
| 72 | +- optionally, use another terminal, go to same directory, start a consumer to view the messages: |
| 73 | + `bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic challenge.notification.create --from-beginning` |
| 74 | + |
| 75 | + |
| 76 | +## Local deployment |
| 77 | + |
| 78 | +- start mock-api, go to `mock-api` folder, run `npm i` and `npm start`, mock api is running at `http://localhost:4000` |
| 79 | +- go to project root folder, install dependencies `npm i` |
| 80 | +- run code lint check `npm run lint`, running `npm run lint:fix` can fix some lint errors |
| 81 | +- start processor app `npm start` |
| 82 | + |
| 83 | + |
| 84 | +## Unit Tests and E2E Tests |
| 85 | + |
| 86 | +Before running tests, setup and start kafka server, start the mock API, but do not start the processor app. |
| 87 | + |
| 88 | +- run `npm run test` to execute unit tests. |
| 89 | +- run `npm run test:cov` to execute unit tests and generate coverage report. |
| 90 | +- run `npm run e2e` to execute e2e tests. |
| 91 | +- run `npm run e2e:cov` to execute e2e tests and generate coverage report. |
| 92 | + |
| 93 | + |
| 94 | +## Verification |
| 95 | + |
| 96 | +- setup and start kafka server, start mock API, start processor app |
| 97 | +- start kafka-console-producer to write messages to `challenge.notification.create` topic: |
| 98 | + `bin/kafka-console-producer.sh --broker-list localhost:9092 --topic challenge.notification.create` |
| 99 | +- write message: |
| 100 | + `{ "topic": "challenge.notification.create", "originator": "challenge-api", "timestamp": "2019-02-16T00:00:00", "mime-type": "application/json", "payload": { "id": "5505f779-b28b-428f-888b-e523b443f3ea", "typeId": "7705f779-b28b-428f-888b-e523b443f3ea", "track": "code", "name": "test", "description": "desc", "timelineTemplateId": "8805f779-b28b-428f-888b-e523b443f3ea", "phases": [{ "id": "8805f779-b28b-428f-888b-e523b443f3eb", "name": "phase", "isActive": true, "duration": 1000 }], "prizeSets": [{ "type": "Challenge prizes", "prizes": [{ "type": "1st", "value": 600 }] }], "reviewType": "community", "tags": ["tag1"], "projectId": 30055214, "startDate": "2019-02-19T00:00:00", "status": "Draft", "created": "2019-02-16T00:00:00", "createdBy": "tester" } }` |
| 101 | + |
| 102 | +- you will see app logging: |
| 103 | +```bash |
| 104 | +info: Process message of challenge id 5505f779-b28b-428f-888b-e523b443f3ea and project id 30055214 |
| 105 | +info: Found member ids [40152933, 40141336] of project id 30055214 |
| 106 | +info: Created resource: { |
| 107 | + "challengeId": "5505f779-b28b-428f-888b-e523b443f3ea", |
| 108 | + "memberHandle": "lordofparadox", |
| 109 | + "roleId": "6605f779-b28b-428f-888b-e523b443f3ea", |
| 110 | + "id": "d4ac5715-b1ed-430b-86ca-56d022c97ce9", |
| 111 | + "memberId": "9e2ad182-d4f7-4c4d-9421-29bc8bd56dfb", |
| 112 | + "created": "2019-08-20T23:02:45.818Z", |
| 113 | + "createdBy": "mock-api" |
| 114 | +} |
| 115 | +info: Created resource: { |
| 116 | + "challengeId": "5505f779-b28b-428f-888b-e523b443f3ea", |
| 117 | + "memberHandle": "SethHafferkamp", |
| 118 | + "roleId": "6605f779-b28b-428f-888b-e523b443f3ea", |
| 119 | + "id": "8a835c7e-5a90-47f1-a020-c95c415e32db", |
| 120 | + "memberId": "980bd2a6-b4b6-4253-87e7-67cb931a76a2", |
| 121 | + "created": "2019-08-20T23:02:45.822Z", |
| 122 | + "createdBy": "mock-api" |
| 123 | +} |
| 124 | +info: Successfully processed message of challenge id 5505f779-b28b-428f-888b-e523b443f3ea and project id 30055214 |
| 125 | +``` |
| 126 | + |
| 127 | + |
| 128 | +- you may write invalid messages like: |
| 129 | + `{ "topic": "challenge.notification.create", "originator": "challenge-api", "timestamp": "2019-02-16T00:00:00", "mime-type": "application/json", "payload": { "id": "abc", "typeId": "7705f779-b28b-428f-888b-e523b443f3ea", "track": "code", "name": "test", "description": "desc", "timelineTemplateId": "8805f779-b28b-428f-888b-e523b443f3ea", "phases": [{ "id": "8805f779-b28b-428f-888b-e523b443f3eb", "name": "phase", "isActive": true, "duration": 1000 }], "prizeSets": [{ "type": "Challenge prizes", "prizes": [{ "type": "1st", "value": 600 }] }], "reviewType": "community", "tags": ["tag1"], "projectId": 30055214, "startDate": "2019-02-19T00:00:00", "status": "Draft", "created": "2019-02-16T00:00:00", "createdBy": "tester" } }` |
| 130 | + |
| 131 | + `{ "topic": "challenge.notification.create", "originator": "challenge-api", "timestamp": "2019-02-16T00:00:00", "mime-type": "application/json", "payload": { "id": "5505f779-b28b-428f-888b-e523b443f3ea", "typeId": "7705f779-b28b-428f-888b-e523b443f3ea", "track": "code", "name": "test", "description": "desc", "timelineTemplateId": "8805f779-b28b-428f-888b-e523b443f3ea", "phases": [{ "id": "8805f779-b28b-428f-888b-e523b443f3eb", "name": "phase", "isActive": true, "duration": 1000 }], "prizeSets": [{ "type": "Challenge prizes", "prizes": [{ "type": "1st", "value": 600 }] }], "reviewType": "community", "tags": ["tag1"], "projectId": 30055214, "startDate": "2019-02-19T00:00:00", "status": "Draft", "created": "abc", "createdBy": "tester" } }` |
| 132 | + |
| 133 | + `{ [ { abc` |
| 134 | +- then in the app console, you will see error messages |
| 135 | + |
| 136 | + |
| 137 | +- to test the health check API, start the processor, then browse `http://localhost:3000/health` in a browser, |
| 138 | + and you will see result `{"checksRun":1}`, you may change the health check API port by setting `PORT` environment variable |
| 139 | + |
0 commit comments