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 authored and dmyger committed Dec 6, 2024
1 parent b617bf2 commit f66fb27
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 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 @@ -856,24 +856,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
24 changes: 11 additions & 13 deletions test/integration/logrotate/test_logrotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,29 @@ def check_logrotate(tt, target):

# Do logrotate.
rc, out = tt.exec('logrotate', target)

# If any of the requested instances is not running return failure.
for inst in target_instances:
if inst not in tt.running_instances:
assert rc != 0
assert "NOT RUNNING" in out
return
assert rc == 0

# Wait for the log files to be re-created.
assert utils.wait_files(5, tt_helper.log_files(tt, expected_instances))

# Check the instances.
assert rc == 0
status = tt_helper.status(tt)
for inst in tt.instances:
was_running = inst in tt.running_instances
assert status[inst]["STATUS"] == orig_status[inst]["STATUS"]
if was_running:
assert status[inst]["PID"] == orig_status[inst]["PID"]
if inst in target_instances:
assert was_running
pid = status[inst]["PID"]
assert f"{inst}: logs has been rotated. PID: {pid}" in out
with open(tt.log_path(inst, utils.log_file)) as f:
assert "reopened" in f.read()
if was_running:
pid = status[inst]["PID"]
assert pid == orig_status[inst]["PID"]
assert f"{inst} (PID = {pid}): logs has been rotated." in out
with open(tt.log_path(inst, utils.log_file)) as f:
assert "reopened" in f.read()
else:
_, sep, inst_name = inst.partition(':')
assert sep != ''
assert f"{inst_name}: the instance is not running, it must be started" in out


def post_start_logrotate_decorator(func):
Expand Down
6 changes: 3 additions & 3 deletions test/integration/running/test_running.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def test_logrotate(tt_cmd, tmpdir_with_cfg):
os.rename(tt_log_file, os.path.join(tmpdir, log_file))
logrotate_rc, logrotate_out = run_command_and_get_output(logrotate_cmd, cwd=tmpdir)
assert logrotate_rc == 0
assert re.search(r"test_env_app: logs has been rotated. PID: \d+.", logrotate_out)
assert re.search(r"test_env_app \(PID = \d+\): logs has been rotated.", logrotate_out)

# Wait for the files to be re-created.
file = wait_file(os.path.dirname(tt_log_file), log_file, [])
Expand Down Expand Up @@ -454,9 +454,9 @@ def test_no_args_usage(tt_cmd):
status_cmd = [tt_cmd, "logrotate"]
status_rc, status_out = run_command_and_get_output(status_cmd, cwd=test_app_path)
assert status_rc == 0
assert re.search(r"app1:(router|master|replica): logs has been rotated. PID: \d+.",
assert re.search(r"app1:(router|master|replica) \(PID = \d+\): logs has been rotated.",
status_out)
assert re.search(r"app2: logs has been rotated. PID: \d+.", status_out)
assert re.search(r"app2 \(PID = \d+\): logs has been rotated.", status_out)

# Stop all applications.
stop_cmd = [tt_cmd, "stop", "-y"]
Expand Down

0 comments on commit f66fb27

Please sign in to comment.