2022-03-24 06:43:41 -07:00
|
|
|
/*
|
|
|
|
|
This Controller library is concerned with timer-based functions that are
|
2025-05-18 15:36:32 -07:00
|
|
|
kicked off periodically.
|
2022-03-24 06:43:41 -07:00
|
|
|
*/
|
|
|
|
|
|
2023-02-25 06:09:56 -08:00
|
|
|
import config from '../../config/index.js'
|
2022-03-24 06:43:41 -07:00
|
|
|
|
|
|
|
|
class TimerControllers {
|
|
|
|
|
constructor (localConfig = {}) {
|
|
|
|
|
// Dependency Injection.
|
|
|
|
|
this.adapters = localConfig.adapters
|
|
|
|
|
if (!this.adapters) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
'Instance of Adapters library required when instantiating Timer Controller libraries.'
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
this.useCases = localConfig.useCases
|
|
|
|
|
if (!this.useCases) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
'Instance of Use Cases library required when instantiating Timer Controller libraries.'
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.debugLevel = localConfig.debugLevel
|
2023-02-25 06:09:56 -08:00
|
|
|
this.config = config
|
2022-03-24 06:43:41 -07:00
|
|
|
|
2024-11-22 10:54:21 -08:00
|
|
|
// Bind 'this' object to all subfunctions
|
|
|
|
|
this.startTimers = this.startTimers.bind(this)
|
|
|
|
|
this.gcOrders = this.gcOrders.bind(this)
|
|
|
|
|
this.gcOffers = this.gcOffers.bind(this)
|
|
|
|
|
this.checkDupOffers = this.checkDupOffers.bind(this)
|
2024-12-02 22:19:54 -04:00
|
|
|
this.loadOffers = this.loadOffers.bind(this)
|
2023-02-25 06:09:56 -08:00
|
|
|
// Bind 'this' object to all subfunctions.
|
2025-04-10 17:57:44 -07:00
|
|
|
// this.exampleTimerFunc = this.exampleTimerFunc.bind(this)
|
2024-12-07 21:25:10 -08:00
|
|
|
this.cleanUsage = this.cleanUsage.bind(this)
|
2024-12-16 05:29:03 -08:00
|
|
|
|
2024-11-22 10:54:21 -08:00
|
|
|
// State
|
|
|
|
|
this.gcOrdersInt = null
|
|
|
|
|
this.gcOffersInt = null
|
|
|
|
|
this.checkDupOffersInt = null
|
2024-12-02 22:19:54 -04:00
|
|
|
this.loadOffersInt = null
|
2022-03-24 06:43:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start all the time-based controllers.
|
|
|
|
|
startTimers () {
|
2024-11-22 10:54:21 -08:00
|
|
|
this.gcOrdersInt = setInterval(this.gcOrders, 60000 * 5)
|
|
|
|
|
this.gcOffersInt = setInterval(this.gcOffers, 60000 * 5)
|
2024-12-28 13:58:59 -08:00
|
|
|
// this.checkDupOffersInt = setInterval(this.checkDupOffers, 60000 * 4.5)
|
2024-12-02 22:19:54 -04:00
|
|
|
this.loadOffersInt = setInterval(this.loadOffers, 60000 * 2)
|
2023-02-25 06:09:56 -08:00
|
|
|
// Any new timer control functions can be added here. They will be started
|
|
|
|
|
// when the server starts.
|
2025-04-10 17:57:44 -07:00
|
|
|
// this.optimizeWalletHandle = setInterval(this.exampleTimerFunc, 60000 * 60)
|
2024-12-07 21:27:41 -08:00
|
|
|
this.cleanUsageHandle = setInterval(this.cleanUsage, 60000 * 60) // 1 hour
|
2023-02-25 06:09:56 -08:00
|
|
|
|
2024-12-02 22:19:54 -04:00
|
|
|
return true
|
2023-02-25 06:09:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stopTimers () {
|
2024-11-22 10:54:21 -08:00
|
|
|
clearInterval(this.gcOrdersInt)
|
|
|
|
|
clearInterval(this.gcOffersInt)
|
2024-12-28 13:58:59 -08:00
|
|
|
// clearInterval(this.checkDupOffers)
|
2023-02-25 06:09:56 -08:00
|
|
|
clearInterval(this.optimizeWalletHandle)
|
2025-05-18 15:36:32 -07:00
|
|
|
clearInterval(this.cleanUsageHandle)
|
2022-03-24 06:43:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Garbage Collect the Orders.
|
|
|
|
|
gcOrders () {
|
|
|
|
|
try {
|
2024-11-22 10:54:21 -08:00
|
|
|
this.useCases.order.removeStaleOrders()
|
2024-12-02 22:19:54 -04:00
|
|
|
return true
|
2022-03-24 06:43:41 -07:00
|
|
|
} catch (err) {
|
|
|
|
|
// Do not throw an error. This is a top-level function.
|
|
|
|
|
console.log('Error in timer-controllers.js/gcOrders(): ', err)
|
2024-12-02 22:19:54 -04:00
|
|
|
return false
|
2022-03-24 06:43:41 -07:00
|
|
|
}
|
|
|
|
|
}
|
2022-03-24 06:57:39 -07:00
|
|
|
|
|
|
|
|
// Garbage Collect the Offers.
|
|
|
|
|
gcOffers () {
|
|
|
|
|
try {
|
2024-11-22 10:54:21 -08:00
|
|
|
this.useCases.offer.removeStaleOffers()
|
2024-12-02 22:19:54 -04:00
|
|
|
return true
|
2022-03-24 06:57:39 -07:00
|
|
|
} catch (err) {
|
|
|
|
|
// Do not throw an error. This is a top-level function.
|
|
|
|
|
console.log('Error in timer-controllers.js/gcOffers(): ', err)
|
2024-12-02 22:19:54 -04:00
|
|
|
return false
|
2022-03-24 06:57:39 -07:00
|
|
|
}
|
|
|
|
|
}
|
2022-10-02 07:18:17 -07:00
|
|
|
|
|
|
|
|
// Remove duplicate Offers
|
|
|
|
|
checkDupOffers () {
|
|
|
|
|
try {
|
2024-11-22 10:54:21 -08:00
|
|
|
this.useCases.offer.removeDuplicateOffers()
|
2024-12-02 22:19:54 -04:00
|
|
|
return true
|
2022-10-02 07:18:17 -07:00
|
|
|
} catch (err) {
|
|
|
|
|
// Do not throw an error. This is a top-level function.
|
|
|
|
|
console.log('Error in timer-controllers.js/checkDupOffers(): ', err)
|
2024-12-02 22:19:54 -04:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load offers From nostr .
|
|
|
|
|
async loadOffers () {
|
|
|
|
|
try {
|
|
|
|
|
await this.useCases.offer.loadOffers()
|
|
|
|
|
return true
|
|
|
|
|
} catch (err) {
|
|
|
|
|
// Do not throw an error. This is a top-level function.
|
|
|
|
|
console.log('Error in timer-controllers.js/loadOffers(): ', err)
|
|
|
|
|
return false
|
2022-10-02 07:18:17 -07:00
|
|
|
}
|
|
|
|
|
}
|
2024-12-07 21:25:10 -08:00
|
|
|
|
|
|
|
|
// Clean the usage state so that stats reflect the last 24 hours.
|
|
|
|
|
cleanUsage () {
|
|
|
|
|
try {
|
2025-05-18 15:36:32 -07:00
|
|
|
const now = new Date()
|
|
|
|
|
console.log(`cleanUsage() Timer Controller executing at ${now.toLocaleString()}`)
|
|
|
|
|
|
2024-12-07 21:25:10 -08:00
|
|
|
this.useCases.usage.cleanUsage()
|
2025-01-15 17:59:09 -04:00
|
|
|
|
|
|
|
|
return true
|
2024-12-07 21:25:10 -08:00
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Error in time-controller.js/cleanUsage(): ', err)
|
|
|
|
|
|
|
|
|
|
// Note: Do not throw an error. This is a top-level function.
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-24 06:43:41 -07:00
|
|
|
}
|
|
|
|
|
|
2022-10-01 11:40:41 -07:00
|
|
|
export default TimerControllers
|