-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
cleanup_app.go
72 lines (56 loc) · 1.83 KB
/
cleanup_app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package cleanup_app
import (
"context"
"database/sql/driver"
"errors"
"github.com/YuukanOO/seelf/internal/deployment/domain"
"github.com/YuukanOO/seelf/pkg/apperr"
"github.com/YuukanOO/seelf/pkg/bus"
"github.com/YuukanOO/seelf/pkg/storage"
)
// Cleanup an application artifacts, images, networks, volumes and so on...
type Command struct {
bus.Command[bus.UnitType]
ID string `json:"id"`
}
func (Command) Name_() string { return "deployment.command.cleanup_app" }
func (c Command) Value() (driver.Value, error) { return storage.ValueJSON(c) }
func init() {
bus.Marshallable.Register(Command{}, func(s string) (bus.Request, error) { return storage.UnmarshalJSON[Command](s) })
}
func Handler(
deploymentsReader domain.DeploymentsReader,
reader domain.AppsReader,
writer domain.AppsWriter,
artifactManager domain.ArtifactManager,
backend domain.Backend,
) bus.RequestHandler[bus.UnitType, Command] {
return func(ctx context.Context, cmd Command) (bus.UnitType, error) {
app, err := reader.GetByID(ctx, domain.AppID(cmd.ID))
if err != nil {
// If the application doesn't exist anymore, may be it has been processed by another job in rare case, so just returns
if errors.Is(err, apperr.ErrNotFound) {
return bus.Unit, nil
}
return bus.Unit, err
}
count, err := deploymentsReader.GetRunningOrPendingDeploymentsCount(ctx, app.ID())
if err != nil {
return bus.Unit, err
}
// Before calling the backend cleanup, make sure the application can be safely deleted.
if err = app.Delete(count); err != nil {
return bus.Unit, err
}
if err = backend.Cleanup(ctx, app); err != nil {
return bus.Unit, err
}
if err = artifactManager.Cleanup(ctx, app); err != nil {
return bus.Unit, err
}
if err = writer.Write(ctx, &app); err != nil {
return bus.Unit, err
}
return bus.Unit, nil
}
}