Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing workers - Part one #14

Open
wants to merge 50 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
748d5b4
Added redis port and script
Oct 28, 2020
316b6f6
Redis env var
Oct 28, 2020
2a66243
Adder trigger endpoint
Oct 28, 2020
d768508
Modified models
Oct 28, 2020
9051a6f
Redis config, added workers and trigger script
Oct 28, 2020
2f4139c
Modified github service test cases
Oct 28, 2020
3155ece
User.login is unique
Oct 29, 2020
7133f24
Added parameter validation and default value
Oct 29, 2020
3fcc8b8
Repository worker wont fail if user already exists
Oct 29, 2020
e3b21d7
Default value, rmeoved Joi validation
Oct 29, 2020
9ff4e73
Default value fix
Oct 29, 2020
d7f642a
Minor modifications
Oct 29, 2020
ae3ea68
Added quit after message is sent
Oct 29, 2020
c296d48
Removing logs
Oct 29, 2020
7fa0908
trigger script correction
Oct 29, 2020
b6ffdb0
Modified graphql query
Nov 2, 2020
f03acb0
Redis channel names correction
Nov 2, 2020
853c0a3
Contribution handler
Nov 2, 2020
4973f83
Modified test query
Nov 2, 2020
cc85390
Refactored contribution handler
Nov 2, 2020
61929ad
Changed linter script command
Nov 3, 2020
20ab9ef
Refactored onRepository handler
Nov 3, 2020
a63271b
Added TRIGGER_QUERY
Nov 3, 2020
e6930db
Reafctored repository message listener
Nov 3, 2020
9fb7a99
Added value assertions
Nov 3, 2020
c52fc35
Added redis config env vars
Nov 3, 2020
96b4602
Swapped host and port
Nov 3, 2020
e962d38
Trigger no checks if channels are subscribed
Nov 4, 2020
c47b035
Added process exit on error
Nov 4, 2020
e9aed9f
new line
Nov 4, 2020
94d3f9c
Fixed versioning
Nov 4, 2020
ec8b871
removed ()
Nov 4, 2020
e38eed1
Removed comments
Nov 4, 2020
bc43a77
Contribution refactor
Nov 5, 2020
3db079c
Removed onTrigger, subscription happens here
Nov 5, 2020
f37e4bd
Added on subscribe listener, trigger client
Nov 5, 2020
abcf6f1
Removed error handling, client creation, publish
Nov 5, 2020
ec479db
Renamed variable
Nov 5, 2020
419a4a6
Added required to schemas
Nov 5, 2020
9973896
onContributon wait until all promises are resolved
Nov 5, 2020
192b02c
Created test file
Nov 5, 2020
0eeb78c
Added stub and fixed typo
Nov 5, 2020
a9cc659
Changed function import
Nov 6, 2020
219c5a5
Added test cases
Nov 6, 2020
84b5d57
Changed mock to be dynamic
Nov 6, 2020
313c9dc
Added repositoryCount to schema
Nov 9, 2020
83bda9e
Added repositoryCount to mock message
Nov 9, 2020
9287050
For each collaboratorsa message is sent
Nov 10, 2020
791836e
Removed stringifies from logger functions
Nov 10, 2020
7e65716
Changed subscribe event handler
Nov 10, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ DBUSER=
DBPASSWORD=
DB=
LOG_LEVEL=
REDIS_PORT=
1 change: 1 addition & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const config = {
dbUser: process.env.NODE_ENV === 'test' ? 'postgres' : process.env.DBUSER,
dbPassword: process.env.NODE_ENV === 'test' ? 'mysecretpassword' : process.env.DBPASSWORD,
db: process.env.NODE_ENV === 'test' ? 'test' : process.env.DB,
redisPort: process.env.REDIS_PORT,
logger: {
level: process.env.LOG_LEVEL,
},
Expand Down
2 changes: 1 addition & 1 deletion db/migrations/1-db-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ async function up(knex) {
try {
await knex.schema.createTable(userTable, (table) => {
table.increments('id').primary();
table.string('login', 255).notNullable();
table.string('login', 255).notNullable().unique();
table.string('avatar_url', 255);
table.string('html_url', 255);
});
Expand Down
6 changes: 3 additions & 3 deletions db/models/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ const schema = Joi.object({
owner: Joi.number().integer().required(),
full_name: Joi.string().required(),
stargazers_count: Joi.number().integer().required(),
html_url: Joi.string().uri().required(),
description: Joi.string(),
html_url: Joi.string().allow(''),
description: Joi.string().allow(''),
language: Joi.string(),
});

const insert = data => db('repository').insert(data);
const insert = data => db('repository').insert(data).returning('*');

const read = params => db('repository').where(params).select();

Expand Down
2 changes: 1 addition & 1 deletion db/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const schema = Joi.object({
html_url: Joi.string().uri(),
});

const insert = data => db('user').insert(data);
const insert = data => db('user').insert(data).returning('*');

const read = param => db('user').where(param).select();

Expand Down
66 changes: 59 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"migrate-up": "knex migrate:up",
"migrate-down": "knex migrate:down",
"seed-db": "knex seed:run",
"seed-test-db": "NODE_ENV=test knex seed:run"
"seed-test-db": "NODE_ENV=test knex seed:run",
"trigger": "node ./scripts/trigger.js"
},
"husky": {
"hooks": {
Expand Down Expand Up @@ -45,7 +46,8 @@
"lodash": "4.17.20",
"pg": "8.3.3",
"pino": "6.7.0",
"pino-pretty": "4.3.0"
"pino-pretty": "4.3.0",
"redis": "^3.0.2"
rolandKoczan marked this conversation as resolved.
Show resolved Hide resolved
},
"devDependencies": {
"chai": "4.2.0",
Expand Down
5 changes: 5 additions & 0 deletions router.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ const repositorySchema = Joi.object({

router.get('/hello', (req, res) => res.send('Hello World !'));

// // Trigger
// router.post('/trigger', (req, res) => {
// // Redis
// });

// Repository
router.get('/repository/:id', async (req, res, next) => {
const { id } = req.params;
Expand Down
13 changes: 13 additions & 0 deletions scripts/trigger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const logger = require('../logger');
const { initChannels } = require('../worker/index');
const { onTrigger } = require('../worker/handlers/trigger');

logger.info('Initiating trigger script');

if (!process.env.TRIGGER_QUERY) throw Error('Missing TRIGGER_QUERY env variable!');

rolandKoczan marked this conversation as resolved.
Show resolved Hide resolved
logger.info('Initiating channels');
initChannels();
rolandKoczan marked this conversation as resolved.
Show resolved Hide resolved

logger.info('Sending trigger message');
onTrigger(process.env.TRIGGER_QUERY);
rolandKoczan marked this conversation as resolved.
Show resolved Hide resolved
28 changes: 21 additions & 7 deletions services/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,43 @@ const graphQLClient = new GraphQLClient(endpoint, {
},
});

exports.searchRepositories = async (queryString) => {
if (!queryString) {
exports.searchRepositories = async (params) => {
const { queryString, first = 5 } = params;

if (!params.queryString) {
throw Error('queryString is a mandatory parameter');
}

const query = `query search($queryString:String!){
search(query: $queryString, type: REPOSITORY, first: 10) {
const query = `query search($queryString:String!, $first:Int){
search(query: $queryString, type: REPOSITORY, first: $first) {
repositoryCount
edges {
node {
... on RepositoryInfo {
... on Repository {
name
description
homepageUrl
stargazerCount
languages(first: 1) {
edges {
node {
name
}
}
}
createdAt
owner {
id
login
avatarUrl
url
}
}
}
}}
}`;

const varibale = { queryString };
const varibale = { queryString, first };

const response = await graphQLClient.request(query, varibale);
return response;
Expand All @@ -47,7 +61,7 @@ exports.searchRepositories = async (queryString) => {
exports.getContributors = async (owner, repoName) => {
const query = `query collaboratorsQuery($owner:String!,$repoName:String!){
repository(owner: $owner, name: $repoName) {
collaborators(first:100, affiliation: DIRECT) {
collaborators(first:10) {
edges {
node {
id
Expand Down
Loading