mirror of
https://github.com/ZSCNetSupportDept/scheduler.git
synced 2025-10-28 20:45:05 +08:00
为signals加上了锁来确保线程安全
This commit is contained in:
@@ -16,13 +16,15 @@ var data *[7][]string
|
||||
var err error
|
||||
|
||||
func GetAssignment(i echo.Context) error {
|
||||
if (carbon.Now().ToDateString() != signals.Table.LastUpdated.ToDateString()) || signals.Table.NeedUpdate == true {
|
||||
if (carbon.Now().ToDateString() != signals.Table.GetLastUpdated().ToDateString()) || signals.Table.IsNeedUpdate() == true {
|
||||
fmt.Printf("At %v:start regenerate table", carbon.Now())
|
||||
data, err = generateTable()
|
||||
if err != nil {
|
||||
i.String(http.StatusInternalServerError, err.Error())
|
||||
return echo.ErrInternalServerError
|
||||
}
|
||||
//signals.Table.SetUpdated(carbon.Now())
|
||||
//测试时注释掉上面的状态更新方便调试
|
||||
}
|
||||
i.Render(http.StatusOK, "table.html", data)
|
||||
return nil
|
||||
|
||||
@@ -2,11 +2,64 @@ package signals
|
||||
|
||||
import (
|
||||
"github.com/golang-module/carbon/v2"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// 有关值班表相关的状态
|
||||
type TablePrototype struct {
|
||||
LastUpdated carbon.Carbon //值班表最后更新的时间
|
||||
NeedUpdate bool //值班表是否需要更新
|
||||
lastUpdated carbon.Carbon //值班表最后更新的时间,不要直接读取这个变量
|
||||
LUMutex sync.RWMutex
|
||||
|
||||
needUpdate bool //值班表是否需要更新,不要直接读取这个变量
|
||||
NUMutex sync.RWMutex
|
||||
}
|
||||
|
||||
var Table TablePrototype
|
||||
|
||||
// 下面是操作这些状态的专用函数,用且仅用它们来操作上面的全局变量
|
||||
|
||||
// 表格最后的更新时间
|
||||
func (t *TablePrototype) GetLastUpdated() carbon.Carbon {
|
||||
t.LUMutex.RLock()
|
||||
defer t.LUMutex.RUnlock()
|
||||
return t.lastUpdated
|
||||
|
||||
}
|
||||
|
||||
// 标记最后更新的时间
|
||||
func (t *TablePrototype) MarkUpdateTime(newtime carbon.Carbon) error {
|
||||
t.LUMutex.Lock()
|
||||
defer t.LUMutex.Unlock()
|
||||
t.lastUpdated = newtime
|
||||
return nil
|
||||
}
|
||||
|
||||
// 表格需要更新吗?
|
||||
func (t *TablePrototype) IsNeedUpdate() bool {
|
||||
t.NUMutex.RLock()
|
||||
defer t.NUMutex.RUnlock()
|
||||
return t.needUpdate
|
||||
|
||||
}
|
||||
|
||||
// 标记表格要求更新吗
|
||||
func (t *TablePrototype) MarkUpdateStatus(i bool) error {
|
||||
t.NUMutex.Lock()
|
||||
defer t.NUMutex.Unlock()
|
||||
t.needUpdate = i
|
||||
return nil
|
||||
}
|
||||
|
||||
// 标记表格更新,是这两个函数的wrapper,一般用这个
|
||||
func (t *TablePrototype) SetUpdated(newtime carbon.Carbon) error {
|
||||
err := t.MarkUpdateTime(newtime)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = t.MarkUpdateStatus(false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user