Skip to content

Commit

Permalink
添加定时任务
Browse files Browse the repository at this point in the history
  • Loading branch information
TruthHun88 committed Jul 30, 2018
1 parent 9a42c6a commit 370c5c4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
29 changes: 25 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@ import (
"github.com/TruthHun/DocHub/models"
_ "github.com/TruthHun/DocHub/routers"
"github.com/astaxie/beego"
"github.com/astaxie/beego/toolbox"
)

func main() {
go execTask()
//定义错误和异常处理控制器
beego.ErrorController(&HomeControllers.BaseController{})
beego.Run()
}

//初始化函数
func init() {
fmt.Println("")
Expand Down Expand Up @@ -52,8 +60,21 @@ func init() {
beego.AddFuncMap("GetDescByDid", models.ModelDocText.GetDescByDid)
}

func main() {
//定义错误和异常处理控制器
beego.ErrorController(&HomeControllers.BaseController{})
beego.Run()
//定时器,定时更新sitemap和ElasticSearch全量索引
func execTask() {
//每天凌晨两点执行sitemap更新
updateSitemap := toolbox.NewTask("updateSitemap", "0 0 2 * * *", func() error {
models.ModelSeo.BuildSitemap()
return nil
})
//每天凌晨3:00执行elasticsearch全文搜索全量更新
updateIndex := toolbox.NewTask("updateIndex", "0 0 3 * * *", func() error {
//TODO:
return nil
})

toolbox.AddTask("updateSitemap", updateSitemap)
toolbox.AddTask("updateIndex", updateIndex)
toolbox.StartTask()
//defer toolbox.StopTask()
}
2 changes: 1 addition & 1 deletion models/Models.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ var Fields = map[string]map[string]string{
}

//初始化数据库注册
func nit() {
func init() {
//初始化数据库
RegisterDB()
runmode := beego.AppConfig.String("runmode")
Expand Down
6 changes: 5 additions & 1 deletion models/SeoModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

"strconv"

"fmt"

"github.com/TruthHun/DocHub/helper"
"github.com/TruthHun/gotil/sitemap"
)
Expand Down Expand Up @@ -66,7 +68,8 @@ func (this *Seo) GetByPage(page string, defaultTitle, defaultKeywords, defaultDe
//}
//生成站点地图
func (this *Seo) BuildSitemap() {

//更新站点地图
helper.Logger.Info(fmt.Sprintf("[%v]更新站点地图[start]", time.Now().Format("2006-01-02 15:04:05")))
var (
files []string
fileNum int
Expand Down Expand Up @@ -120,4 +123,5 @@ func (this *Seo) BuildSitemap() {
if err := Sitemap.CreateSitemapIndex(si, "sitemap.xml"); err != nil {
helper.Logger.Error("sitemap生成失败:" + err.Error())
}
helper.Logger.Info(fmt.Sprintf("[%v]更新站点地图[end]", time.Now().Format("2006-01-02 15:04:05")))
}

0 comments on commit 370c5c4

Please sign in to comment.