feat: activity log producers
This commit is contained in:
1 parent
50b1649965
commit
e37a46a027
93 files changed
+3396
-440
No files matched your search
@@ -1,7 +1,9 @@
|
||||
package useCases
|
||||
|
||||
import (
|
||||
"RenewCMS/internal/domain/audit"
|
||||
"RenewCMS/internal/domain/update"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
@@ -24,6 +26,7 @@ type StartUpdateDownloadUseCase struct {
|
||||
platform update.Platform
|
||||
feedHost string
|
||||
now func() time.Time
|
||||
recorder audit.Recorder
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
@@ -43,6 +46,7 @@ type StartUpdateDownloadDependencies struct {
|
||||
Platform update.Platform
|
||||
FeedURL string
|
||||
Now func() time.Time
|
||||
Recorder audit.Recorder
|
||||
}
|
||||
|
||||
type jobFailure struct {
|
||||
@@ -78,10 +82,19 @@ func NewStartUpdateDownloadUseCase(dependencies StartUpdateDownloadDependencies)
|
||||
platform: dependencies.Platform,
|
||||
feedHost: update.HostOf(dependencies.FeedURL),
|
||||
now: dependencies.Now,
|
||||
recorder: dependencies.Recorder,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StartUpdateDownloadUseCase) Start() (update.UpdateStatus, error) {
|
||||
func (s *StartUpdateDownloadUseCase) Start(ctx context.Context) (update.UpdateStatus, error) {
|
||||
status, err := s.start(ctx)
|
||||
if err != nil {
|
||||
recordFailure(ctx, s.recorder, audit.NewEvent(audit.ActionUpdateDownloaded, audit.CategorySystem), err)
|
||||
}
|
||||
return status, err
|
||||
}
|
||||
|
||||
func (s *StartUpdateDownloadUseCase) start(ctx context.Context) (update.UpdateStatus, error) {
|
||||
if !s.helper.Installed() {
|
||||
return update.UpdateStatus{}, update.ErrNotPrepared
|
||||
}
|
||||
@@ -118,7 +131,7 @@ func (s *StartUpdateDownloadUseCase) Start() (update.UpdateStatus, error) {
|
||||
return update.UpdateStatus{}, err
|
||||
}
|
||||
status := s.status.build(true)
|
||||
go s.run(job, binary, checksums, signature)
|
||||
go s.run(context.WithoutCancel(ctx), job, binary, checksums, signature)
|
||||
|
||||
return status, nil
|
||||
}
|
||||
@@ -139,15 +152,19 @@ func (s *StartUpdateDownloadUseCase) preflight(result update.CheckResult) (binar
|
||||
return found[0], found[1], found[2], nil
|
||||
}
|
||||
|
||||
func (s *StartUpdateDownloadUseCase) run(job update.Job, binary, checksums, signature update.Asset) {
|
||||
func (s *StartUpdateDownloadUseCase) run(ctx context.Context, job update.Job, binary, checksums, signature update.Asset) {
|
||||
defer func() {
|
||||
if recovered := recover(); recovered != nil {
|
||||
s.fail(job, downloadFailure(fmt.Errorf("panic: %v", recovered)))
|
||||
s.fail(ctx, job, downloadFailure(fmt.Errorf("panic: %v", recovered)))
|
||||
}
|
||||
}()
|
||||
if err := s.execute(&job, binary, checksums, signature); err != nil {
|
||||
s.fail(job, err)
|
||||
s.fail(ctx, job, err)
|
||||
return
|
||||
}
|
||||
event := audit.NewEvent(audit.ActionUpdateDownloaded, audit.CategorySystem)
|
||||
event.Target = audit.ReleaseTarget(job.Tag)
|
||||
s.recorder.Record(ctx, event)
|
||||
}
|
||||
|
||||
func (s *StartUpdateDownloadUseCase) execute(job *update.Job, binary, checksums, signature update.Asset) error {
|
||||
@@ -263,7 +280,7 @@ func downloadFailure(err error) error {
|
||||
return jobFailure{kind: update.ErrorKindDownload, reason: err}
|
||||
}
|
||||
|
||||
func (s *StartUpdateDownloadUseCase) fail(job update.Job, err error) {
|
||||
func (s *StartUpdateDownloadUseCase) fail(ctx context.Context, job update.Job, err error) {
|
||||
kind := update.ErrorKindDownload
|
||||
var failure jobFailure
|
||||
if errors.As(err, &failure) {
|
||||
@@ -279,4 +296,7 @@ func (s *StartUpdateDownloadUseCase) fail(job update.Job, err error) {
|
||||
if saveErr := s.jobs.Save(job); saveErr != nil {
|
||||
s.logger.Printf("update job record could not be saved: %v", saveErr)
|
||||
}
|
||||
event := audit.NewEvent(audit.ActionUpdateDownloaded, audit.CategorySystem)
|
||||
event.Target = audit.ReleaseTarget(job.Tag)
|
||||
failFrom(ctx, s.recorder, event, downloadFailureReasons[kind])
|
||||
}
|
||||
Reference in new issue
Block a user