-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EventEmitter, init WordpressService, cleanup members/*.json
- Loading branch information
Showing
29 changed files
with
385 additions
and
268 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ module.exports = { | |
'node_modules/**', | ||
'grunt/**', | ||
'Gruntfile.js', | ||
'public/js/app/members/**' | ||
] | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const config = { | ||
setupFilesAfterEnv: ['./tests/setup.js'], | ||
}; | ||
|
||
module.exports = config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const NodeCache = require("node-cache"); | ||
const cacheService = new NodeCache( | ||
{ | ||
stdTTL: 300, // use 5 min for all caches if not changed with ttl | ||
checkperiod: 600 // cleanup memory every 10 min | ||
} | ||
) | ||
|
||
module.exports = cacheService |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const {EventEmitter} = require("events") | ||
|
||
module.exports = class Scheduler extends EventEmitter { | ||
constructor(eventName, action, ms) { | ||
super() | ||
this.eventName = eventName | ||
this.action = action | ||
this.handle = undefined | ||
this.interval = ms | ||
this.addListener(this.eventName, this.action); | ||
} | ||
|
||
start() { | ||
if (!this.handle) { | ||
this.handle = setInterval(() => this.emit(this.eventName), this.interval); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
const {convert} = require("url-slug"); | ||
|
||
class WordpressRepository { | ||
constructor(wordpressClient) { | ||
this.wordpressClient = wordpressClient | ||
} | ||
async fetchNews() { | ||
let response = await this.wordpressClient.get('/wp-json/wp/v2/posts/?per_page=100&_embed&_fields=_links.author,_links.wp:featuredmedia,_embedded,title,content.rendered,date,categories&categories=587') | ||
|
||
if (response.status !== 200) { | ||
throw new Error('WordpressRepository::fetchNews failed with response status "' + response.status + '"') | ||
} | ||
|
||
const rawNewsData = JSON.parse(response.data) | ||
|
||
if (typeof rawNewsData !== 'object' || rawNewsData === null) { | ||
throw new Error('WordpressRepository::mapNewsResponse malformed response, not an object') | ||
} | ||
|
||
return rawNewsData.map(item => ({ | ||
slug: convert(item.title.rendered), | ||
bcSlug: item.title.rendered.replace(/ /g, '-'), | ||
date: item.date, | ||
title: item.title.rendered, | ||
content: item.content.rendered, | ||
author: item._embedded.author[0].name, | ||
media: item._embedded['wp:featuredmedia'][0].source_url, | ||
})) | ||
} | ||
|
||
async fetchTournamentNews() { | ||
let response = await this.wordpressClient.get('/wp-json/wp/v2/posts/?per_page=10&_embed&_fields=content.rendered,categories&categories=638') | ||
|
||
if (response.status !== 200) { | ||
throw new Error('WordpressRepository::fetchTournamentNews failed with response status "' + response.status + '"') | ||
} | ||
|
||
let dataObjectToArray = JSON.parse(response.data); | ||
|
||
let sortedData = dataObjectToArray.map(item => ({ | ||
content: item.content.rendered, | ||
category: item.categories | ||
})); | ||
|
||
return sortedData.filter(article => article.category[1] !== 284) | ||
} | ||
|
||
async fetchContentCreators() { | ||
const response = await this.wordpressClient.get('/wp-json/wp/v2/posts/?per_page=100&_embed&_fields=content.rendered,categories&categories=639') | ||
|
||
if (response.status !== 200) { | ||
throw new Error('WordpressRepository::fetchContentCreators failed with response status "' + response.status + '"') | ||
} | ||
|
||
const items = JSON.parse(response.data); | ||
|
||
return items.map(item => ({ | ||
content: item.content.rendered, | ||
})) | ||
} | ||
|
||
async fetchFafTeams() { | ||
const response = await this.wordpressClient.get('/wp-json/wp/v2/posts/?per_page=100&_embed&_fields=content.rendered,categories&categories=636') | ||
|
||
if (response.status !== 200) { | ||
throw new Error('WordpressRepository::fetchFafTeams failed with response status "' + response.status + '"') | ||
} | ||
|
||
const items = JSON.parse(response.data); | ||
|
||
return items.map(item => ({ | ||
content: item.content.rendered, | ||
})) | ||
} | ||
|
||
async fetchNewshub() { | ||
const response = await this.wordpressClient.get('/wp-json/wp/v2/posts/?per_page=10&_embed&_fields=_links.author,_links.wp:featuredmedia,_embedded,title,newshub_externalLinkUrl,newshub_sortIndex,content.rendered,date,categories&categories=283') | ||
|
||
if (response.status !== 200) { | ||
throw new Error('WordpressRepository::fetchNewshub failed with response status "' + response.status + '"') | ||
} | ||
|
||
const items = JSON.parse(response.data); | ||
const sortedData = items.map(item => ({ | ||
category: item.categories, | ||
sortIndex: item.newshub_sortIndex, | ||
link: item.newshub_externalLinkUrl, | ||
date: item.date, | ||
title: item.title.rendered, | ||
content: item.content.rendered, | ||
author: item._embedded.author[0].name, | ||
media: item._embedded['wp:featuredmedia'][0].source_url, | ||
})); | ||
|
||
sortedData.sort((articleA, articleB) => articleB.sortIndex - articleA.sortIndex); | ||
|
||
return sortedData.filter((article) => { | ||
return article.category[1] !== 284; | ||
}) | ||
} | ||
} | ||
|
||
module.exports = WordpressRepository |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
const {MutexService} = require("./MutexService"); | ||
const wordpressTTL = 60 * 60 | ||
|
||
class WordpressService { | ||
constructor(cacheService, wordpressRepository, lockTimeout = 3000) { | ||
this.lockTimeout = lockTimeout | ||
this.cacheService = cacheService | ||
this.mutexServices = { | ||
news: new MutexService(), | ||
tournament: new MutexService(), | ||
creators: new MutexService(), | ||
teams: new MutexService(), | ||
newshub: new MutexService(), | ||
} | ||
this.wordpressRepository = wordpressRepository | ||
} | ||
|
||
getCacheKey(name) { | ||
return 'WordpressService_' + name | ||
} | ||
|
||
async getNews(ignoreCache = false) { | ||
const cacheKey = this.getCacheKey('news') | ||
|
||
if (this.cacheService.has(cacheKey) && ignoreCache === false) { | ||
return this.cacheService.get(cacheKey) | ||
} | ||
|
||
if (this.mutexServices.news.locked) { | ||
await this.mutexServices.news.acquire(() => { | ||
}, this.lockTimeout) | ||
return this.getNews() | ||
} | ||
|
||
await this.mutexServices.news.acquire(async () => { | ||
const result = await this.wordpressRepository.fetchNews() | ||
this.cacheService.set(cacheKey, result, wordpressTTL); | ||
}) | ||
|
||
return this.getNews() | ||
} | ||
|
||
async getTournamentNews(ignoreCache = false) { | ||
const cacheKey = this.getCacheKey('tournament-news') | ||
|
||
if (this.cacheService.has(cacheKey) && ignoreCache === false) { | ||
return this.cacheService.get(cacheKey) | ||
} | ||
|
||
if (this.mutexServices.tournament.locked) { | ||
await this.mutexService.acquire(() => { | ||
}, this.lockTimeout) | ||
return this.getTournamentNews() | ||
} | ||
|
||
await this.mutexServices.tournament.acquire(async () => { | ||
const result = await this.wordpressRepository.fetchTournamentNews() | ||
this.cacheService.set(cacheKey, result, wordpressTTL); | ||
}) | ||
|
||
return this.getTournamentNews() | ||
} | ||
|
||
async getContentCreators(ignoreCache = false) { | ||
const cacheKey = this.getCacheKey('content-creators') | ||
|
||
if (this.cacheService.has(cacheKey) && ignoreCache === false) { | ||
return this.cacheService.get(cacheKey) | ||
} | ||
|
||
if (this.mutexServices.creators.locked) { | ||
await this.mutexServices.creators.acquire(() => { | ||
}, this.lockTimeout) | ||
return this.getContentCreators() | ||
} | ||
|
||
await this.mutexServices.creators.acquire(async () => { | ||
const result = await this.wordpressRepository.fetchContentCreators() | ||
this.cacheService.set(cacheKey, result, wordpressTTL); | ||
}) | ||
|
||
return this.getContentCreators() | ||
} | ||
|
||
async getFafTeams(ignoreCache = false) { | ||
const cacheKey = this.getCacheKey('faf-teams') | ||
|
||
if (this.cacheService.has(cacheKey) && ignoreCache === false) { | ||
return this.cacheService.get(cacheKey) | ||
} | ||
|
||
if (this.mutexServices.teams.locked) { | ||
await this.mutexService.acquire(() => { | ||
}, this.lockTimeout) | ||
return this.getFafTeams() | ||
} | ||
|
||
await this.mutexServices.teams.acquire(async () => { | ||
const result = await this.wordpressRepository.fetchFafTeams() | ||
this.cacheService.set(cacheKey, result, wordpressTTL); | ||
}) | ||
|
||
return this.getFafTeams() | ||
} | ||
|
||
async getNewshub(ignoreCache = false) { | ||
const cacheKey = this.getCacheKey('newshub') | ||
|
||
if (this.cacheService.has(cacheKey) && ignoreCache === false) { | ||
return this.cacheService.get(cacheKey) | ||
} | ||
|
||
if (this.mutexServices.newshub.locked) { | ||
await this.mutexService.acquire(() => { | ||
}, this.lockTimeout) | ||
return this.getNewshub() | ||
} | ||
|
||
await this.mutexServices.newshub.acquire(async () => { | ||
const result = await this.wordpressRepository.fetchNewshub() | ||
this.cacheService.set(cacheKey, result, wordpressTTL); | ||
}) | ||
|
||
return this.getNewshub() | ||
} | ||
} | ||
|
||
module.exports = WordpressService |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const WordpressService = require("./WordpressService") | ||
const WordpressRepository = require("./WordpressRepository") | ||
const {Axios} = require("axios") | ||
const cacheService = require('./CacheService') | ||
|
||
module.exports = (wordpressBaseURL) => { | ||
const config = { | ||
baseURL: wordpressBaseURL | ||
}; | ||
const wordpressClient = new Axios(config) | ||
|
||
return new WordpressService(cacheService, new WordpressRepository(wordpressClient)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.