Skip to content

Commit c06646b

Browse files
authored
chore: add debug log (#709)
1 parent 98b24b5 commit c06646b

2 files changed

Lines changed: 78 additions & 4 deletions

File tree

internal/core/local_runtime/instance.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/google/uuid"
1313
"github.com/langgenius/dify-plugin-daemon/internal/types/app"
1414
"github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities"
15+
"github.com/langgenius/dify-plugin-daemon/pkg/utils/log"
1516
)
1617

1718
const (
@@ -124,6 +125,15 @@ func (s *PluginInstance) Stop() {
124125
// Once the subprocess exists itself, STDOUT always close, which results in `CLOSE STDOUT`
125126
func (s *PluginInstance) StartStdout() {
126127
defer func() {
128+
log.Info(
129+
"plugin stdout reader exiting",
130+
"plugin", s.pluginUniqueIdentifier,
131+
"instance", s.ID()[:8],
132+
"started", s.started,
133+
"shutdown", s.shutdown,
134+
"last_active_at", s.lastActiveAt,
135+
"instance_error", s.Error(),
136+
)
127137
// notify shutdown signal
128138
s.WalkNotifiers(func(notifier PluginInstanceNotifier) {
129139
notifier.OnInstanceShutdown(s)
@@ -167,6 +177,15 @@ func (s *PluginInstance) StartStdout() {
167177
),
168178
)
169179
})
180+
} else {
181+
log.Warn(
182+
"plugin stdout reader reached eof",
183+
"plugin", s.pluginUniqueIdentifier,
184+
"instance", s.ID()[:8],
185+
"started", s.started,
186+
"shutdown", s.shutdown,
187+
"last_active_at", s.lastActiveAt,
188+
)
170189
}
171190

172191
// once reader of stdout is closed, kill subprocess
@@ -176,13 +195,37 @@ func (s *PluginInstance) StartStdout() {
176195
s.WalkNotifiers(func(notifier PluginInstanceNotifier) {
177196
notifier.OnInstanceErrorLog(s, fmt.Errorf("failed to kill subprocess: %s", err.Error()))
178197
})
198+
log.Warn(
199+
"plugin subprocess kill returned",
200+
"plugin", s.pluginUniqueIdentifier,
201+
"instance", s.ID()[:8],
202+
"error", err,
203+
)
204+
} else {
205+
log.Warn(
206+
"plugin subprocess killed after stdout closed",
207+
"plugin", s.pluginUniqueIdentifier,
208+
"instance", s.ID()[:8],
209+
)
179210
}
180211

181212
// collect subprocess, avoid zombie processes
182213
if _, err := s.cmd.Process.Wait(); err != nil {
183214
s.WalkNotifiers(func(notifier PluginInstanceNotifier) {
184215
notifier.OnInstanceErrorLog(s, fmt.Errorf("failed to reap subprocess: %s", err.Error()))
185216
})
217+
log.Warn(
218+
"plugin subprocess wait returned error",
219+
"plugin", s.pluginUniqueIdentifier,
220+
"instance", s.ID()[:8],
221+
"error", err,
222+
)
223+
} else {
224+
log.Info(
225+
"plugin subprocess reaped",
226+
"plugin", s.pluginUniqueIdentifier,
227+
"instance", s.ID()[:8],
228+
)
186229
}
187230
}
188231

@@ -282,6 +325,13 @@ func (s *PluginInstance) Monitor() error {
282325

283326
// check heartbeat
284327
if time.Since(s.lastActiveAt) > MAX_HEARTBEAT_INTERVAL {
328+
log.Warn(
329+
"plugin instance marked inactive",
330+
"plugin", s.pluginUniqueIdentifier,
331+
"instance", s.ID()[:8],
332+
"inactive_seconds", time.Since(s.lastActiveAt).Seconds(),
333+
"instance_error", s.Error(),
334+
)
285335
s.WalkNotifiers(func(notifier PluginInstanceNotifier) {
286336
// notify handlers
287337
notifier.OnInstanceLaunchFailed(

internal/core/local_runtime/subprocess.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/langgenius/dify-plugin-daemon/pkg/entities/constants"
1313
"github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities"
1414
routinepkg "github.com/langgenius/dify-plugin-daemon/pkg/routine"
15+
"github.com/langgenius/dify-plugin-daemon/pkg/utils/log"
1516
"github.com/langgenius/dify-plugin-daemon/pkg/utils/routine"
1617
)
1718

@@ -124,26 +125,49 @@ func (r *LocalPluginRuntime) startNewInstance() error {
124125
instance.AddNotifier(&PluginInstanceNotifierTemplate{
125126
// the first heartbeat will trigger this
126127
OnInstanceReadyImpl: func(pi *PluginInstance) {
127-
// notify plugin started
128-
r.WalkNotifiers(func(notifier PluginRuntimeNotifier) {
129-
notifier.OnInstanceReady(instance)
130-
})
131128
// mark the instance as started
132129
instance.started = true
133130
// setup instance
134131
r.instanceLocker.Lock()
132+
before := len(r.instances)
135133
r.instances = append(r.instances, instance)
134+
after := len(r.instances)
136135
r.instanceLocker.Unlock()
136+
log.Info(
137+
"local runtime instance ready",
138+
"plugin", r.Config.Identity(),
139+
"instance", instance.ID()[:8],
140+
"pid", e.Process.Pid,
141+
"instances_before", before,
142+
"instances_after", after,
143+
)
144+
// notify plugin started
145+
r.WalkNotifiers(func(notifier PluginRuntimeNotifier) {
146+
notifier.OnInstanceReady(instance)
147+
})
137148

138149
close(launchChannel)
139150
},
140151
OnInstanceShutdownImpl: func(pi *PluginInstance) {
141152
// remove the instance from the list
142153
r.instanceLocker.Lock()
154+
before := len(r.instances)
143155
r.instances = slices.DeleteFunc(r.instances, func(instance *PluginInstance) bool {
144156
return instance.instanceId == pi.instanceId
145157
})
158+
after := len(r.instances)
146159
r.instanceLocker.Unlock()
160+
log.Warn(
161+
"local runtime instance shutdown",
162+
"plugin", r.Config.Identity(),
163+
"instance", pi.ID()[:8],
164+
"pid", e.Process.Pid,
165+
"started", pi.started,
166+
"shutdown", pi.shutdown,
167+
"instances_before", before,
168+
"instances_after", after,
169+
"instance_error", pi.Error(),
170+
)
147171

148172
if !instance.started {
149173
// if the instance is not started, it means the plugin is not ready

0 commit comments

Comments
 (0)