MCPcopy Index your code
hub / github.com/TruthHun/BookStack / CheckAndGenerateEbook

Method CheckAndGenerateEbook

models/ebook.go:151–182  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

149}
150
151func (m *Ebook) CheckAndGenerateEbook() {
152 if convert2ebookRunning {
153 return
154 }
155 convert2ebookRunning = true
156 o := orm.NewOrm()
157 o.QueryTable(m).Filter("book_id__gt", 0).Filter("status", EBookStatusProccessing).Update(orm.Params{"status": EBookStatusPending})
158 cpuNum := runtime.NumCPU()/2 + 1
159 if cpuNum > 1 && runtime.NumCPU() == cpuNum { // 比如双核服务器,不能直接占用双核,以避免服务器无法正常提供服务
160 cpuNum = cpuNum - 1
161 }
162 sleep := 5 * time.Second
163 handling := make(chan bool, cpuNum)
164 for {
165 for len(handling) < cpuNum {
166 var ebook Ebook
167 o.QueryTable(m).Filter("book_id__gt", 0).Filter("status", EBookStatusPending).OrderBy("id").One(&ebook)
168 if ebook.Id > 0 {
169 handling <- true
170 o.QueryTable(m).Filter("book_id", ebook.BookID).Filter("status", EBookStatusPending).Update(orm.Params{"status": EBookStatusProccessing})
171 go func(bookId int) {
172 m.generate(bookId)
173 <-handling
174 }(ebook.BookID)
175 } else {
176 time.Sleep(sleep)
177 }
178 }
179 time.Sleep(sleep)
180 }
181
182}
183
184//离线文档生成
185func (m *Ebook) generate(bookID int) {

Callers 1

InitFunction · 0.80

Calls 2

generateMethod · 0.95
UpdateMethod · 0.45

Tested by

no test coverage detected