标签:jobs name echo row uil ash lines 格式 img
sql2slack对于slack 的处理是通过http 请求的,默认已经固定了格式了,以下是一个简单的修改,可以方便的支持钉钉webhook消息
而且具有通用性
主要是关于使用resty 处理http 请求的部分,默认是写死的,可以修改为
job.go
// _, err = resty.New().R().SetDoNotParseResponse(true).SetHeader("content-type", "application/json").SetBody(map[string]interface{}{
// "text": msg,
// }).Post(j.Channel)
?
// support dingding more flex
_, err = resty.New().R().SetDoNotParseResponse(true).SetHeader("content-type", "application/json").SetBody(msg).Post(j.Channel)
return err
为了方便,我们可以基于关键字模式的钉钉webhook
example.s2s.hcl
job tst {
channel = "替换为钉钉webhook的地址"
?
driver = "mysql"
?
dsn = "root:demo@tcp(mysql:3306)/demo"
?
query = <<SQL
SELECT users.* FROM users where status =1
SQL
?
schedule = "* * * * *"
?
message = <<JS
if ( $rows.length < 1 ) {
return
}
log("this is a demo")
var msg = "";
_.chain($rows).pluck(‘name‘).each(function(name){
msg += name+"--------";
})
var info = {
msgtype: "text",
text: {
content: msg
}
}
log(JSON.stringify(info))
say(JSON.stringify(info))
JS
}
version: "3"
services:
mysql:
image: mysql:5.7.16
ports:
- 3306:3306
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
environment:
MYSQL_ROOT_PASSWORD: demo
MYSQL_DATABASE: demo
MYSQL_USER: demo
MYSQL_PASSWORD: demo
TZ: Asia/Shanghai
sql2slack:
build: ./
image: dalongrong/sql2slack
command: -jobs-dir /opt/hcls
volumes:
- "./hcls:/opt/hcls"
FROM golang:alpine as builder
WORKDIR /app
RUN /bin/sed -i ‘s,http://dl-cdn.alpinelinux.org,https://mirrors.aliyun.com,g‘ /etc/apk/repositories
RUN apk add --no-cache git
COPY . /app/
ENV GO111MODULE=on
ENV GOPROXY=https://goproxy.cn
RUN go build
?
FROM alpine
COPY --from=builder /app/sql2slack ./
RUN echo $PWD
ENTRYPOINT ["./sql2slack"]
需要先启动mysql 服务,然后执行下边的sql内容
CREATE TABLE `users` (
`name` varchar(100) ,
`status` varchar(100)
) ENGINE=InnoDB
?
INSERT INTO demo.users
(name, status)
VALUES(‘dalong‘, ‘0‘);
INSERT INTO demo.users
(name, status)
VALUES(‘demo‘, ‘1‘);
INSERT INTO demo.users
(name, status)
VALUES(‘rong‘, ‘1‘);
docker-compose up -d sql2slack
以上是一个简单的扩展调整来支持钉钉消息,因为sql2slack 支持多数据库以及多数据源(独立文件模式)
同时基于js 引擎的灵活数据处理能力,我们基于此可以做好多灵活的数据处理
https://github.com/alash3al/sql2slack
https://github.com/rongfengliang/sql2webhook
标签:jobs name echo row uil ash lines 格式 img
原文地址:https://www.cnblogs.com/rongfengliang/p/13206808.html