-
Notifications
You must be signed in to change notification settings - Fork 37
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
wordpress service + event-emitters #487
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the categories url was configurable via env?
But I see so many categories now, I'm no longer sure.
Maybe we should at least put the ids as const variables at the top of the file for better naming in the actual urls?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was just copy&pasta, https://github.com/FAForever/website/pull/487/files#diff-7e4783341040a4ba6619cd0157ccb8d361c450d4ac84148a2fe589414db924ddL22
maybe we can optimize it later? there are for sure other problems, like "options/fields" not used...