cronsun定时推送百度热搜新闻到飞书

安装cronsun(分布式定时任务管理)

1、下载cronsun二进制文件:github.com/shunfei/cronsun/release...

2、安装MongoDB、etcd3,我采用的docker方式,采用Portainer管理docker:

Cronsun定时推送百度热搜新闻到飞书

3、解压cronsun二进制文件,编辑Etcd(conf/db.json)和MongoDB(conf/etcd.json)的配置文件

4、启动cronsun和cronweb节点

./cronnode -conf conf/base.json
./cronweb -conf conf/base.json

使用supervisord管理守护进程
【1】cronnode.ini

[program:cronnode]
command = /www/cronsun/cronnode -conf "/www/cronsun/conf/base.json"
restart_when_binary_changed=true
restart_signal_when_binary_changed=SIGHUP
numprocs=1                    ; #启动几个进程
process_name=%(program_name)s_%(process_num)s ; process_name expr (default %(program_name)s)
autostart=true                ; #随着supervisord的启动而启动
autorestart=true              ; #自动重启。。当然要选上了
startsecs=5                   ; #启动5秒后没有异常退出,就当作已经正常启动了
startretries=1                ; #启动失败时的最多重试次数
exitcodes=0                   ; #正常退出代码(是说退出代码是这个时就不再重启了吗?待确定)
stopsignal=KILL               ; #用来杀死进程的信号
stopwaitsecs=10               ; #发送SIGKILL前的等待时间
redirect_stderr=true          ; #重定向stderr到stdout
stopasgroup=true
stdout_logfile=/tmp/cronnode_stdout.log
directory=/www/cronsun/

【2】cronweb.ini

[program:cronweb]
command = /www/cronsun/cronweb -conf "/www/cronsun/conf/base.json"
restart_when_binary_changed=true
restart_signal_when_binary_changed=SIGHUP
numprocs=1                    ; #启动几个进程
process_name=%(program_name)s_%(process_num)s ; process_name expr (default %(program_name)s)
autostart=true                ; #随着supervisord的启动而启动
autorestart=true              ; #自动重启。。当然要选上了
startsecs=5                   ; #启动5秒后没有异常退出,就当作已经正常启动了
startretries=1                ; #启动失败时的最多重试次数
exitcodes=0                   ; #正常退出代码(是说退出代码是这个时就不再重启了吗?待确定)
stopsignal=KILL               ; #用来杀死进程的信号
stopwaitsecs=10               ; #发送SIGKILL前的等待时间
redirect_stderr=true          ; #重定向stderr到stdout
stopasgroup=true
stdout_logfile=/tmp/cronweb_stdout.log
directory=/www/cronsun/

5、在浏览器打开127.0.0.1:7079 ,采用默认账户登录(用户名 :admin@admin.com、密码:admin,登录后记得修改密码),添加自己的定时任务

cronsun定时推送百度热搜新闻到飞书

推送百度热搜到飞书的代码

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io"
    "log"
    "net/http"
    "strconv"
    "strings"
    "time"

    "github.com/PuerkitoBio/goquery"
)

const (
    WebHook = "https://open.feishu.cn/open-apis/bot/v2/hook/xxx"
)

type TextMsgParams struct {
    MsgType string      `json:"msg_type"`
    Content TextMsgBody `json:"content"`
}

type TextMsgBody struct {
    Text string `json:"text"`
}

func main() {
    // 百度热搜榜
    url := "https://top.baidu.com/board?tab=realtime"

    res, err := http.Get(url)
    if err != nil {
        log.Fatal(err)
    }
    defer res.Body.Close()
    if res.StatusCode != 200 {
        log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
    }

    doc, err := goquery.NewDocumentFromReader(res.Body)
    if err != nil {
        log.Fatal(err)
    }

    var baiduHot []string
    doc.Find("#sanRoot > main > div.container.right-container_2EFJr > div > div:nth-child(2) > div").Each(func(i int, s *goquery.Selection) {
        titleDom := s.Find("div:nth-child(" + strconv.Itoa(i+1) + ") > div.content_1YWBm > a > div.c-single-text-ellipsis")
        title := strings.TrimSpace(titleDom.Text())
        baiduHot = append(baiduHot, title)
    })

    news := strings.Join(baiduHot, "\n\n")
    resp := SendTextMsg(news)
    fmt.Println(resp)
}

func SendTextMsg(msg string) string {
    client := &http.Client{Timeout: 2 * time.Second}

    message := TextMsgParams{MsgType: "text", Content: TextMsgBody{
        Text: msg,
    }}
    jsonStr, _ := json.Marshal(message)

    resp, err := client.Post(WebHook, "application/json", bytes.NewBuffer(jsonStr))
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    result, _ := io.ReadAll(resp.Body)
    return string(result)
}

推送结果

cronsun定时推送百度热搜新闻到飞书

遇到的问题

1、cronsun启动报错:“./cronnode -conf conf/base.json -l -1

2023-04-27T14:44:32.740+0800 ERROR node/server.go:38 Connect to MongoDB [127.0.0.1:27017] failed: no reachable servers”

问题原因:cronsun不兼容新版本的mongo

解决办法:
1、更换mongo为低版本的,如mongo:4.4.23
2、自己修复cronsun的Go源码,重新编译

本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!