golang支持webhook、cron和经典调度的任务调度插件leprechaun的使用

Golang支持Webhook、Cron和经典调度的任务调度插件Leprechaun使用指南

概述

Leprechaun是一个Golang编写的任务调度工具,支持三种类型的任务调度:

  1. 经典调度(Schedule)
  2. Webhook触发(Hook)
  3. Cron表达式调度(Cron)

安装

进入leprechaun目录运行:

make install  # 需要sudo权限

安装远程服务:

make install-remote-service

任务配置示例

1. 经典调度任务

name: job1  # 任务名称
definition: schedule  # 任务类型
schedule:
  min: 0  # 每分钟
  hour: 0  # 每小时
  day: 0  # 每天
steps:  # 按顺序执行的步骤
  - touch ./test.txt
  - echo "Is this working?" > ./test.txt
  - mv ./test.txt ./imwondering.txt

2. Webhook触发任务

name: job2  # 任务名称
definition: hook  # 任务类型
id: 45DE2239F  # 用于查找任务的ID
steps:
  - echo "Hooked!" > ./hook.txt

触发方式:向{host}:{port}/hook?id={id_of_recipe}发送请求,例如:

localhost:11400/hook?id=45DE2239F

3. Cron表达式调度任务

name: job3  # 任务名称
definition: cron  # 任务类型
pattern: * * * * *  # Cron表达式
steps:  # 按顺序执行的步骤
  - touch ./test.txt
  - echo "Is this working?" > ./test.txt
  - mv ./test.txt ./imwondering.txt

高级功能

1. 步骤管道

可以将一个步骤的输出传递给下一个步骤:

steps:
  - echo "Pipe this to next step" }>
  - cat > piped.txt

2. 步骤失败处理

如果某个步骤失败则停止执行后续步骤:

steps:
  - ! echo "Pipe this to next step" }>
  - cat > piped.txt

3. 远程步骤执行

可以在远程主机上执行步骤:

steps:
  - rmt:some_host echo "Pipe this to next step" }>
  - rmt:some_other_host grep -a "Pipe" }>
  - cat > stored.txt

4. 异步步骤执行

使用->标记异步步骤:

steps:
  - -> ping google.com  # 异步执行
  - echo "I will not wait above task to perform"  # 立即执行

启动与停止

启动服务:

leprechaun &

查看帮助:

leprechaun --help

环境变量支持

步骤中可以使用环境变量:

steps:
  - echo "Is this working?" > $LOGNAME

构建

构建主服务:

make build

构建远程服务:

make build-remote-service

测试

运行测试:

make test

生成测试报告:

make test-with-report

测试特定包:

make test-package package=[name]

更多关于golang支持webhook、cron和经典调度的任务调度插件leprechaun的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang支持webhook、cron和经典调度的任务调度插件leprechaun的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


Leprechaun - Golang任务调度插件使用指南

Leprechaun是一个轻量级的Golang任务调度库,支持Webhook、Cron定时任务和经典调度。下面我将详细介绍它的使用方法。

安装Leprechaun

go get github.com/kilgaloon/leprechaun

基本使用示例

1. 创建调度器实例

package main

import (
	"github.com/kilgaloon/leprechaun/daemon"
	"github.com/kilgaloon/leprechaun/worker"
)

func main() {
	// 创建配置
	cfg := daemon.NewConfig(".")
	
	// 创建调度器实例
	scheduler := worker.New(cfg)
	
	// 启动调度器
	scheduler.Start()
	
	// 保持程序运行
	select {}
}

Webhook任务调度

2. 注册Webhook任务

func setupWebhookTasks(scheduler *worker.Worker) {
	// 定义一个简单的Webhook处理器
	handler := func(ctx *worker.Context) {
		// 获取请求参数
		name := ctx.GetRequest().GetFormValue("name")
		
		// 处理逻辑
		ctx.GetResponse().SetBody(fmt.Sprintf("Hello, %s!", name))
		
		// 记录日志
		ctx.GetLogger().Info("Webhook任务执行完成")
	}
	
	// 注册Webhook路由
	scheduler.RegisterWebhook("greet", "/greet", handler)
}

