-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: automatically obtain datasets from data sources at regular inte…
…rvals (#6) * Feat: auto update * Chore: rename import package name * Feat: log display on data update
- Loading branch information
Showing
14 changed files
with
242 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package cron | ||
|
||
import ( | ||
"github.com/lezi-wiki/lezi-api/pkg/text" | ||
"github.com/lezi-wiki/lezi-api/pkg/util" | ||
"github.com/lezi-wiki/lezi-api/services/remote" | ||
) | ||
|
||
func getNewData() { | ||
util.Log().Info("准备从 GitHub 更新数据集") | ||
data, err := remote.GetDataFromGitHub() | ||
if err != nil { | ||
util.Log().Error("更新数据集失败") | ||
return | ||
} | ||
|
||
text.Data = data | ||
util.Log().Debug("数据集已更新:%v", text.Data) | ||
|
||
util.Log().Info("数据集更新完成") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package cron | ||
|
||
import ( | ||
"github.com/lezi-wiki/lezi-api/pkg/util" | ||
"github.com/robfig/cron" | ||
) | ||
|
||
var task *cron.Cron | ||
|
||
func init() { | ||
task = cron.New() | ||
task.Start() | ||
} | ||
|
||
func InitJobs() { | ||
// 每30分钟获取远端数据 | ||
err := task.AddFunc("0 */30 * * *", getNewData) | ||
if err != nil { | ||
util.Log().Error("Cron jobs 错误, %s", err.Error()) | ||
util.Log().Warning("自动更新服务未启动,数据将无法从 GitHub 自动获取!") | ||
return | ||
} else { | ||
util.Log().Info("自动更新服务已启动,数据将从 GitHub 自动获取!") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package http | ||
|
||
import "github.com/idoubi/goz" | ||
|
||
var client *goz.Request | ||
|
||
func init() { | ||
client = goz.NewClient() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package http | ||
|
||
import ( | ||
"errors" | ||
"github.com/idoubi/goz" | ||
) | ||
|
||
const ( | ||
ErrHttp = "http error" | ||
) | ||
|
||
func Get(url string, opt ...goz.Options) ([]byte, error) { | ||
req, err := client.Get(url, opt...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
code := req.GetStatusCode() | ||
if code != 200 { | ||
return nil, errors.New(ErrHttp) | ||
} | ||
|
||
data, err := req.GetBody() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return data, nil | ||
} | ||
|
||
func Post(url string, data interface{}, opts ...goz.Options) ([]byte, error) { | ||
newOpt := []goz.Options{ | ||
{ | ||
JSON: data, | ||
}, | ||
} | ||
for _, option := range opts { | ||
newOpt = append(newOpt, option) | ||
} | ||
|
||
req, err := client.Post(url, newOpt...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := req.GetBody() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return resp, nil | ||
} |
Oops, something went wrong.