Skip to content

Commit

Permalink
timer ticker support for timer works
Browse files Browse the repository at this point in the history
  • Loading branch information
fuxiaohei committed Feb 20, 2014
1 parent 274dbc9 commit c24390c
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 34 deletions.
5 changes: 2 additions & 3 deletions app/cmd/backup.go
Expand Up @@ -92,14 +92,13 @@ func GetBackupFiles() ([]os.FileInfo, error) {

// StartBackupTimer starts backup operation timer for auto backup stuff.
func StartBackupTimer(app *GoInk.App, t int) {
println("backup files after", t, "hours")
time.AfterFunc(time.Duration(t)*time.Hour, func() {
model.SetTimerFunc("backup-data", 144, func() {
filename, e := DoBackup(app, true)
if e != nil {
model.CreateMessage("backup", "[0]"+e.Error())
} else {
model.CreateMessage("backup", "[1]"+filename)
}
StartBackupTimer(app, t)
println("backup files in", t, "hours")
})
}
7 changes: 2 additions & 5 deletions app/model/comment.go
Expand Up @@ -5,7 +5,6 @@ import (
"html/template"
"sort"
"strings"
"time"
)

var (
Expand Down Expand Up @@ -315,11 +314,9 @@ func RecycleComments() {
SyncContents()
}

// StartCommentsTimers starts a timer to recycle comments.
func StartCommentsTimer() {
time.AfterFunc(time.Duration(6)*time.Hour, func() {
func startCommentsTimer() {
SetTimerFunc("comment-recycle", 36, func() {
println("recycle comments in 6 hours timer")
RecycleComments()
StartCommentsTimer()
})
}
17 changes: 4 additions & 13 deletions app/model/content.go
Expand Up @@ -9,7 +9,6 @@ import (
"path/filepath"
"sort"
"strings"
"time"
)

var (
Expand Down Expand Up @@ -301,7 +300,9 @@ func LoadContents() {
sort.Sort(sort.Reverse(sort.IntSlice(pageIndex)))
contentsIndex["article"] = articleIndex
contentsIndex["page"] = pageIndex
// generate indexes
generatePublishArticleIndex()
generateContentTmpIndexes()
}

func generatePublishArticleIndex() {
Expand All @@ -316,10 +317,9 @@ func generatePublishArticleIndex() {
}

func startContentSyncTimer() {
time.AfterFunc(time.Duration(1)*time.Hour, func() {
SetTimerFunc("content-sync", 6, func() {
println("write contents in 1 hour timer")
SyncContents()
startContentSyncTimer()
})
}

Expand Down Expand Up @@ -405,17 +405,8 @@ func GetTaggedArticleList(tag string, page, size int) ([]*Content, *utils.Pager)
}

func startContentTmpIndexesTimer() {
time.AfterFunc(time.Duration(6)*time.Hour, func() {
SetTimerFunc("content-indexes", 36, func() {
println("write content indexes in 6 hours timer")
generateContentTmpIndexes()
startContentTmpIndexesTimer()
})
}

// StartContentsTimer starts some timers for contents.
// An hour timer is for sync all contents because their hits number are changing.
func StartContentsTimer() {
startContentSyncTimer()
generateContentTmpIndexes()
startContentTmpIndexesTimer()
}
6 changes: 2 additions & 4 deletions app/model/message.go
Expand Up @@ -3,7 +3,6 @@ package model
import (
"github.com/fuxiaohei/GoBlog/app/utils"
"strings"
"time"
)

var (
Expand Down Expand Up @@ -144,11 +143,10 @@ func generateBackupMessage(co interface{}) string {
return "备份全站到 " + strings.TrimPrefix(str, "[1]") + " 成功."
}

func StartMessageTimer() {
time.AfterFunc(time.Duration(90)*time.Minute, func() {
func startMessageTimer() {
SetTimerFunc("message-sync", 9, func() {
println("write messages in 1.5 hour timer")
RecycleMessages()
SyncMessages()
StartMessageTimer()
})
}
14 changes: 5 additions & 9 deletions app/model/storage.go
Expand Up @@ -12,7 +12,7 @@ import (
var (
appVersion int
// global data storage instance
Storage *jsonStorage
Storage *jsonStorage
// global tmp data storage instance. Temp data are generated for special usages, will not backup.
TmpStorage *jsonStorage
)
Expand Down Expand Up @@ -220,8 +220,8 @@ func writeDefaultData() {
writeDefaultTmpData()
}

func writeDefaultTmpData(){
TmpStorage.Set("contents",make(map[string][]int))
func writeDefaultTmpData() {
TmpStorage.Set("contents", make(map[string][]int))
}

func loadAllData() {
Expand Down Expand Up @@ -257,12 +257,8 @@ func Init(v int) {
// Start timers for content, comment and message.
func All() {
loadAllData()
// content timer, save visit hits and comment numbers
StartContentsTimer()
// comment timer, recycle invalid comments whose parents or contents are removed
StartCommentsTimer()
// message timer, recycle old messages and save read status
StartMessageTimer()
// start model timer, do all timer stuffs
StartModelTimer()
}

// SyncAll writes all current memory data to storage files.
Expand Down
74 changes: 74 additions & 0 deletions app/model/timer.go
@@ -0,0 +1,74 @@
package model

import (
"time"
)

type timerFunc struct {
Fn func()
Ticker int
}

var (
timerCount int
timerFuncs map[string]*timerFunc
)

func init() {
timerCount = 0
timerFuncs = make(map[string]*timerFunc)
}

// SetTimerFunc adds timer func for time ticker.
// Ticker means step time, after ticker size step passed, do function.
// Name is unique name of func.If set same name func, use the last one.
func SetTimerFunc(name string, ticker int, fn func()) {
tfn := new(timerFunc)
tfn.Fn = fn
tfn.Ticker = ticker
timerFuncs[name] = tfn
}

// ChangeTimerFunc can change timer func by given name.
// If the func of name is none, do not change anything, print error message.
func ChangeTimerFunc(name string, ticker int, fn func()) {
if _, ok := timerFuncs[name]; ok {
timerFuncs[name].Fn = fn
timerFuncs[name].Ticker = ticker
} else {
println("change invalid timer func : " + name)
}
}

// DelTimerFunc deletes timer func.
func DelTimerFunc(name string) {
delete(timerFuncs, name)
}

// StartModelTimer adds models' timer and starts time ticker.
// The default step is 10 min once.
func StartModelTimer() {
// start all timers
startCommentsTimer()
startContentSyncTimer()
startContentTmpIndexesTimer()
startMessageTimer()
// start time ticker
ticker := time.NewTicker(time.Duration(10) * time.Minute)
go doTimers(ticker.C)
}

func doTimers(c <-chan time.Time) {
for {
<-c
timerCount++
for _, tfn := range timerFuncs {
if timerCount%tfn.Ticker == 0 {
tfn.Fn()
}
}
if timerCount > 999 {
timerCount = 0
}
}
}

0 comments on commit c24390c

Please sign in to comment.