E5F5 Fix Reload Workers by aphralG · Pull Request #1250 · nginx/agent · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
wait for one worker to be reloaded
  • Loading branch information
aphralG committed Sep 8, 2025
commit dc8d8ff39e84625fda4326efec62afcfb119a93d
18 changes: 9 additions & 9 deletions internal/resource/nginx_instance_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (i *NginxInstanceOperator) Validate(ctx context.Context, instance *mpi.Inst
}

func (i *NginxInstanceOperator) Reload(ctx context.Context, instance *mpi.Instance) error {
var reloadTime time.Time
var createdTime time.Time
var errorsFound error
pid := instance.GetInstanceRuntime().GetProcessId()

Expand All @@ -72,7 +72,7 @@ func (i *NginxInstanceOperator) Reload(ctx context.Context, instance *mpi.Instan
workers := i.nginxProcessOperator.NginxWorkerProcesses(ctx, pid)

if len(workers) > 0 {
reloadTime = workers[0].Created
createdTime = workers[0].Created
}

errorLogs := i.errorLogs(instance)
Expand All @@ -92,7 +92,7 @@ func (i *NginxInstanceOperator) Reload(ctx context.Context, instance *mpi.Instan
slog.WarnContext(ctx, "Error finding parent process ID, unable to check if NGINX worker "+
"processes have reloaded", "error", procErr)
} else {
i.checkWorkers(ctx, instance.GetInstanceMeta().GetInstanceId(), reloadTime, processes)
i.checkWorkers(ctx, instance.GetInstanceMeta().GetInstanceId(), createdTime, processes)
}

slog.InfoContext(ctx, "NGINX reloaded", "process_id", pid)
Expand All @@ -117,7 +117,7 @@ func (i *NginxInstanceOperator) Reload(ctx context.Context, instance *mpi.Instan
return nil
}

func (i *NginxInstanceOperator) checkWorkers(ctx context.Context, instanceID string, reloadTime time.Time,
func (i *NginxInstanceOperator) checkWorkers(ctx context.Context, instanceID string, createdTime time.Time,
processes []*nginxprocess.Process,
) {
backoffSettings := &config.BackOff{
Expand Down Expand Up @@ -148,13 +148,13 @@ func (i *NginxInstanceOperator) checkWorkers(ctx context.Context, instanceID str
}

for _, worker := range currentWorkers {
if !worker.Created.After(reloadTime) {
return fmt.Errorf("waiting for all NGINX workers to be newer "+
"than %v, found worker with time %v", reloadTime, worker.Created)
if worker.Created.After(createdTime) {
return nil
}
}

return nil
return fmt.Errorf("waiting for NGINX worker to be newer "+
"than %v", createdTime)
})
if err != nil {
slog.WarnContext(ctx, "Failed to check if NGINX worker processes have successfully reloaded, "+
Expand All @@ -163,7 +163,7 @@ func (i *NginxInstanceOperator) checkWorkers(ctx context.Context, instanceID str
return
}

slog.InfoContext(ctx, "All NGINX workers have been reloaded")
slog.InfoContext(ctx, "NGINX workers have been reloaded")
}

func (i *NginxInstanceOperator) validateConfigCheckResponse(out []byte) error {
Expand Down
38 changes: 36 additions & 2 deletions internal/resource/nginx_instance_operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,41 @@ func TestInstanceOperator_checkWorkers(t *testing.T) {
},
},
{
name: "Test 2: Unsuccessful reload",
name: "Test 2: Successful reload - one workers is reloaded",
expectedLog: "All NGINX workers have been reloaded",
reloadTime: time.Date(2025, 8, 13, 8, 0, 0, 0, time.Local),
instanceID: "e1374cb1-462d-3b6c-9f3b-f28332b5f10c",
masterProcess: []*nginxprocess.Process{
{
PID: 1234,
Created: time.Date(2025, 8, 13, 8, 1, 0, 0, time.Local),
PPID: 1,
Name: "nginx",
Cmd: "nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off;",
Exe: exePath,
},
},
workers: []*nginxprocess.Process{
{
PID: 567,
Created: time.Date(2025, 8, 13, 8, 1, 0, 0, time.Local),
PPID: 1234,
Name: "nginx",
Cmd: "nginx: worker process",
Exe: exePath,
},
{
PID: 789,
PPID: 1234,
Created: time.Date(2025, 8, 13, 7, 1, 0, 0, time.Local),
Name: "nginx",
Cmd: "nginx: worker process",
Exe: exePath,
},
},
},
{
name: "Test 3: Unsuccessful reload",
expectedLog: "\"Failed to check if NGINX worker processes have successfully reloaded, timed out " +
"waiting\" error=\"waiting for NGINX worker processes\"",
reloadTime: time.Date(2025, 8, 13, 8, 0, 0, 0, time.Local),
Expand All @@ -333,7 +367,7 @@ func TestInstanceOperator_checkWorkers(t *testing.T) {
workers: []*nginxprocess.Process{
{
PID: 567,
Created: time.Date(2025, 8, 13, 8, 1, 0, 0, time.Local),
Created: time.Date(2025, 8, 13, 7, 1, 0, 0, time.Local),
PPID: 1234,
Name: "nginx",
Cmd: "nginx: worker process",
Expand Down
Loading
0