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"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@@ -13,10 +15,11 @@ type RequestRestartUseCase struct {
|
||||
requests update.RestartRequestStore
|
||||
status updateStatusBuilder
|
||||
now func() time.Time
|
||||
recorder audit.Recorder
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func NewRequestRestartUseCase(store update.StatusStore, jobs update.JobStore, helper update.HelperDetector, results update.ResultStore, requests update.RestartRequestStore, currentVersion string, now func() time.Time) *RequestRestartUseCase {
|
||||
func NewRequestRestartUseCase(store update.StatusStore, jobs update.JobStore, helper update.HelperDetector, results update.ResultStore, requests update.RestartRequestStore, currentVersion string, now func() time.Time, recorder audit.Recorder) *RequestRestartUseCase {
|
||||
return &RequestRestartUseCase{
|
||||
jobs: jobs,
|
||||
helper: helper,
|
||||
@@ -24,12 +27,33 @@ func NewRequestRestartUseCase(store update.StatusStore, jobs update.JobStore, he
|
||||
requests: requests,
|
||||
status: updateStatusBuilder{store: store, jobs: jobs, results: results, requests: requests, currentVersion: currentVersion, now: now},
|
||||
now: now,
|
||||
recorder: recorder,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RequestRestartUseCase) Request(userId uint32, kind string) (update.UpdateStatus, error) {
|
||||
func (r *RequestRestartUseCase) Request(ctx context.Context, userId uint32, kind string) (update.UpdateStatus, error) {
|
||||
event := audit.NewEvent(audit.ActionRestartRequested, audit.CategorySystem)
|
||||
status, request, err := r.request(userId, kind)
|
||||
if err != nil {
|
||||
recordFailure(ctx, r.recorder, event, err, restartFailureReasons...)
|
||||
return update.UpdateStatus{}, err
|
||||
}
|
||||
event.Params = restartParams(request)
|
||||
r.recorder.Record(ctx, event)
|
||||
return status, nil
|
||||
}
|
||||
|
||||
func restartParams(request update.RestartRequest) map[string]any {
|
||||
params := map[string]any{audit.ParamKind: request.Kind}
|
||||
if request.Kind == update.RestartKindUpdate {
|
||||
params[audit.ParamTag] = request.Tag
|
||||
}
|
||||
return params
|
||||
}
|
||||
|
||||
func (r *RequestRestartUseCase) request(userId uint32, kind string) (update.UpdateStatus, update.RestartRequest, error) {
|
||||
if !r.helper.Installed() {
|
||||
return update.UpdateStatus{}, update.ErrNotPrepared
|
||||
return update.UpdateStatus{}, update.RestartRequest{}, update.ErrNotPrepared
|
||||
}
|
||||
|
||||
r.mu.Lock()
|
||||
@@ -37,19 +61,19 @@ func (r *RequestRestartUseCase) Request(userId uint32, kind string) (update.Upda
|
||||
|
||||
current := r.status.build(true)
|
||||
if err := refuseWhileBusy(current); err != nil {
|
||||
return update.UpdateStatus{}, err
|
||||
return update.UpdateStatus{}, update.RestartRequest{}, err
|
||||
}
|
||||
request, err := r.requestFor(kind, userId)
|
||||
if err != nil {
|
||||
return update.UpdateStatus{}, err
|
||||
return update.UpdateStatus{}, update.RestartRequest{}, err
|
||||
}
|
||||
if err := r.results.Delete(); err != nil {
|
||||
return update.UpdateStatus{}, err
|
||||
return update.UpdateStatus{}, update.RestartRequest{}, err
|
||||
}
|
||||
if err := r.requests.Write(request); err != nil {
|
||||
return update.UpdateStatus{}, err
|
||||
return update.UpdateStatus{}, update.RestartRequest{}, err
|
||||
}
|
||||
return r.status.build(true), nil
|
||||
return r.status.build(true), request, nil
|
||||
}
|
||||
|
||||
func refuseWhileBusy(status update.UpdateStatus) error {
|
||||
|
||||
Reference in new issue
Block a user