MCPcopy Create free account
hub / github.com/QuantumNous/new-api / SyncTaskPluginsOnceContext

Function SyncTaskPluginsOnceContext

controller/task_plugin.go:1026–1186  ·  view source on GitHub ↗

SyncTaskPluginsOnceContext loads the enabled plugin rows into the registry and records the ones this node runs differently from the database as the plugins sync topic's errors. A database error keeps the current plugins.

(ctx context.Context)

Source from the content-addressed store, hash-verified

1024// and records the ones this node runs differently from the database as the
1025// plugins sync topic's errors. A database error keeps the current plugins.
1026func SyncTaskPluginsOnceContext(ctx context.Context) error {
1027 started := time.Now()
1028 taskPluginSyncState.Lock()
1029 defer taskPluginSyncState.Unlock()
1030 databaseSnapshot, err := model.GetTaskPluginSyncSnapshot()
1031 if err != nil {
1032 syncErr := fmt.Errorf("sync task plugins: %w", err)
1033 taskPluginSyncState.lastRebuild = taskPluginRebuildOutcome{
1034 Status: "failed",
1035 AttemptedAt: time.Now(),
1036 Generation: jsplugin.DefaultRegistry.Generation().Number,
1037 DatabaseRevision: taskPluginSyncState.lastRebuild.DatabaseRevision,
1038 Error: syncErr.Error(),
1039 }
1040 logger.LogDebug(
1041 ctx,
1042 "task_plugin subsystem=sync event=failed stage=database_snapshot retained_generation=%d elapsed_ms=%d",
1043 jsplugin.DefaultRegistry.Generation().Number,
1044 time.Since(started).Milliseconds(),
1045 )
1046 return syncErr
1047 }
1048 databasePlugins := databaseSnapshot.Plugins
1049 sort.Slice(databasePlugins, func(i, j int) bool { return databasePlugins[i].Key < databasePlugins[j].Key })
1050 currentOverrides := jsplugin.DefaultRegistry.OverridePlugins()
1051 generationBefore := jsplugin.DefaultRegistry.Generation().Number
1052 logger.LogDebug(
1053 ctx,
1054 "task_plugin subsystem=sync event=start database_revision=%q generation=%d desired_plugins=%d current_overrides=%d",
1055 databaseSnapshot.Revision,
1056 generationBefore,
1057 len(databasePlugins),
1058 len(currentOverrides),
1059 )
1060 nextOverrides := make([]*jsplugin.LoadedPlugin, 0, len(databasePlugins))
1061 nextHashes := make(map[string]string, len(databasePlugins))
1062 seen := make(map[string]bool, len(databasePlugins))
1063 for _, plugin := range databasePlugins {
1064 seen[plugin.Key] = true
1065 if current := currentOverrides[plugin.Key]; current != nil && taskPluginSyncState.hashes[plugin.Key] == plugin.SourceHash {
1066 nextOverrides = append(nextOverrides, current)
1067 nextHashes[plugin.Key] = plugin.SourceHash
1068 logger.LogDebug(
1069 ctx,
1070 "task_plugin subsystem=sync event=plugin plugin=%q version=%q action=reuse",
1071 plugin.Key,
1072 plugin.Version,
1073 )
1074 continue
1075 }
1076 logger.LogDebug(
1077 ctx,
1078 "task_plugin subsystem=sync event=plugin plugin=%q version=%q action=compile_start",
1079 plugin.Key,
1080 plugin.Version,
1081 )
1082 var compiled *jsplugin.LoadedPlugin
1083 source, compileErr := model.GetTaskPluginSource(plugin.Id)

Callers 5

UploadTaskPluginFunction · 0.85
DeleteTaskPluginVersionFunction · 0.85
ActivateTaskPluginFunction · 0.85
SetTaskPluginStatusFunction · 0.85
syncTaskPluginsOnceFunction · 0.85

Calls 15

LogDebugFunction · 0.92
GetTaskPluginSourceFunction · 0.92
CompilePluginFunction · 0.92
SysErrorFunction · 0.92
LogTextFunction · 0.92
SetUnloadedPluginsFunction · 0.92
SetSyncTopicErrorsFunction · 0.92
UnlockMethod · 0.80
GenerationMethod · 0.80
OverridePluginsMethod · 0.80
ReplaceOverridesMethod · 0.80

Tested by

no test coverage detected