Skip to content

Commit ce2c8dc

Browse files
committed
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
1 parent e31afa3 commit ce2c8dc

File tree

3 files changed

+21
-24
lines changed

3 files changed

+21
-24
lines changed

cli/cmd/logrotate.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,10 @@ func internalLogrotateModule(cmdCtx *cmdcontext.CmdCtx, args []string) error {
4747
}
4848

4949
for _, run := range runningCtx.Instances {
50-
res, err := running.Logrotate(&run)
50+
err := running.Logrotate(&run)
5151
if err != nil {
52-
return err
52+
log.Infof(err.Error())
5353
}
54-
log.Info(res)
5554
}
5655

5756
return nil

cli/running/running.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -858,24 +858,27 @@ func Status(run *InstanceCtx) process_utils.ProcessState {
858858
}
859859

860860
// Logrotate rotates logs of a started tarantool instance.
861-
func Logrotate(run *InstanceCtx) (string, error) {
861+
func Logrotate(run *InstanceCtx) error {
862+
fullInstanceName := GetAppInstanceName(*run)
863+
862864
pid, err := process_utils.GetPIDFromFile(run.PIDFile)
863865
if err != nil {
864-
return "", errors.New(instStateStopped.String())
866+
return fmt.Errorf("%s: the instance is not running, it must be started", fullInstanceName)
865867
}
866868

867869
alive, err := process_utils.IsProcessAlive(pid)
868870
if !alive {
869-
return "", errors.New(instStateDead.String())
871+
return errors.New(instStateDead.String())
870872
}
871873

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

876878
// Rotates logs [instance name pid]
877-
fullInstanceName := GetAppInstanceName(*run)
878-
return fmt.Sprintf("%s: logs has been rotated. PID: %v.", fullInstanceName, pid), nil
879+
log.Infof("%s (PID = %v): logs has been rotated.", fullInstanceName, pid)
880+
881+
return nil
879882
}
880883

881884
// Check returns the result of checking the syntax of the application file.

test/integration/logrotate/test_logrotate.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,26 @@ def post_start(tt_app, instances):
3030

3131
# Do logrotate.
3232
rc, out = tt_app.logrotate(target)
33-
34-
# If any of the requested instances is not running return failure.
35-
for inst in tt_app.instances_of(target):
36-
if not tt_app.is_instance_of(inst, *running_targets):
37-
assert rc != 0
38-
assert "NOT RUNNING" in out
39-
return
33+
assert rc == 0
4034

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

4438
# Check the instances.
45-
assert rc == 0
4639
status = tt_app.status()
4740
for inst in tt_app.instances:
4841
inst_id = tt_app.inst_id(inst)
4942
was_running = tt_app.is_instance_of(inst, *running_targets)
5043
if tt_app.is_instance_of(inst, target):
51-
assert was_running
52-
assert status[inst_id]["STATUS"] == "RUNNING"
53-
assert status[inst_id]["PID"] == orig_status[inst_id]["PID"]
54-
pid = status[inst_id]["PID"]
55-
assert f"{inst_id}: logs has been rotated. PID: {pid}" in out
56-
with open(tt_app.log_path(inst, log_file)) as f:
57-
assert "reopened" in f.read()
44+
if was_running:
45+
assert status[inst_id]["STATUS"] == "RUNNING"
46+
pid = status[inst_id]["PID"]
47+
assert pid == orig_status[inst_id]["PID"]
48+
assert f"{inst_id} (PID = {pid}): logs has been rotated." in out
49+
with open(tt_app.log_path(inst, log_file)) as f:
50+
assert "reopened" in f.read()
51+
else:
52+
assert f"{inst_id}: the instance is not running, it must be started" in out
5853
else:
5954
assert inst_id not in out
6055
assert status[inst_id]["STATUS"] == orig_status[inst_id]["STATUS"]

0 commit comments

Comments
 (0)