init project

This commit is contained in:
admin committed 2025-12-05 17:23:29 +08:00
commit d63cc8d9f5
33 files changed
+1529

No files matched your search

+77
View File
@@ -0,0 +1,77 @@
package crontab
import (
"context"
"fmt"
"git.xiaotuo.co/micro/skeleton/internal/runtime"
"git.xiaotuo.co/micro/skeleton/pkg/util"
"git.xiaotuo.co/pkg/tool/stringx"
"github.com/robfig/cron/v3"
log "github.com/sirupsen/logrus"
)
type Crontab struct {
options *runtime.Options
exit context.CancelFunc
}
func NewCrontab() *Crontab {
ctx, cancel := context.WithCancel(context.Background())
opts := &runtime.Options{
ID: stringx.MakeUUID(),
Type: "crontab",
Context: ctx,
}
return &Crontab{
options: opts,
exit: cancel,
}
}
func (m *Crontab) Init(opts ...runtime.Option) error {
for _, o := range opts {
o(m.options)
}
return nil
}
func (m *Crontab) Start() error {
log.Infof("crontab %s Start...", m.String())
defer log.Infof("crontab Exit...")
c := cron.New(cron.WithSeconds())
// _, _ = c.AddJob("10/* * * * * *", Job(func() { // 每天 10s 执行
//
// }))
c.Start()
log.Infof("crontab start")
<-m.options.Context.Done()
log.Infof("crontab done")
c.Stop()
return nil
}
func (m *Crontab) Stop() error {
m.exit()
return nil
}
func (m *Crontab) String() string {
return fmt.Sprintf("%s://%s", m.options.Type, m.options.ID)
}
func Job(fn func()) cron.Job {
return cron.NewChain(cron.SkipIfStillRunning(cron.DefaultLogger)).Then(cron.FuncJob(fn))
}
func newContext() context.Context {
ctx := context.Background()
ctx = context.WithValue(ctx, "req_id", util.MakeRequestID())
return ctx
}
+51
View File
@@ -0,0 +1,51 @@
package monitor
import (
"context"
"fmt"
"git.xiaotuo.co/micro/skeleton/internal/runtime"
"git.xiaotuo.co/pkg/tool/stringx"
)
type monitor struct {
options *runtime.Options
exit context.CancelFunc
}
func NewMonitor() runtime.Runtime {
ctx, cancel := context.WithCancel(context.Background())
opts := &runtime.Options{
ID: stringx.MakeUUID(),
Type: "monitor",
Context: ctx,
}
return &monitor{
options: opts,
exit: cancel,
}
}
func (m *monitor) Init(opts ...runtime.Option) error {
for _, o := range opts {
o(m.options)
}
return nil
}
func (m *monitor) Start() error {
select {
case <-m.options.Context.Done():
return nil
}
}
func (m *monitor) Stop() error {
m.exit()
return nil
}
func (m *monitor) String() string {
return fmt.Sprintf("%s://%s", m.options.Type, m.options.ID)
}
+36
View File
@@ -0,0 +1,36 @@
package runtime
import (
"context"
)
type Option func(o *Options)
type Options struct {
ID string
Type string
// 自定义参数
Context context.Context
}
func WithID(id string) Option {
return func(o *Options) {
o.ID = id
}
}
func WithType(t string) Option {
return func(o *Options) {
o.Type = t
}
}
func SetRuntimeOption(k, v interface{}) Option {
return func(o *Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, k, v)
}
}
+12
View File
@@ -0,0 +1,12 @@
package runtime
type Runtime interface {
// Init initializes runtime
Init(...Option) error
// Start starts the runtime
Start() error
// Stop shuts down the runtime
Stop() error
// String defines the runtime implementation
String() string
}