CasaOS/service/task.go
link d4bed3e5c7
Dev (#261)
* fix bug

* updata UI

* 0.3.2

### Added

- [Files] Files can now be selected multiple files and downloaded, deleted, moved, etc.
- [Apps] Support to modify the application opening address.([#204](https://github.com/IceWhaleTech/CasaOS/issues/204))

### Changed

- [Apps] Hide the display of non-essential environment variables in the application.
- [System] Network, disk, cpu, memory, etc. information is modified to be pushed via socket.
- [System] Optimize opening speed.([#214](https://github.com/IceWhaleTech/CasaOS/issues/214))
### Fixed

- [System] Fixed the problem that sync data cannot submit the device ID ([#68](https://github.com/IceWhaleTech/CasaOS/issues/68))
- [Files] Fixed the code editor center alignment display problem.([#210](https://github.com/IceWhaleTech/CasaOS/issues/210))
- [Files] Fixed the problem of wrong name when downloading files.([#240](https://github.com/IceWhaleTech/CasaOS/issues/240))
- [System] Fixed the network display as a negative number problem.([#224](https://github.com/IceWhaleTech/CasaOS/issues/224))

* Modify log help class

* Fix some bugs in 0.3.2

* Solve the operation file queue problem

* Exclude web folders

* update UI
2022-06-08 18:19:45 +08:00

145 lines
3.3 KiB
Go

package service
import (
json2 "encoding/json"
"strconv"
"github.com/IceWhaleTech/CasaOS/pkg/config"
httper2 "github.com/IceWhaleTech/CasaOS/pkg/utils/httper"
"github.com/IceWhaleTech/CasaOS/service/model"
"github.com/IceWhaleTech/CasaOS/types"
"github.com/tidwall/gjson"
"gorm.io/gorm"
)
type TaskService interface {
List(desc bool) []model.TaskDBModel
Delete(id string)
Add(m *model.TaskDBModel)
Update(m *model.TaskDBModel)
Info(id string) model.TaskDBModel
SyncTaskService()
GetServerTasks() []model.TaskDBModel
}
type taskService struct {
db *gorm.DB
}
func (s *taskService) List(desc bool) []model.TaskDBModel {
var list []model.TaskDBModel
var orderBy string
if !desc {
orderBy = "id"
} else {
orderBy = "id DESC"
}
s.db.Order(orderBy).Where("state=?", types.TASK_STATE_UNCOMPLETE).Find(&list)
return list
}
func (s *taskService) Delete(id string) {
var m model.TaskDBModel
s.db.Where("id = ?", id).Delete(&m)
}
func (s *taskService) Add(m *model.TaskDBModel) {
s.db.Save(m)
}
func (s *taskService) Update(m *model.TaskDBModel) {
s.db.Model(&m).Update("state", m.State)
}
func (s *taskService) taskDirService(id string) model.TaskDBModel {
var m model.TaskDBModel
s.db.Where("id = ?", id).First(&m)
return m
}
func (s *taskService) Info(id string) model.TaskDBModel {
var m model.TaskDBModel
s.db.Where("id = ?", id).Delete(&m)
return m
}
func (s *taskService) GetServerTasks() []model.TaskDBModel {
var count int64
s.db.Model(&model.TaskDBModel{}).Count(&count)
head := make(map[string]string)
t := make(chan string)
go func() {
str := httper2.Get(config.ServerInfo.ServerApi+"/token", nil)
t <- gjson.Get(str, "data").String()
}()
head["Authorization"] = <-t
listS := httper2.Get(config.ServerInfo.ServerApi+"/v1/task/list/0?desc=true", head)
list := []model.TaskDBModel{}
json2.Unmarshal([]byte(gjson.Get(listS, "data").String()), &list)
//go func(list []model.TaskDBModel) {
// for _, dbModel := range list {
// dbModel.Id = 0
// s.db.Create(&dbModel)
// }
//}(list)
return list
}
func (s *taskService) SyncTaskService() {
var count int64
s.db.Model(&model.TaskDBModel{}).Count(&count)
head := make(map[string]string)
t := make(chan string)
go func() {
str := httper2.Get(config.ServerInfo.ServerApi+"/token", nil)
t <- gjson.Get(str, "data").String()
}()
head["Authorization"] = <-t
listS := httper2.Get(config.ServerInfo.ServerApi+"/v1/task/list/"+strconv.Itoa(int(count)), head)
list := []model.TaskDBModel{}
json2.Unmarshal([]byte(gjson.Get(listS, "data").String()), &list)
go func(list []model.TaskDBModel) {
for _, dbModel := range list {
dbModel.Id = 0
s.db.Create(&dbModel)
}
}(list)
}
func SyncTask(db *gorm.DB) {
var count int64
db.Model(&model.TaskDBModel{}).Count(&count)
head := make(map[string]string)
t := make(chan string)
go func() {
str := httper2.Get(config.ServerInfo.ServerApi+"/token", nil)
t <- gjson.Get(str, "data").String()
}()
head["Authorization"] = <-t
listS := httper2.Get(config.ServerInfo.ServerApi+"/v1/task/list/"+strconv.Itoa(int(count)), head)
list := []model.TaskDBModel{}
json2.Unmarshal([]byte(gjson.Get(listS, "data").String()), &list)
go func(list []model.TaskDBModel) {
for _, dbModel := range list {
dbModel.Id = 0
db.Create(&dbModel)
}
}(list)
}
func NewTaskService(db *gorm.DB) TaskService {
return &taskService{db: db}
}