use a cron job to update max history online user count

pull/388/head
Michael Li 1 year ago
parent 99e17d26a9
commit 92c0036bc1
No known key found for this signature in database

@ -384,7 +384,7 @@ release/paopao serve --no-default-features --features sqlite3,localoss,loggerfil
|[`Pprof`](docs/proposal/23062905-添加Pprof功能特性用于获取Profile.md)| 性能优化 | 内测 | 开启Pprof功能收集Profile信息 | |[`Pprof`](docs/proposal/23062905-添加Pprof功能特性用于获取Profile.md)| 性能优化 | 内测 | 开启Pprof功能收集Profile信息 |
|`PhoneBind` | 其他 | 稳定 | 手机绑定功能 | |`PhoneBind` | 其他 | 稳定 | 手机绑定功能 |
|`UseAuditHook` | 其他 | 内测 | 使用审核hook功能 | |`UseAuditHook` | 其他 | 内测 | 使用审核hook功能 |
|`UseJobManager` | 其他 | 内测 | 使用JobManager功能 | |`DisableJobManager` | 其他 | 内测 | 禁止使用JobManager功能 |
|`Web:DisallowUserRegister` | 功能特性 | 稳定 | 不允许用户注册 | |`Web:DisallowUserRegister` | 功能特性 | 稳定 | 不允许用户注册 |
> 功能项状态详情参考 [features-status](features-status.md). > 功能项状态详情参考 [features-status](features-status.md).

@ -203,7 +203,7 @@
* [x] 接口定义 * [x] 接口定义
* [x] 业务逻辑实现 * [x] 业务逻辑实现
* `UseJobManager` 使用JobManager功能 (目前状态: 内测 待完善后将转为Builtin) * `DisableJobManager` 禁止使用JobManager功能 (目前状态: 内测 待完善后将转为Builtin)
* [ ] 提按文档 * [ ] 提按文档
* [x] 接口定义 * [x] 接口定义
* [x] 业务逻辑实现 * [x] 业务逻辑实现

@ -38,6 +38,7 @@ var (
CacheSetting *cacheConf CacheSetting *cacheConf
EventManagerSetting *eventManagerConf EventManagerSetting *eventManagerConf
MetricManagerSetting *metricManagerConf MetricManagerSetting *metricManagerConf
JobManagerSetting *jobManagerConf
CacheIndexSetting *cacheIndexConf CacheIndexSetting *cacheIndexConf
SimpleCacheIndexSetting *simpleCacheIndexConf SimpleCacheIndexSetting *simpleCacheIndexConf
BigCacheIndexSetting *bigCacheIndexConf BigCacheIndexSetting *bigCacheIndexConf
@ -75,6 +76,7 @@ func setupSetting(suite []string, noDefault bool) error {
"Cache": &CacheSetting, "Cache": &CacheSetting,
"EventManager": &EventManagerSetting, "EventManager": &EventManagerSetting,
"MetricManager": &MetricManagerSetting, "MetricManager": &MetricManagerSetting,
"JobManager": &JobManagerSetting,
"PprofServer": &PprofServerSetting, "PprofServer": &PprofServerSetting,
"WebServer": &WebServerSetting, "WebServer": &WebServerSetting,
"AdminServer": &AdminServerSetting, "AdminServer": &AdminServerSetting,

@ -23,6 +23,8 @@ MetricManager: # 指标监控管理器的配置参数
MaxTempEventBuf: 256 # 最大log缓存条数, 设置范围[10, ++], 默认256 MaxTempEventBuf: 256 # 最大log缓存条数, 设置范围[10, ++], 默认256
MaxTickCount: 60 # 最大的循环周期, 设置范围[60, ++], 默认60 MaxTickCount: 60 # 最大的循环周期, 设置范围[60, ++], 默认60
TickWaitTime: 1 # 一个周期的等待时间,单位:秒 默认1s TickWaitTime: 1 # 一个周期的等待时间,单位:秒 默认1s
JobManager: # Cron Job理器的配置参数
MaxOnlineInterval: "@every 5m" # 更新最大在线人数默认每5分钟更新一次
Features: Features:
Default: [] Default: []
WebServer: # Web服务 WebServer: # Web服务

@ -120,6 +120,10 @@ type metricManagerConf struct {
TickWaitTime time.Duration TickWaitTime time.Duration
} }
type jobManagerConf struct {
MaxOnlineInterval string
}
type cacheIndexConf struct { type cacheIndexConf struct {
MaxUpdateQPS int MaxUpdateQPS int
MinWorker int MinWorker int

@ -73,10 +73,10 @@ func Schedule(s cron.Schedule, fn JobFn) EntryID {
func Initial() { func Initial() {
_onceInitial.Do(func() { _onceInitial.Do(func() {
initEventManager() initEventManager()
if cfg.If("UseJobManager") { cfg.Not("DisableJobManager", func() {
initJobManager() initJobManager()
logrus.Debugln("initial JobManager") logrus.Debugln("initial JobManager")
} })
}) })
} }

@ -0,0 +1,41 @@
// Copyright 2023 ROC. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package web
import (
"github.com/alimy/tryst/cfg"
"github.com/robfig/cron/v3"
"github.com/rocboss/paopao-ce/internal/conf"
"github.com/rocboss/paopao-ce/internal/events"
"github.com/sirupsen/logrus"
)
func onMaxOnlineJob() {
spec := conf.JobManagerSetting.MaxOnlineInterval
schedule, err := cron.ParseStandard(spec)
if err != nil {
panic(err)
}
events.Schedule(schedule, func() {
onlineUserKeys, err := _wc.Keys(conf.PrefixOnlineUser + "*")
if maxOnline := len(onlineUserKeys); err == nil && maxOnline > 0 {
if _, err = _wc.PutHistoryMaxOnline(maxOnline); err != nil {
logrus.Warnf("onMaxOnlineJob[2] occurs error: %s", err)
} else {
logrus.Debug("trigger onMaxOnlineJob run action")
}
} else if err != nil {
logrus.Warnf("onMaxOnlineJob[1] occurs error: %s", err)
}
})
}
func scheduleInnerJobs() {
cfg.Not("DisableJobManager", func() {
lazyInitial()
onMaxOnlineJob()
logrus.Debug("schedule inner jobs complete")
})
}

@ -46,6 +46,8 @@ func RouteWeb(e *gin.Engine) {
api.RegisterAlipayPubServant(e, newAlipayPubSrv(ds)) api.RegisterAlipayPubServant(e, newAlipayPubSrv(ds))
api.RegisterAlipayPrivServant(e, newAlipayPrivSrv(ds, client)) api.RegisterAlipayPrivServant(e, newAlipayPrivSrv(ds, client))
}) })
// shedule inner jobs if need
scheduleInnerJobs()
} }
// lazyInitial do some package lazy initialize for performance // lazyInitial do some package lazy initialize for performance

Loading…
Cancel
Save