Skip to content

Commit

Permalink
fix: fix status is empty (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
CorrectRoadH authored May 10, 2024
1 parent 70b66ec commit 7ad5845
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 33 deletions.
2 changes: 1 addition & 1 deletion common/fixtures/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/IceWhaleTech/CasaOS-Installer/service"
)

func WaitFecthReleaseCompeleted(s *service.StatusService) {
func WaitFetchReleaseCompeleted(s *service.StatusService) {
time.Sleep(service.GetReleaseCostTime + 500*time.Millisecond)
}

Expand Down
18 changes: 6 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"net/http"
"os"
"path/filepath"
"sync"
"time"

"github.com/IceWhaleTech/CasaOS-Common/model"
Expand Down Expand Up @@ -94,13 +93,12 @@ func init() {
}

func main() {
service.InstallerService = &service.StatusService{
ImplementService: service.NewInstallerService(sysRoot),
SysRoot: sysRoot,
Have_other_get_release_flag_lock: sync.RWMutex{},
}
service.InstallerService = service.NewStatusService(service.NewInstallerService(sysRoot), sysRoot)

service.InstallerService.Launch(sysRoot)
err := service.InstallerService.Launch(sysRoot)
if err != nil {
logger.Error("error when trying to launch", zap.Error(err))
}

// watch rauc offline
os.MkdirAll(config.RAUC_OFFLINE_PATH, os.ModePerm)
Expand Down Expand Up @@ -188,11 +186,7 @@ func watchOfflineDir(watcher *fsnotify.Watcher) {
return
}
if event.Has(fsnotify.Remove) || event.Has(fsnotify.Create) {
service.InstallerService = &service.StatusService{
ImplementService: service.NewInstallerService(sysRoot),
SysRoot: sysRoot,
Have_other_get_release_flag_lock: sync.RWMutex{},
}
service.InstallerService = service.NewStatusService(service.NewInstallerService(sysRoot), sysRoot)
}

case err, ok := <-watcher.Errors:
Expand Down
31 changes: 15 additions & 16 deletions service/status_serivce.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ import (
var ErrHaveOtherCron = fmt.Errorf("have other cron")

type StatusService struct {
ImplementService UpdaterServiceInterface
SysRoot string
Have_other_get_release_flag_lock sync.RWMutex
ImplementService UpdaterServiceInterface
SysRoot string

status codegen.Status
message string
Expand Down Expand Up @@ -93,13 +92,14 @@ func (r *StatusService) InitEventTypeMapStatus() {
r.EventTypeMapMessageType[InstallBegin] = common.EventTypeInstallUpdateBegin
r.EventTypeMapMessageType[InstallEnd] = common.EventTypeInstallUpdateEnd
r.EventTypeMapMessageType[InstallError] = common.EventTypeInstallUpdateError

logger.Info("status init success")
}

func NewStatusService(implementService UpdaterServiceInterface, sysRoot string) *StatusService {
statusService := &StatusService{
ImplementService: implementService,
SysRoot: sysRoot,
Have_other_get_release_flag_lock: sync.RWMutex{},
ImplementService: implementService,
SysRoot: sysRoot,
}
statusService.InitEventTypeMapStatus()
statusService.status = codegen.Status{
Expand All @@ -114,13 +114,10 @@ func (r *StatusService) GetStatus() (codegen.Status, string) {
return r.status, r.message
}

func (r *StatusService) UpdateStatusWithMessage(eventType EventType, newPackageStatus string) {
func (r *StatusService) UpdateStatusWithMessage(eventType EventType, eventMessage string) {
r.lock.Lock()
defer r.lock.Unlock()
if (eventType != InstallEnd && eventType != InstallError && eventType != InstallBegin) && (r.status.Status == codegen.Installing) {
return
}

fmt.Println(r.EventTypeMapStatus)
switch eventType {
case DownloadBegin:
r.status = r.EventTypeMapStatus[DownloadBegin]
Expand All @@ -140,17 +137,21 @@ func (r *StatusService) UpdateStatusWithMessage(eventType EventType, newPackageS
r.status = r.EventTypeMapStatus[InstallEnd]
case InstallError:
r.status = r.EventTypeMapStatus[InstallError]
default:
r.status = codegen.Status{
Status: codegen.Idle,
}
fmt.Println("找不到")
}

r.message = newPackageStatus
r.message = eventMessage

ctx := context.Background()

// 这里怎么map一下?🤔
event := r.EventTypeMapMessageType[eventType]

go PublishEventWrapper(ctx, event, map[string]string{
common.PropertyTypeMessage.Name: newPackageStatus,
common.PropertyTypeMessage.Name: eventMessage,
})
}

Expand Down Expand Up @@ -234,7 +235,6 @@ func (r *StatusService) PostInstall(release codegen.Release, sysRoot string) err
}

func (r *StatusService) ShouldUpgrade(release codegen.Release, sysRoot string) bool {

su := r.ImplementService.ShouldUpgrade(release, sysRoot)
return su
}
Expand All @@ -261,7 +261,6 @@ func (r *StatusService) PostMigration(sysRoot string) error {
}

func (r *StatusService) Cronjob(ctx context.Context, sysRoot string) error {

logger.Info("start a check update job")

ctx = context.WithValue(ctx, types.Trigger, types.CRON_JOB)
Expand Down
8 changes: 4 additions & 4 deletions service/status_serivce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Test_Status_Case1_NotUpdate(t *testing.T) {
assert.Equal(t, codegen.FetchUpdating, value.Status)
assert.Equal(t, types.FETCHING, msg)

fixtures.WaitFecthReleaseCompeleted(statusService)
fixtures.WaitFetchReleaseCompeleted(statusService)
fixtures.WaitDownloadCompeleted(statusService)
value, msg = statusService.GetStatus()
assert.Equal(t, codegen.Idle, value.Status)
Expand Down Expand Up @@ -83,7 +83,7 @@ func Test_Status_Case1_Download_Success(t *testing.T) {
assert.Equal(t, codegen.FetchUpdating, value.Status)
assert.Equal(t, types.FETCHING, msg)

fixtures.WaitFecthReleaseCompeleted(statusService)
fixtures.WaitFetchReleaseCompeleted(statusService)
value, msg = statusService.GetStatus()
assert.Equal(t, codegen.Downloading, value.Status)
assert.Equal(t, types.DOWNLOADING, msg)
Expand Down Expand Up @@ -115,7 +115,7 @@ func Test_Status_Case2_Download_Failed(t *testing.T) {
statusService.Cronjob(context.Background(), sysRoot)
}()

fixtures.WaitFecthReleaseCompeleted(statusService)
fixtures.WaitFetchReleaseCompeleted(statusService)
fixtures.WaitDownloadCompeleted(statusService)

value, msg := statusService.GetStatus()
Expand Down Expand Up @@ -145,7 +145,7 @@ func Test_Status_Case3_Install_Success(t *testing.T) {
statusService.Cronjob(ctx, sysRoot)
}()

fixtures.WaitFecthReleaseCompeleted(statusService)
fixtures.WaitFetchReleaseCompeleted(statusService)
fixtures.WaitDownloadCompeleted(statusService)
value, msg := statusService.GetStatus()
assert.Equal(t, codegen.Idle, value.Status)
Expand Down

0 comments on commit 7ad5845

Please sign in to comment.