feat: 三个三方IM定时任务的统一添加 (#62)

This commit is contained in:
二丫讲梵 2022-07-10 17:40:04 +08:00 committed by GitHub
parent f46c19510d
commit 7e910bf5fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 25 deletions

View File

@ -118,3 +118,4 @@ feishu:
flag: "feishu" # 作为飞书在平台的标识
app-id: "xxxxxxx" # 飞书的app-id
app-secret: "xxxxxxxxxxx" # 飞书的app-secret
enable-sync: false # 是否开启定时同步飞书的任务

View File

@ -165,10 +165,12 @@ type WeComConfig struct {
CorpID string `mapstructure:"corp-id" json:"corpId"`
AgentID int `mapstructure:"agent-id" json:"agentId"`
CorpSecret string `mapstructure:"corp-secret" json:"corpSecret"`
EnableSync bool `mapstructure:"enable-sync" json:"enableSync"`
}
type FeiShuConfig struct {
Flag string `mapstructure:"flag" json:"flag"`
AppID string `mapstructure:"app-id" json:"appId"`
AppSecret string `mapstructure:"app-secret" json:"appSecret"`
EnableSync bool `mapstructure:"enable-sync" json:"enableSync"`
}

View File

@ -5,10 +5,12 @@ import (
"github.com/eryajf/go-ldap-admin/config"
"github.com/eryajf/go-ldap-admin/model"
"github.com/eryajf/go-ldap-admin/public/common"
"github.com/eryajf/go-ldap-admin/public/tools"
"github.com/eryajf/go-ldap-admin/service/ildap"
"github.com/eryajf/go-ldap-admin/service/isql"
jsoniter "github.com/json-iterator/go"
"github.com/robfig/cron/v3"
"github.com/tidwall/gjson"
)
@ -304,3 +306,61 @@ func ConvertUserData(flag string, remoteData []map[string]interface{}) (users []
}
return
}
func InitCron() {
c := cron.New(cron.WithSeconds())
if config.Conf.DingTalk.EnableSync {
//启动定时任务
_, err := c.AddFunc("0 1 5 * * *", func() {
common.Log.Info("每天凌晨5点1分0秒执行一次同步钉钉部门信息到ldap")
DingTalk.SyncDingTalkDepts(nil, nil)
})
if err != nil {
common.Log.Errorf("启动同步部门的定时任务失败: %v", err)
}
//每天凌晨1点执行一次
_, err = c.AddFunc("0 30 5 * * *", func() {
common.Log.Info("每天凌晨5点30分执行一次同步钉钉用户信息到ldap")
DingTalk.SyncDingTalkUsers(nil, nil)
})
if err != nil {
common.Log.Errorf("启动同步用户的定时任务失败: %v", err)
}
}
if config.Conf.WeCom.EnableSync {
_, err := c.AddFunc("0 1 5 * * *", func() {
common.Log.Info("每天凌晨5点1分0秒执行一次同步企业微信部门信息到ldap")
WeCom.SyncWeComDepts(nil, nil)
})
if err != nil {
common.Log.Errorf("启动同步部门的定时任务失败: %v", err)
}
//每天凌晨1点执行一次
_, err = c.AddFunc("0 30 5 * * *", func() {
common.Log.Info("每天凌晨5点30分执行一次同步企业微信用户信息到ldap")
WeCom.SyncWeComUsers(nil, nil)
})
if err != nil {
common.Log.Errorf("启动同步用户的定时任务失败: %v", err)
}
}
if config.Conf.FeiShu.EnableSync {
_, err := c.AddFunc("0 1 5 * * *", func() {
common.Log.Info("每天凌晨5点1分0秒执行一次同步飞书部门信息到ldap")
FeiShu.SyncFeiShuDepts(nil, nil)
})
if err != nil {
common.Log.Errorf("启动同步部门的定时任务失败: %v", err)
}
//每天凌晨1点执行一次
_, err = c.AddFunc("0 30 5 * * *", func() {
common.Log.Info("每天凌晨5点30分执行一次同步飞书用户信息到ldap")
FeiShu.SyncFeiShuUsers(nil, nil)
})
if err != nil {
common.Log.Errorf("启动同步用户的定时任务失败: %v", err)
}
}
c.Start()
}

22
main.go
View File

@ -9,7 +9,6 @@ import (
"time"
"github.com/eryajf/go-ldap-admin/logic"
"github.com/robfig/cron/v3"
"github.com/eryajf/go-ldap-admin/config"
"github.com/eryajf/go-ldap-admin/middleware"
@ -65,26 +64,9 @@ func main() {
common.Log.Fatalf("listen: %s\n", err)
}
}()
if config.Conf.DingTalk.EnableSync {
// 启动定时任务
c := cron.New(cron.WithSeconds())
_, err := c.AddFunc("0 1 0 * * *", func() {
common.Log.Info("每天0点1分0秒执行一次同步钉钉部门和用户信息到ldap")
logic.DingTalk.SyncDingTalkDepts(nil, nil)
})
if err != nil {
common.Log.Errorf("启动同步部门的定时任务失败: %v", err)
}
//每天凌晨1点执行一次
_, err = c.AddFunc("0 15 0 * * *", func() {
common.Log.Info("每天凌晨00点15分执行一次同步钉钉部门和用户信息到ldap")
logic.DingTalk.SyncDingTalkUsers(nil, nil)
})
if err != nil {
common.Log.Errorf("启动同步用户的定时任务失败: %v", err)
}
c.Start()
}
logic.InitCron()
common.Log.Info(fmt.Sprintf("Server is running at %s:%d/%s", host, port, config.Conf.System.UrlPathPrefix))