Files
Yeuoly 888ad788bc refactor: plugin lifecycle control panel (#499)
* refactor: introduce local plugin control panel and cleanup environment setup process

* fix: args

* refactor: new local runtime

* temp: stash work for refactor on RemotePluginServer

* refactor: unify local runtime lifetime and sperate init environment process

* chore: add missing files

* stash

* refactor: local plugin lifetime control

* refactor: complete installation process of control panel

* refactor: adapt service layer to new controlpanel

* refactor: pluginManager.Install

* fix: add routine wrap to InstallServerless, avoid blocking main thread

* feat: reinstall serverless runtime

* chore: add comments to Reinstall and update confusing naming

* refactor: unify install plugin service

* refactor: add labels to debugging runtime

* refactor: add getters to plugin manager

* refactor: split install service to decode/install_task/install service

* ???

* refactor: adapt controllers

* refactor: session write

* refactor: session runtime

* Refine install task orchestration (#501)

* refactor: installing task

* refactor cluster management, decouple lifetime management and cluster

* fix cli test command

* fix: cleanup TODO comments and implement GracefulStop for instance

* feat: add logger to control panel

* fix: multiple nil references

* refactor: better lifetime control

* refactor: better cycle interval

* fix(LocalPluginRuntime): prevent returning err when it's not  error

* fix: avoid adding empty PipExtraArgs

* fix: missing errors in Environment init

* fix: add truncateMessage to avoid db explosion

* cleanup: better lifecycle management

* fix: init status at the beginning of installation

* optimize: GracefulStop for pluginInstance

* refactor: tests

* refactor: centralize routine labels (#504)

* cleanup: RoutineKey

* fix: init routine pool

* fix: correctly handle cluster register error

* fix: memory leak

* fix: add \n to instance write

* fix(installer.go): set success to true after succeed for defer func

* refactor

* fix: missing cwd in testutils

* fix: scaleup default runtime nums to 1 when testing

* fix: localruntime appconfig in testing module

* Update internal/core/local_runtime/load_balancing.go

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>

* fix: more efficiency implement in installer_local.go

* fix: returns after failing in onDebuggingRuntimeDisconnected

* fix: returns after failing in onDebuggingRuntimeDisconnected

* fix: splits tests

* refactor: naming

* refactor: manifest.VersionX

* fix: adapt SetDefault to tests

* fix: enforce use constants in DBType

* fix: generate

* fix: linter

* cleanup tests

* refactor: change  package to

* cleanup: useless codes

* Update internal/cluster/plugin.go

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>

* cleanup

* refactor: decouple connection_key management from debugging_time

* refactor: confused naming

* feat: recycle resources to adapt to https://github.com/langgenius/dify-plugin-daemon/pull/500

* refactor: confusing redirecting

* fix: support get serverless runtime

* fix: race condition in Launching

* fix: avoid ManifestValidate in first step of debugging handshake

* fix: adding ReleaseAllLocks to finalizers

* wtf: what a beautiful code

* refactor: rename Stream.Async to Stream.Process

* fix: kill process if daed instance was detected

* fix: correctly handle failures

* fix: consistence of difference interfaces

* fix: add stacktrace to panic

* fix: only trigger once  event

* fix: ensure plugin runtime was shutdown

* feat: cleanup install tasks

* fix: add scale logs

---------

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
2025-11-18 17:28:02 +08:00

341 lines
7.9 KiB
Go

package db
import (
"fmt"
"github.com/langgenius/dify-plugin-daemon/pkg/utils/log"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
/*
ORM for pgsql
*/
var DifyPluginDB *gorm.DB
var (
ErrDatabaseNotFound = gorm.ErrRecordNotFound
)
func Create(data any, ctx ...*gorm.DB) error {
if len(ctx) > 0 {
return ctx[0].Create(data).Error
}
return DifyPluginDB.Create(data).Error
}
func Update(data any, ctx ...*gorm.DB) error {
if len(ctx) > 0 {
return ctx[0].Save(data).Error
}
return DifyPluginDB.Save(data).Error
}
func Delete(data any, ctx ...*gorm.DB) error {
if len(ctx) > 0 {
return ctx[0].Delete(data).Error
}
return DifyPluginDB.Delete(data).Error
}
func DeleteByCondition[T any](condition T, ctx ...*gorm.DB) error {
var model T
if len(ctx) > 0 {
return ctx[0].Where(condition).Delete(&model).Error
}
return DifyPluginDB.Where(condition).Delete(&model).Error
}
func ReplaceAssociation[T any, R any](source *T, field string, associations []R, ctx ...*gorm.DB) error {
if len(ctx) > 0 {
return ctx[0].Model(source).Association(field).Replace(associations)
}
return DifyPluginDB.Model(source).Association(field).Replace(associations)
}
func AppendAssociation[T any, R any](source *T, field string, associations R, ctx ...*gorm.DB) error {
if len(ctx) > 0 {
return ctx[0].Model(source).Association(field).Append(associations)
}
return DifyPluginDB.Model(source).Association(field).Append(associations)
}
type genericComparableConstraint interface {
int | int8 | int16 | int32 | int64 |
uint | uint8 | uint16 | uint32 | uint64 |
float32 | float64 |
bool
}
type genericEqualConstraint interface {
genericComparableConstraint | string
}
type GenericQuery func(tx *gorm.DB) *gorm.DB
func Equal[T genericEqualConstraint](field string, value T) GenericQuery {
return func(tx *gorm.DB) *gorm.DB {
return tx.Where(fmt.Sprintf("%s = ?", field), value)
}
}
func EqualOr[T genericEqualConstraint](field string, value T) GenericQuery {
return func(tx *gorm.DB) *gorm.DB {
return tx.Or(fmt.Sprintf("%s = ?", field), value)
}
}
func NotEqual[T genericEqualConstraint](field string, value T) GenericQuery {
return func(tx *gorm.DB) *gorm.DB {
return tx.Where(fmt.Sprintf("%s <> ?", field), value)
}
}
func GreaterThan[T genericComparableConstraint](field string, value T) GenericQuery {
return func(tx *gorm.DB) *gorm.DB {
return tx.Where(fmt.Sprintf("%s > ?", field), value)
}
}
func GreaterThanOrEqual[T genericComparableConstraint](field string, value T) GenericQuery {
return func(tx *gorm.DB) *gorm.DB {
return tx.Where(fmt.Sprintf("%s >= ?", field), value)
}
}
func LessThan[T genericComparableConstraint](field string, value T) GenericQuery {
return func(tx *gorm.DB) *gorm.DB {
return tx.Where(fmt.Sprintf("%s < ?", field), value)
}
}
func LessThanOrEqual[T genericComparableConstraint](field string, value T) GenericQuery {
return func(tx *gorm.DB) *gorm.DB {
return tx.Where(fmt.Sprintf("%s <= ?", field), value)
}
}
func Like(field string, value string) GenericQuery {
return func(tx *gorm.DB) *gorm.DB {
return tx.Where(fmt.Sprintf("%s LIKE ?", field), "%"+value+"%")
}
}
func Page(page int, pageSize int) GenericQuery {
return func(tx *gorm.DB) *gorm.DB {
return tx.Offset((page - 1) * pageSize).Limit(pageSize)
}
}
func OrderBy(field string, desc bool) GenericQuery {
return func(tx *gorm.DB) *gorm.DB {
if desc {
return tx.Order(fmt.Sprintf("%s DESC", field))
}
return tx.Order(field)
}
}
// bitwise operation
func WithBit[T genericComparableConstraint](field string, value T) GenericQuery {
return func(tx *gorm.DB) *gorm.DB {
return tx.Where(fmt.Sprintf("%s & ? = ?", field), value, value)
}
}
func WithoutBit[T genericComparableConstraint](field string, value T) GenericQuery {
return func(tx *gorm.DB) *gorm.DB {
return tx.Where(fmt.Sprintf("%s & ~? != 0", field), value)
}
}
func Inc[T genericComparableConstraint](updates map[string]T) GenericQuery {
return func(tx *gorm.DB) *gorm.DB {
m := make(map[string]any)
for field, value := range updates {
m[field] = gorm.Expr(fmt.Sprintf("%s + ?", field), value)
}
return tx.UpdateColumns(m)
}
}
func Dec[T genericComparableConstraint](updates map[string]T) GenericQuery {
return func(tx *gorm.DB) *gorm.DB {
m := make(map[string]any)
for field, value := range updates {
m[field] = gorm.Expr(fmt.Sprintf("%s - ?", field), value)
}
return tx.UpdateColumns(m)
}
}
func Model(model any) GenericQuery {
return func(tx *gorm.DB) *gorm.DB {
return tx.Model(model)
}
}
func Fields(fields ...string) GenericQuery {
return func(tx *gorm.DB) *gorm.DB {
return tx.Select(fields)
}
}
func Preload(model string, args ...interface{}) GenericQuery {
return func(tx *gorm.DB) *gorm.DB {
return tx.Preload(model, args...)
}
}
func Join(field string) GenericQuery {
return func(tx *gorm.DB) *gorm.DB {
return tx.Joins(field)
}
}
func WLock /* write lock */ () GenericQuery {
return func(tx *gorm.DB) *gorm.DB {
return tx.Clauses(clause.Locking{Strength: "UPDATE"})
}
}
func Where[T any](model *T) GenericQuery {
return func(tx *gorm.DB) *gorm.DB {
return tx.Where(model)
}
}
func WhereSQL(sql string, args ...interface{}) GenericQuery {
return func(tx *gorm.DB) *gorm.DB {
return tx.Where(sql, args...)
}
}
func Action(fn func(tx *gorm.DB)) GenericQuery {
return func(tx *gorm.DB) *gorm.DB {
fn(tx)
return tx
}
}
/*
Should be used first in query chain
*/
func WithTransactionContext(tx *gorm.DB) GenericQuery {
return func(_ *gorm.DB) *gorm.DB {
return tx
}
}
func InArray(field string, value []interface{}) GenericQuery {
return func(tx *gorm.DB) *gorm.DB {
return tx.Where(fmt.Sprintf("%s IN ?", field), value)
}
}
func Run(query ...GenericQuery) error {
tmp := DifyPluginDB
for _, q := range query {
tmp = q(tmp)
}
// execute query
return tmp.Error
}
func GetAny[T any](sql string, data ...interface{}) (T /* data */, error) {
var result T
err := DifyPluginDB.Raw(sql, data...).Scan(&result).Error
return result, err
}
func GetOne[T any](query ...GenericQuery) (T /* data */, error) {
var data T
tmp := DifyPluginDB
for _, q := range query {
tmp = q(tmp)
}
err := tmp.First(&data).Error
return data, err
}
func GetAll[T any](query ...GenericQuery) ([]T /* data */, error) {
var data []T
tmp := DifyPluginDB
for _, q := range query {
tmp = q(tmp)
}
err := tmp.Find(&data).Error
return data, err
}
func GetCount[T any](query ...GenericQuery) (int64 /* count */, error) {
var model T
var count int64
tmp := DifyPluginDB
for _, q := range query {
tmp = q(tmp)
}
err := tmp.Model(&model).Count(&count).Error
return count, err
}
func GetSum[T any, R genericComparableConstraint](fields string, query ...GenericQuery) (R, error) {
var model T
var sum R
tmp := DifyPluginDB
for _, q := range query {
tmp = q(tmp)
}
err := tmp.Model(&model).Select(fmt.Sprintf("SUM(%s)", fields)).Scan(&sum).Error
return sum, err
}
func DelAssociation[T any](field string, query ...GenericQuery) error {
var model T
tmp := DifyPluginDB.Model(&model)
for _, q := range query {
tmp = q(tmp)
}
return tmp.Association(field).Unscoped().Clear()
}
func WithTransaction(fn func(tx *gorm.DB) error, ctx ...*gorm.DB) error {
// Start a transaction
db := DifyPluginDB
if len(ctx) > 0 {
db = ctx[0]
}
tx := db.Begin()
if tx.Error != nil {
return tx.Error
}
err := fn(tx)
if err != nil {
if err := tx.Rollback().Error; err != nil {
log.Error("failed to rollback tx: %v", err)
}
return err
}
tx.Commit()
return nil
}
// NOTE: not used in production, only for testing
func DropTable(model any) error {
return DifyPluginDB.Migrator().DropTable(model)
}
// NOTE: not used in production, only for testing
func CreateDatabase(dbname string) error {
return DifyPluginDB.Exec(fmt.Sprintf("CREATE DATABASE %s", dbname)).Error
}
// NOTE: not used in production, only for testing
func CreateTable(model any) error {
return DifyPluginDB.Migrator().CreateTable(model)
}