Skip to content

Commit 7e910bf

Browse files
authored
feat: 三个三方IM定时任务的统一添加 (#62)
1 parent f46c195 commit 7e910bf

File tree

4 files changed

+70
-25
lines changed

4 files changed

+70
-25
lines changed

config.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,5 @@ feishu:
117117
# 配置获取详细文档参考:http://ldapdoc.eryajf.net/pages/83c90b/
118118
flag: "feishu" # 作为飞书在平台的标识
119119
app-id: "xxxxxxx" # 飞书的app-id
120-
app-secret: "xxxxxxxxxxx" # 飞书的app-secret
120+
app-secret: "xxxxxxxxxxx" # 飞书的app-secret
121+
enable-sync: false # 是否开启定时同步飞书的任务

config/config.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,12 @@ type WeComConfig struct {
165165
CorpID string `mapstructure:"corp-id" json:"corpId"`
166166
AgentID int `mapstructure:"agent-id" json:"agentId"`
167167
CorpSecret string `mapstructure:"corp-secret" json:"corpSecret"`
168+
EnableSync bool `mapstructure:"enable-sync" json:"enableSync"`
168169
}
169170

170171
type FeiShuConfig struct {
171-
Flag string `mapstructure:"flag" json:"flag"`
172-
AppID string `mapstructure:"app-id" json:"appId"`
173-
AppSecret string `mapstructure:"app-secret" json:"appSecret"`
172+
Flag string `mapstructure:"flag" json:"flag"`
173+
AppID string `mapstructure:"app-id" json:"appId"`
174+
AppSecret string `mapstructure:"app-secret" json:"appSecret"`
175+
EnableSync bool `mapstructure:"enable-sync" json:"enableSync"`
174176
}

logic/a_logic.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import (
55

66
"github.com/eryajf/go-ldap-admin/config"
77
"github.com/eryajf/go-ldap-admin/model"
8+
"github.com/eryajf/go-ldap-admin/public/common"
89
"github.com/eryajf/go-ldap-admin/public/tools"
910
"github.com/eryajf/go-ldap-admin/service/ildap"
1011
"github.com/eryajf/go-ldap-admin/service/isql"
1112
jsoniter "github.com/json-iterator/go"
13+
"github.com/robfig/cron/v3"
1214
"github.com/tidwall/gjson"
1315
)
1416

@@ -304,3 +306,61 @@ func ConvertUserData(flag string, remoteData []map[string]interface{}) (users []
304306
}
305307
return
306308
}
309+
310+
func InitCron() {
311+
c := cron.New(cron.WithSeconds())
312+
313+
if config.Conf.DingTalk.EnableSync {
314+
//启动定时任务
315+
_, err := c.AddFunc("0 1 5 * * *", func() {
316+
common.Log.Info("每天凌晨5点1分0秒执行一次同步钉钉部门信息到ldap")
317+
DingTalk.SyncDingTalkDepts(nil, nil)
318+
})
319+
if err != nil {
320+
common.Log.Errorf("启动同步部门的定时任务失败: %v", err)
321+
}
322+
//每天凌晨1点执行一次
323+
_, err = c.AddFunc("0 30 5 * * *", func() {
324+
common.Log.Info("每天凌晨5点30分执行一次同步钉钉用户信息到ldap")
325+
DingTalk.SyncDingTalkUsers(nil, nil)
326+
})
327+
if err != nil {
328+
common.Log.Errorf("启动同步用户的定时任务失败: %v", err)
329+
}
330+
}
331+
if config.Conf.WeCom.EnableSync {
332+
_, err := c.AddFunc("0 1 5 * * *", func() {
333+
common.Log.Info("每天凌晨5点1分0秒执行一次同步企业微信部门信息到ldap")
334+
WeCom.SyncWeComDepts(nil, nil)
335+
})
336+
if err != nil {
337+
common.Log.Errorf("启动同步部门的定时任务失败: %v", err)
338+
}
339+
//每天凌晨1点执行一次
340+
_, err = c.AddFunc("0 30 5 * * *", func() {
341+
common.Log.Info("每天凌晨5点30分执行一次同步企业微信用户信息到ldap")
342+
WeCom.SyncWeComUsers(nil, nil)
343+
})
344+
if err != nil {
345+
common.Log.Errorf("启动同步用户的定时任务失败: %v", err)
346+
}
347+
}
348+
if config.Conf.FeiShu.EnableSync {
349+
_, err := c.AddFunc("0 1 5 * * *", func() {
350+
common.Log.Info("每天凌晨5点1分0秒执行一次同步飞书部门信息到ldap")
351+
FeiShu.SyncFeiShuDepts(nil, nil)
352+
})
353+
if err != nil {
354+
common.Log.Errorf("启动同步部门的定时任务失败: %v", err)
355+
}
356+
//每天凌晨1点执行一次
357+
_, err = c.AddFunc("0 30 5 * * *", func() {
358+
common.Log.Info("每天凌晨5点30分执行一次同步飞书用户信息到ldap")
359+
FeiShu.SyncFeiShuUsers(nil, nil)
360+
})
361+
if err != nil {
362+
common.Log.Errorf("启动同步用户的定时任务失败: %v", err)
363+
}
364+
}
365+
c.Start()
366+
}

main.go

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"time"
1010

1111
"github.com/eryajf/go-ldap-admin/logic"
12-
"github.com/robfig/cron/v3"
1312

1413
"github.com/eryajf/go-ldap-admin/config"
1514
"github.com/eryajf/go-ldap-admin/middleware"
@@ -65,26 +64,9 @@ func main() {
6564
common.Log.Fatalf("listen: %s\n", err)
6665
}
6766
}()
68-
if config.Conf.DingTalk.EnableSync {
69-
//启动定时任务
70-
c := cron.New(cron.WithSeconds())
71-
_, err := c.AddFunc("0 1 0 * * *", func() {
72-
common.Log.Info("每天0点1分0秒执行一次同步钉钉部门和用户信息到ldap")
73-
logic.DingTalk.SyncDingTalkDepts(nil, nil)
74-
})
75-
if err != nil {
76-
common.Log.Errorf("启动同步部门的定时任务失败: %v", err)
77-
}
78-
//每天凌晨1点执行一次
79-
_, err = c.AddFunc("0 15 0 * * *", func() {
80-
common.Log.Info("每天凌晨00点15分执行一次同步钉钉部门和用户信息到ldap")
81-
logic.DingTalk.SyncDingTalkUsers(nil, nil)
82-
})
83-
if err != nil {
84-
common.Log.Errorf("启动同步用户的定时任务失败: %v", err)
85-
}
86-
c.Start()
87-
}
67+
68+
// 启动定时任务
69+
logic.InitCron()
8870

8971
common.Log.Info(fmt.Sprintf("Server is running at %s:%d/%s", host, port, config.Conf.System.UrlPathPrefix))
9072

0 commit comments

Comments
 (0)