Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
53 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
gonews | ||
nohup.out | ||
*.log | ||
*.log | ||
.idea/ |
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,43 @@ | ||
package main | ||
|
||
import ( | ||
"time" | ||
"os/exec" | ||
"fmt" | ||
"os" | ||
"strings" | ||
) | ||
|
||
// 自动获取数据, interval单位为分钟 | ||
func initDataPuller(dir string, interval int64) { | ||
ticker := time.NewTicker(time.Duration(interval) * time.Minute) | ||
go func() { | ||
for { | ||
// 拉取数据 | ||
cmd := exec.Command("git", "pull", "origin", "master") | ||
cmd.Dir = dir | ||
out, err := cmd.Output() | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "%s\n", "Pull failed") | ||
fmt.Fprintf(os.Stderr, "%s\n", err) | ||
fmt.Fprintf(os.Stderr, "%s\n", out) | ||
} else { | ||
if strings.Contains(string(out),"Already up to date") { | ||
continue | ||
} | ||
fmt.Fprintf(os.Stdout, "%s\n", "Pull success") | ||
|
||
// 缓存数据操作 | ||
files := getFileList(dir) | ||
for _, file := range files { | ||
wg.Add(1) | ||
go cacheNews(file) | ||
} | ||
wg.Wait() | ||
fmt.Fprintf(os.Stdout, "%s\n", "Success to cache news") | ||
} | ||
// 定时器 | ||
<-ticker.C | ||
} | ||
}() | ||
} |