12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package remote_manager
- import (
- "time"
- "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/plugin_errors"
- "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
- "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
- "github.com/langgenius/dify-plugin-daemon/internal/utils/routine"
- )
- func (r *RemotePluginRuntime) InitEnvironment() error {
- return nil
- }
- func (r *RemotePluginRuntime) Stopped() bool {
- return !r.alive
- }
- func (r *RemotePluginRuntime) Stop() {
- r.alive = false
- if r.conn == nil {
- return
- }
- r.conn.Close()
- }
- func (r *RemotePluginRuntime) Type() plugin_entities.PluginRuntimeType {
- return plugin_entities.PLUGIN_RUNTIME_TYPE_REMOTE
- }
- func (r *RemotePluginRuntime) StartPlugin() error {
- var exitError error
- // handle heartbeat
- routine.Submit(func() {
- r.lastActiveAt = time.Now()
- ticker := time.NewTicker(5 * time.Second)
- defer ticker.Stop()
- for {
- select {
- case <-ticker.C:
- if time.Since(r.lastActiveAt) > 60*time.Second {
- // kill this connection if it's not active for a long time
- r.conn.Close()
- exitError = plugin_errors.ErrPluginNotActive
- return
- }
- case <-r.shutdownChan:
- return
- }
- }
- })
- r.response.Async(func(data []byte) {
- plugin_entities.ParsePluginUniversalEvent(
- data,
- func(session_id string, data []byte) {
- r.callbacksLock.RLock()
- listeners := r.callbacks[session_id][:]
- r.callbacksLock.RUnlock()
- // handle session event
- for _, listener := range listeners {
- listener(data)
- }
- },
- func() {
- r.lastActiveAt = time.Now()
- },
- func(err string) {
- log.Error("plugin %s: %s", r.Configuration().Identity(), err)
- },
- func(message string) {
- log.Info("plugin %s: %s", r.Configuration().Identity(), message)
- },
- )
- })
- return exitError
- }
- func (r *RemotePluginRuntime) Wait() (<-chan bool, error) {
- return r.shutdownChan, nil
- }
- func (r *RemotePluginRuntime) Checksum() (string, error) {
- return r.checksum, nil
- }
|