tRPC-Go timer plugin
English | 中文
timer service
server:
service:
- name: trpc.app.server.service
nic: eth1
port: 9001
network: "0 */5 * * * *"
protocol: timer
timeout: 1000
package main
import (
"context"
"trpc.group/trpc-go/trpc-database/timer"
trpc "trpc.group/trpc-go/trpc-go"
)
func main() {
s := trpc.NewServer()
timer.RegisterHandlerService(s, handle)
s.Serve()
}
func handle(ctx context.Context) error {
return nil
}
task scheduler
The default timer is a local timer, which all nodes will start timer tasks, and each node is independent of each other.
Timer task uses cron second-level syntax. If you want to execute immediately when the program starts, you need to configure: network = "0 */5 * * * *?startAtOnce=1", so that the first execution failure will prevent the program to start.
If you need distributed mutual exclusion tasks, which the same service, the same time, only one node will execute, you can implement the Scheduler and register it.
type Scheduler struct {
}
func (s *Scheduler) Schedule(serviceName string, newNode string, holdTime time.Duration) (nowNode string, err error) {
return nowNode, nil
}
func main() {
timer.RegisterScheduler("name", &Scheduler{})
s := trpc.NewServer()
timer.RegisterHandlerService(s, handle)
s.Serve()
}
server:
service:
- name: trpc.app.server.service
network: "0 */1 * * * *?scheduler=name"
protocol: timer
parameter description
| Schedule | task timer | self-registration |
| ServiceName | service process name | self-defined |
| holdTime | effective time of task preemption | unit: seconds |
| StartAtOnce | whether to execute when starting the program | 1: execute; 0: not execute |