From c0b013ff25315f9e695700587c47a9e3ae24d8d8 Mon Sep 17 00:00:00 2001 From: Mikhail Elhimov Date: Wed, 13 Nov 2024 23:06:42 +0300 Subject: [PATCH] logrotate: don't exit at non-running instance Just warn that the instance is not running and continue (like `tt stop` or `tt kill` do) Closes #774 --- cli/cmd/logrotate.go | 5 ++-- cli/running/running.go | 15 +++++++----- test/integration/logrotate/test_logrotate.py | 24 +++++++++----------- test/integration/running/test_running.py | 6 ++--- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/cli/cmd/logrotate.go b/cli/cmd/logrotate.go index d6594da08..00b70f484 100644 --- a/cli/cmd/logrotate.go +++ b/cli/cmd/logrotate.go @@ -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 diff --git a/cli/running/running.go b/cli/running/running.go index 031fc385e..01fe2b087 100644 --- a/cli/running/running.go +++ b/cli/running/running.go @@ -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. diff --git a/test/integration/logrotate/test_logrotate.py b/test/integration/logrotate/test_logrotate.py index 15f22b49a..c437d6824 100644 --- a/test/integration/logrotate/test_logrotate.py +++ b/test/integration/logrotate/test_logrotate.py @@ -26,19 +26,12 @@ 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 @@ -46,11 +39,16 @@ def check_logrotate(tt, target): 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): diff --git a/test/integration/running/test_running.py b/test/integration/running/test_running.py index cb2ebda0a..db927c6f5 100644 --- a/test/integration/running/test_running.py +++ b/test/integration/running/test_running.py @@ -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, []) @@ -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"]