mirror of https://github.com/rocboss/paopao-ce
parent
0ad77cf17b
commit
2ae7d4d02a
@ -0,0 +1,110 @@
|
||||
// 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 events
|
||||
|
||||
import (
|
||||
"github.com/robfig/cron/v3"
|
||||
)
|
||||
|
||||
var (
|
||||
_defaultJobManager JobManager = (*jobManager)(nil)
|
||||
)
|
||||
|
||||
type (
|
||||
EntryID = cron.EntryID
|
||||
)
|
||||
|
||||
// JobFn job help function that implement cron.Job interface
|
||||
type JobFn func()
|
||||
|
||||
func (fn JobFn) Run() {
|
||||
fn()
|
||||
}
|
||||
|
||||
// Job job interface
|
||||
type Job interface {
|
||||
cron.Schedule
|
||||
cron.Job
|
||||
}
|
||||
|
||||
type simpleJob struct {
|
||||
cron.Schedule
|
||||
cron.Job
|
||||
}
|
||||
|
||||
// JobManager job manger interface
|
||||
type JobManager interface {
|
||||
Start()
|
||||
Stop()
|
||||
Remove(id EntryID)
|
||||
Schedule(Job) EntryID
|
||||
}
|
||||
|
||||
type jobManager struct {
|
||||
m *cron.Cron
|
||||
}
|
||||
|
||||
func (j *jobManager) Start() {
|
||||
j.m.Start()
|
||||
}
|
||||
|
||||
func (j *jobManager) Stop() {
|
||||
j.m.Stop()
|
||||
}
|
||||
|
||||
// Remove an entry from being run in the future.
|
||||
func (j *jobManager) Remove(id EntryID) {
|
||||
j.m.Remove(id)
|
||||
}
|
||||
|
||||
// Schedule adds a Job to the Cron to be run on the given schedule.
|
||||
// The job is wrapped with the configured Chain.
|
||||
func (j *jobManager) Schedule(job Job) EntryID {
|
||||
return j.m.Schedule(job, job)
|
||||
}
|
||||
|
||||
func initJobManager() {
|
||||
_defaultJobManager = &jobManager{
|
||||
m: cron.New(),
|
||||
}
|
||||
StartJobManager()
|
||||
}
|
||||
|
||||
func StartJobManager() {
|
||||
_defaultJobManager.Start()
|
||||
}
|
||||
|
||||
func StopJobManager() {
|
||||
_defaultJobManager.Stop()
|
||||
}
|
||||
|
||||
// NewJob create new Job instance
|
||||
func NewJob(s cron.Schedule, fn JobFn) Job {
|
||||
return &simpleJob{
|
||||
Schedule: s,
|
||||
Job: fn,
|
||||
}
|
||||
}
|
||||
|
||||
// RemoveJob an entry from being run in the future.
|
||||
func RemoveJob(id EntryID) {
|
||||
_defaultJobManager.Remove(id)
|
||||
}
|
||||
|
||||
// ScheduleJob adds a Job to the Cron to be run on the given schedule.
|
||||
// The job is wrapped with the configured Chain.
|
||||
func ScheduleJob(job Job) EntryID {
|
||||
return _defaultJobManager.Schedule(job)
|
||||
}
|
||||
|
||||
// Schedule adds a Job to the Cron to be run on the given schedule.
|
||||
// The job is wrapped with the configured Chain.
|
||||
func Schedule(s cron.Schedule, fn JobFn) EntryID {
|
||||
job := &simpleJob{
|
||||
Schedule: s,
|
||||
Job: fn,
|
||||
}
|
||||
return _defaultJobManager.Schedule(job)
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
// 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 events
|
||||
|
||||
import (
|
||||
"github.com/alimy/tryst/event"
|
||||
"github.com/alimy/tryst/pool"
|
||||
"github.com/rocboss/paopao-ce/internal/conf"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var (
|
||||
_defaultEventManager event.EventManager
|
||||
)
|
||||
|
||||
func initEventManager() {
|
||||
var opts []pool.Option
|
||||
s := conf.EventManagerSetting
|
||||
if s.MinWorker > 5 {
|
||||
opts = append(opts, pool.MinWorkerOpt(s.MinWorker))
|
||||
} else {
|
||||
opts = append(opts, pool.MinWorkerOpt(5))
|
||||
}
|
||||
if s.MaxEventBuf > 10 {
|
||||
opts = append(opts, pool.MaxRequestBufOpt(s.MaxEventBuf))
|
||||
} else {
|
||||
opts = append(opts, pool.MaxRequestBufOpt(10))
|
||||
}
|
||||
if s.MaxTempEventBuf > 10 {
|
||||
opts = append(opts, pool.MaxRequestTempBufOpt(s.MaxTempEventBuf))
|
||||
} else {
|
||||
opts = append(opts, pool.MaxRequestTempBufOpt(10))
|
||||
}
|
||||
opts = append(opts, pool.MaxTickCountOpt(s.MaxTickCount), pool.TickWaitTimeOpt(s.TickWaitTime))
|
||||
_defaultEventManager = event.NewEventManager(func(req event.Event, err error) {
|
||||
if err != nil {
|
||||
logrus.Errorf("handle event[%s] occurs error: %s", req.Name(), err)
|
||||
}
|
||||
}, opts...)
|
||||
}
|
||||
|
||||
func StartEventManager() {
|
||||
_defaultEventManager.Start()
|
||||
}
|
||||
|
||||
func StopEventManager() {
|
||||
_defaultEventManager.Stop()
|
||||
}
|
||||
|
||||
// OnEvent push event to gorotine pool then handled automatic.
|
||||
func OnEvent(event event.Event) {
|
||||
_defaultEventManager.OnEvent(event)
|
||||
}
|
Loading…
Reference in new issue