-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
249 additions
and
51 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
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,6 @@ | ||
const { JavaApiM2MClientFactory } = require("./JavaApiM2MClientFactory"); | ||
const appConfig = require("../config/app"); | ||
|
||
const client = JavaApiM2MClientFactory(appConfig.m2mOauth.clientId, appConfig.m2mOauth.clientSecret, appConfig.m2mOauth.url, appConfig.apiUrl) | ||
|
||
module.exports.JavaApiM2MClient = client |
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,51 @@ | ||
const {ClientCredentials} = require("simple-oauth2"); | ||
const { Axios } = require("axios"); | ||
const { AuthFailed } = require("./ApiErrors"); | ||
|
||
const getToken = async function(clientId, clientSecret, host, scope) { | ||
const tokenClient = new ClientCredentials({ | ||
client: { | ||
id: clientId, | ||
secret: clientSecret, | ||
|
||
}, | ||
auth: { | ||
tokenHost: host, | ||
tokenPath: '/oauth2/token', | ||
revokePath: '/oauth2/revoke' | ||
}, | ||
options: { | ||
authorizationMethod: 'body' | ||
} | ||
}) | ||
|
||
try { | ||
return tokenClient.getToken({ | ||
scope: scope ?? '', | ||
}) | ||
} catch (error) { | ||
throw new AuthFailed(error.toString()) | ||
} | ||
} | ||
|
||
module.exports.JavaApiM2MClientFactory = async function(clientId, clientSecret, host, javaApiBaseURL) { | ||
let passport = await getToken(clientId, clientSecret, host, '') | ||
|
||
const client = new Axios({ | ||
baseURL: javaApiBaseURL | ||
}) | ||
|
||
client.interceptors.request.use(config => { | ||
if (passport.expired()) { | ||
passport = passport.refresh() | ||
} | ||
|
||
config.headers = { | ||
Authorization: `Bearer ${passport.token.access_token}` | ||
} | ||
|
||
return config; | ||
}); | ||
|
||
return client | ||
} |
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
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 |
---|---|---|
@@ -1,27 +1,79 @@ | ||
const appConfig = require("../config/app") | ||
const WordpressServiceFactory = require("../lib/WordpressServiceFactory"); | ||
const Scheduler = require("../lib/Scheduler"); | ||
const LeaderboardService = require("../lib/LeaderboardService"); | ||
const cacheService = require("../lib/CacheService"); | ||
const LeaderboardRepository = require("../lib/LeaderboardRepository"); | ||
const {JavaApiM2MClient} = require('../lib/JavaApiM2MClient') | ||
|
||
const warmupWordpressCache = async () => { | ||
const wordpressService = WordpressServiceFactory(appConfig.wordpressUrl) | ||
|
||
const successHandler = (name) => { | ||
console.info(name, 'cache generated') | ||
} | ||
const errorHandler = (e, name) => { | ||
console.error(name, e.toString(), 'cache failed') | ||
const successHandler = (name) => { | ||
console.debug('Cache updated', {name}) | ||
} | ||
const errorHandler = (e, name) => { | ||
console.error(e.toString() , {name, entrypoint: 'cron-jobs.js'}) | ||
} | ||
|
||
const warmupWordpressCache = () => { | ||
try { | ||
const wordpressService = WordpressServiceFactory(appConfig.wordpressUrl) | ||
|
||
wordpressService.getNews(true) | ||
.then(() => successHandler('wordpressService::getNews')) | ||
.catch((e) => errorHandler(e, 'wordpressService::getNews')) | ||
|
||
wordpressService.getNewshub(true) | ||
.then(() => successHandler('wordpressService::getNewshub')) | ||
.catch((e) => errorHandler(e, 'wordpressService::getNewshub')) | ||
|
||
wordpressService.getContentCreators(true) | ||
.then(() => successHandler('wordpressService::getContentCreators')) | ||
.catch((e) => errorHandler(e, 'wordpressService::getContentCreators')) | ||
|
||
wordpressService.getTournamentNews(true) | ||
.then(() => successHandler('wordpressService::getTournamentNews')) | ||
.catch((e) => errorHandler(e, 'wordpressService::getTournamentNews')) | ||
|
||
wordpressService.getFafTeams(true) | ||
.then(() => successHandler('wordpressService::getFafTeams')) | ||
.catch((e) => errorHandler(e, 'wordpressService::getFafTeams')) | ||
} catch (e) { | ||
console.error('Error: cron-jobs::warmupWordpressCache failed with "' + e.toString() + '"', {entrypoint: 'cron-jobs.js'}) | ||
} | ||
} | ||
|
||
const warmupLeaderboard = async () => { | ||
try { | ||
const leaderboardService = new LeaderboardService(cacheService, new LeaderboardRepository(await JavaApiM2MClient)) | ||
|
||
wordpressService.getNews(true).then(() => successHandler('getNews')).catch((e) => errorHandler(e, 'getNews')) | ||
wordpressService.getNewshub(true).then(() => successHandler('getNewshub')).catch((e) => errorHandler(e, 'getNewshub')) | ||
wordpressService.getContentCreators(true).then(() => successHandler('getContentCreators')).catch((e) => errorHandler(e, 'getContentCreators')) | ||
wordpressService.getTournamentNews(true).then(() => successHandler('getTournamentNews')).catch((e) => errorHandler(e, 'getTournamentNews')) | ||
wordpressService.getFafTeams(true).then(() => successHandler('getFafTeams')).catch((e) => errorHandler(e, 'getFafTeams')) | ||
await leaderboardService.getLeaderboard(1, true) | ||
.then(() => successHandler('leaderboardService::getLeaderboard(global)')) | ||
.catch((e) => errorHandler(e, 'leaderboardService::getLeaderboard(global)')) | ||
|
||
await leaderboardService.getLeaderboard(2, true) | ||
.then(() => successHandler('leaderboardService::getLeaderboard(1v1)')) | ||
.catch((e) => errorHandler(e, 'leaderboardService::getLeaderboard(1v1)')) | ||
|
||
await leaderboardService.getLeaderboard(3, true) | ||
.then(() => successHandler('leaderboardService::getLeaderboard(2v2)')) | ||
.catch((e) => errorHandler(e, 'leaderboardService::getLeaderboard(2v2)')) | ||
|
||
await leaderboardService.getLeaderboard(4, true) | ||
.then(() => successHandler('leaderboardService::getLeaderboard(4v4)')) | ||
.catch((e) => errorHandler(e, 'leaderboardService::getLeaderboard(4v4)')) | ||
|
||
} catch (e) { | ||
console.error('Error: cron-jobs::warmupLeaderboard failed with "' + e.toString() + '"', {entrypoint: 'cron-jobs.js'}) | ||
} | ||
} | ||
|
||
module.exports = async () => { | ||
await warmupWordpressCache() | ||
warmupWordpressCache() | ||
warmupLeaderboard().then(() => {}) | ||
|
||
const wordpressScheduler = new Scheduler('createWordpressCaches', warmupWordpressCache, 60 * 59 * 1000) | ||
wordpressScheduler.start() | ||
wordpressScheduler.start() | ||
|
||
|
||
const leaderboardScheduler = new Scheduler('createLeaderboardCaches', warmupLeaderboard, 60 * 59 * 1000) | ||
leaderboardScheduler.start() | ||
} |
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
Oops, something went wrong.