Skip to content

Commit

Permalink
logrotate: don't exit at non-running instance
Browse files Browse the repository at this point in the history
Just warn that the instance is not running and continue (like `tt stop`
or `tt kill` do)

Closes #774
  • Loading branch information
elhimov committed Nov 14, 2024
1 parent e31afa3 commit ce2c8dc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
5 changes: 2 additions & 3 deletions cli/cmd/logrotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ func internalLogrotateModule(cmdCtx *cmdcontext.CmdCtx, args []string) error {
}

for _, run := range runningCtx.Instances {
res, err := running.Logrotate(&run)
err := running.Logrotate(&run)
if err != nil {
return err
log.Infof(err.Error())
}
log.Info(res)
}

return nil
Expand Down
15 changes: 9 additions & 6 deletions cli/running/running.go
Original file line number Diff line number Diff line change
Expand Up @@ -858,24 +858,27 @@ func Status(run *InstanceCtx) process_utils.ProcessState {
}

// Logrotate rotates logs of a started tarantool instance.
func Logrotate(run *InstanceCtx) (string, error) {
func Logrotate(run *InstanceCtx) error {
fullInstanceName := GetAppInstanceName(*run)

pid, err := process_utils.GetPIDFromFile(run.PIDFile)
if err != nil {
return "", errors.New(instStateStopped.String())
return fmt.Errorf("%s: the instance is not running, it must be started", fullInstanceName)
}

alive, err := process_utils.IsProcessAlive(pid)
if !alive {
return "", errors.New(instStateDead.String())
return errors.New(instStateDead.String())
}

if err := syscall.Kill(pid, syscall.Signal(syscall.SIGHUP)); err != nil {
return "", fmt.Errorf(`can't rotate logs: "%v"`, err)
return fmt.Errorf(`can't rotate logs: "%v"`, err)
}

// Rotates logs [instance name pid]
fullInstanceName := GetAppInstanceName(*run)
return fmt.Sprintf("%s: logs has been rotated. PID: %v.", fullInstanceName, pid), nil
log.Infof("%s (PID = %v): logs has been rotated.", fullInstanceName, pid)

return nil
}

// Check returns the result of checking the syntax of the application file.
Expand Down
25 changes: 10 additions & 15 deletions test/integration/logrotate/test_logrotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,26 @@ def post_start(tt_app, instances):

# Do logrotate.
rc, out = tt_app.logrotate(target)

# If any of the requested instances is not running return failure.
for inst in tt_app.instances_of(target):
if not tt_app.is_instance_of(inst, *running_targets):
assert rc != 0
assert "NOT RUNNING" in out
return
assert rc == 0

# Wait for the log files to be re-created.
tt_app_wait_log_files(tt_app, tt_app.instances_of(target))

# Check the instances.
assert rc == 0
status = tt_app.status()
for inst in tt_app.instances:
inst_id = tt_app.inst_id(inst)
was_running = tt_app.is_instance_of(inst, *running_targets)
if tt_app.is_instance_of(inst, target):
assert was_running
assert status[inst_id]["STATUS"] == "RUNNING"
assert status[inst_id]["PID"] == orig_status[inst_id]["PID"]
pid = status[inst_id]["PID"]
assert f"{inst_id}: logs has been rotated. PID: {pid}" in out
with open(tt_app.log_path(inst, log_file)) as f:
assert "reopened" in f.read()
if was_running:
assert status[inst_id]["STATUS"] == "RUNNING"
pid = status[inst_id]["PID"]
assert pid == orig_status[inst_id]["PID"]
assert f"{inst_id} (PID = {pid}): logs has been rotated." in out
with open(tt_app.log_path(inst, log_file)) as f:
assert "reopened" in f.read()
else:
assert f"{inst_id}: the instance is not running, it must be started" in out
else:
assert inst_id not in out
assert status[inst_id]["STATUS"] == orig_status[inst_id]["STATUS"]
Expand Down

0 comments on commit ce2c8dc

Please sign in to comment.