Cron定时任务

3. 注册Cron任务

func setupCronTasks(scheduler *worker.Worker) {
	// 定义一个Cron任务
	task := func() {
		scheduler.GetLogger().Info("Cron任务执行: 每30秒一次")
	}
	
	// 添加Cron任务
	err := scheduler.AddCronJob("example_cron", "*/30 * * * * *", task)
	if err != nil {
		scheduler.GetLogger().Errorf("添加Cron任务失败: %v", err)
	}
}

经典调度任务

4. 注册经典调度任务

func setupClassicTasks(scheduler *worker.Worker) {
	// 定义一个经典任务
	task := func() {
		scheduler.GetLogger().Info("经典任务执行")
	}
	
	// 添加立即执行的任务
	scheduler.AddJob("immediate_job", task)
	
	// 添加延迟执行的任务(5秒后)
	scheduler.AddJobWithDelay("delayed_job", 5*time.Second, task)
	
	// 添加周期性任务(每10秒执行一次)
	scheduler.AddRecurringJob("recurring_job", 10*time.Second, task)
}

完整示例

package main

import (
	"fmt"
	"time"
	
	"github.com/kilgaloon/leprechaun/daemon"
	"github.com/kilgaloon/leprechaun/worker"
)

func main() {
	// 1. 创建配置和调度器
	cfg := daemon.NewConfig(".")
	scheduler := worker.New(cfg)
	
	// 2. 设置各种任务
	setupWebhookTasks(scheduler)
	setupCronTasks(scheduler)
	setupClassicTasks(scheduler)
	
	// 3. 启动调度器
	scheduler.Start()
	
	// 4. 保持程序运行
	select {}
}

func setupWebhookTasks(s *worker.Worker) {
	handler := func(ctx *worker.Context) {
		name := ctx.GetRequest().GetFormValue("name")
		ctx.GetResponse().SetBody(fmt.Sprintf("Hello, %s!", name))
		ctx.GetLogger().Info("Webhook任务执行完成")
	}
	s.RegisterWebhook("greet", "/greet", handler)
}

func setupCronTasks(s *worker.Worker) {
	task := func() {
		s.GetLogger().Info("Cron任务执行: 每30秒一次")
	}
	if err := s.AddCronJob("example_cron", "*/30 * * * * *", task); err != nil {
		s.GetLogger().Errorf("添加Cron任务失败: %v", err)
	}
}

func setupClassicTasks(s *worker.Worker) {
	task := func() {
		s.GetLogger().Info("经典任务执行")
	}
	s.AddJob("immediate_job", task)
	s.AddJobWithDelay("delayed_job", 5*time.Second, task)
	s.AddRecurringJob("recurring_job", 10*time.Second, task)
}

高级功能

5. 配置文件

Leprechaun支持通过配置文件进行配置。创建configs/configuration.ini文件:

[server]
host = 127.0.0.1
port = 8080
workers = 10

[settings]
debug = true

6. 任务监控

// 获取所有任务状态
jobs := scheduler.GetJobs()
for name, job := range jobs {
	fmt.Printf("任务名称: %s, 状态: %s\n", name, job.GetStatus())
}

// 停止特定任务
scheduler.StopJob("recurring_job")

// 重新启动任务
scheduler.StartJob("recurring_job")

注意事项

  1. Leprechaun的Cron表达式支持秒级精度(6位),格式为: 秒 分 时 日 月 周
  2. Webhook默认监听在8080端口,可在配置中修改
  3. 任务执行日志会自动记录
  4. 对于长时间运行的任务,建议使用context处理取消逻辑

希望这个指南能帮助你快速上手Leprechaun任务调度插件!

回到顶部