aboutsummaryrefslogtreecommitdiff
path: root/internal/engine/session.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/engine/session.go')
-rw-r--r--internal/engine/session.go36
1 files changed, 26 insertions, 10 deletions
diff --git a/internal/engine/session.go b/internal/engine/session.go
index 8d52027..33b6a76 100644
--- a/internal/engine/session.go
+++ b/internal/engine/session.go
@@ -28,22 +28,32 @@ type Session struct {
dry bool
}
-// NewSession starts a run. A real one opens the log; the caller closes the
-// session when the run is over.
+// NewSession starts a run. The log is opened by OpenLog, or by the first
+// Apply or ApplyUndo: sorting opens it before it plans anything, while undo
+// opens it only once it knows it will reverse something, and neither must
+// create a log a dry run would not. The caller closes the session when the
+// run is over.
func (e *Engine) NewSession(dry bool) (*Session, error) {
- s := &Session{e: e, claims: plan.NewClaims(), dry: dry}
- if dry {
- return s, nil
+ return &Session{e: e, claims: plan.NewClaims(), dry: dry}, nil
+}
+
+// OpenLog opens the log and takes the run id, once. A dry session does
+// neither: journal.Open creates the state directory and an empty krino.log
+// merely by being called (spec §11).
+func (s *Session) OpenLog() error {
+ if s.dry || s.j != nil {
+ return nil
}
- j, err := journal.Open(e.Config.LogFile())
+ j, err := journal.Open(s.e.Config.LogFile())
if err != nil {
- return nil, err
+ return err
}
- s.j, s.run = j, journal.NewRunID(e.Now())
- return s, nil
+ s.j, s.run = j, journal.NewRunID(s.e.Now())
+ return nil
}
-// Run is the run id every entry of this session carries; "" for a dry one.
+// Run is the run id every entry of this session carries; "" for a dry one,
+// and until the log is open.
func (s *Session) Run() string { return s.run }
// Journal is the log this session writes, nil for a dry one.
@@ -87,6 +97,9 @@ func (s *Session) Plan(ctx context.Context, d *Dir) (*DirPlan, error) {
// overwrite) from displacing this run's own result (spec §7.4). A dry
// session keeps every claim, since it applies nothing.
func (s *Session) Apply(ctx context.Context, dp *DirPlan, approved map[string]bool) (*ApplyResult, error) {
+ if err := s.OpenLog(); err != nil {
+ return nil, err
+ }
res, err := s.e.Apply(ctx, dp, approved, s.j, s.run)
if !s.dry {
s.claims = plan.NewClaims()
@@ -133,6 +146,9 @@ func (s *Session) PlanUndo(runID string) (*UndoPlan, error) {
// ApplyUndo carries out up and logs it under this session's run id.
func (s *Session) ApplyUndo(ctx context.Context, up *UndoPlan) (*ApplyResult, error) {
+ if err := s.OpenLog(); err != nil {
+ return nil, err
+ }
return s.e.ApplyUndo(ctx, up, s.j, s.run)
}