-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
f13e4f2
commit 6f45c9c
Showing
15 changed files
with
9,832 additions
and
6,358 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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,63 +1,79 @@ | ||
const http = require("http"); | ||
const mongoose = require("mongoose"); | ||
const { start, apiRoutes } = require("./app"); | ||
const config = require("./config/config"); | ||
const logger = require("./config/logger"); | ||
const socketio = require("./websocket/socketio"); | ||
const mqtt = require("./mqtt/mqtt"); | ||
|
||
let server; | ||
|
||
mongoose | ||
.connect(config.mongoose.url, config.mongoose.options) | ||
.then(async () => { | ||
try { | ||
logger.info("Connected to MongoDB"); | ||
|
||
// migration | ||
// await migrate(); | ||
|
||
app = await start(); | ||
const httpServer = http.createServer(app); | ||
|
||
var { message, io } = await socketio(httpServer); | ||
logger.info(message); | ||
|
||
var { message, client } = await mqtt(io); | ||
logger.info(message); | ||
|
||
apiRoutes(app, io, client); | ||
|
||
server = httpServer.listen(config.port, () => { | ||
logger.info(`Listening to port ${config.port}`); | ||
}); | ||
} catch (error) { | ||
logger.error(error.message); | ||
} | ||
}); | ||
|
||
const exitHandler = () => { | ||
if (server) { | ||
server.close(() => { | ||
logger.info("Server closed"); | ||
process.exit(1); | ||
}); | ||
} else { | ||
process.exit(1); | ||
} | ||
}; | ||
|
||
const unexpectedErrorHandler = (error) => { | ||
logger.error(error); | ||
exitHandler(); | ||
}; | ||
|
||
process.on("uncaughtException", unexpectedErrorHandler); | ||
process.on("unhandledRejection", unexpectedErrorHandler); | ||
|
||
process.on("SIGTERM", () => { | ||
logger.info("SIGTERM received"); | ||
if (server) { | ||
server.close(); | ||
} | ||
}); | ||
const http = require("http"); | ||
const mongoose = require("mongoose"); | ||
const { start, apiRoutes } = require("./app"); | ||
const config = require("./config/config"); | ||
const logger = require("./config/logger"); | ||
const socketio = require("./websocket/socketio"); | ||
const mqtt = require("./mqtt/mqtt"); | ||
const { agenda } = require("./jobs"); | ||
const { CronTime } = require("cron-time-generator"); | ||
|
||
let server; | ||
|
||
const startAgenda = async () => { | ||
await agenda.start(); | ||
|
||
/** For testing | ||
* every 5 minutes: cronTime.every(5).minutes() | ||
* every 5 seconds: cronTime.everyMinute() | ||
* every sunday at 00:00 : cronTime.everySundayAt(0, 0) | ||
* */ | ||
|
||
await agenda.every(CronTime.everyDayAt(16, 28), "retrieveDailyAqi"); | ||
// agenda.now("retrieveDailyAqi"); | ||
}; | ||
|
||
mongoose | ||
.connect(config.mongoose.url, config.mongoose.options) | ||
.then(async () => { | ||
try { | ||
logger.info("Connected to MongoDB"); | ||
|
||
// migration | ||
// await migrate(); | ||
|
||
app = await start(agenda); | ||
const httpServer = http.createServer(app); | ||
|
||
var { message, io } = await socketio(httpServer); | ||
logger.info(message); | ||
|
||
var { message, client } = await mqtt(io); | ||
logger.info(message); | ||
|
||
apiRoutes(app, io, client); | ||
await startAgenda(); | ||
|
||
server = httpServer.listen(config.port, () => { | ||
logger.info(`Listening to port ${config.port}`); | ||
}); | ||
} catch (error) { | ||
logger.error(error.message); | ||
} | ||
}); | ||
|
||
const exitHandler = () => { | ||
if (server) { | ||
server.close(() => { | ||
logger.info("Server closed"); | ||
process.exit(1); | ||
}); | ||
} else { | ||
process.exit(1); | ||
} | ||
}; | ||
|
||
const unexpectedErrorHandler = (error) => { | ||
logger.error(error); | ||
exitHandler(); | ||
}; | ||
|
||
process.on("uncaughtException", unexpectedErrorHandler); | ||
process.on("unhandledRejection", unexpectedErrorHandler); | ||
|
||
process.on("SIGTERM", () => { | ||
logger.info("SIGTERM received"); | ||
if (server) { | ||
server.close(); | ||
} | ||
}); |
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,19 @@ | ||
const { retrieveDailyAqi } = require("./retrieveDailyAqi"); | ||
const logger = require("../../config/logger"); | ||
|
||
let definitions = [retrieveDailyAqi]; | ||
|
||
const loadDefinitions = async (agenda) => { | ||
try { | ||
console.log() | ||
for (let definition of definitions) { | ||
await definition(agenda); | ||
} | ||
} catch (error) { | ||
logger.error("Error when load agenda job definitions : " + error); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
loadDefinitions, | ||
}; |
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,53 @@ | ||
const { aqiApiToken, predictionServiceUrl } = require("../../config/config"); | ||
const axios = require("axios"); | ||
const logger = require("../../config/logger"); | ||
|
||
const AQI_URL = "http://api.waqi.info"; | ||
|
||
const retrieveDailyAqi = async (agenda) => { | ||
try { | ||
// define(jobName, fn, [options]) | ||
agenda.define( | ||
"retrieveDailyAqi", | ||
async function (job, done) { | ||
try { | ||
var response = await axios({ | ||
method: "get", | ||
url: `${AQI_URL}/feed/@1583/?token=${aqiApiToken}`, | ||
}); | ||
|
||
response = response.data; | ||
|
||
logger.info( | ||
`New AQI retrieved for date ${response.data.time.iso} from ${AQI_URL}: ${response.data.aqi}` | ||
); | ||
|
||
var requestBody = { | ||
newAqi: response.data.aqi, | ||
date: response.data.time.iso.substring(0, 10), | ||
}; | ||
|
||
console.log(requestBody); | ||
|
||
await axios({ | ||
method: "post", | ||
url: `${predictionServiceUrl}/new/aqi`, | ||
data: requestBody, | ||
}); | ||
|
||
done(); | ||
} catch (error) { | ||
logger.error( | ||
"Failed to update new daily aqi in job [agenda.retrieveDailyAqi] : " + | ||
error.message | ||
); | ||
} | ||
}, | ||
{ priority: "highest", concurrency: 20 } | ||
); | ||
} catch (error) { | ||
logger.error("Error in job [agenda.retrieveDailyAqi] : " + error); | ||
} | ||
}; | ||
|
||
module.exports = { retrieveDailyAqi }; |
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,36 @@ | ||
const config = require("../config/config"); | ||
const { Agenda } = require("@hokify/agenda"); | ||
const logger = require("../config/logger"); | ||
const { loadDefinitions } = require("./definitions/index.js"); | ||
|
||
var mongoConnectionString = config.mongoose.url; | ||
const processEvery = "60 seconds"; | ||
|
||
/** | ||
* If having trouble with "Agenda is not a constructor". | ||
* Follow this instruction: https://github.com/agenda/agenda/issues/1293 | ||
*/ | ||
var agenda = new Agenda({ | ||
db: { | ||
address: mongoConnectionString, | ||
options: { useNewUrlParser: true }, | ||
collection: "jobs", | ||
}, | ||
useUnifiedTopology: true, | ||
}); | ||
|
||
// Specifies the frequency at which agenda will query the database looking for jobs that need to be processed | ||
agenda.processEvery(processEvery); | ||
|
||
// listen for the ready or error event. | ||
agenda | ||
.on("ready", () => logger.info("Agenda started!")) | ||
.on("error", () => logger.error("Agenda connection error!")); | ||
|
||
// Define agenda definitions | ||
loadDefinitions(agenda); | ||
|
||
// Logs all registered jobs | ||
console.log({ jobs: agenda.definitions }); | ||
|
||
module.exports = { agenda }; |
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.