88using System . Net . Http . Json ;
99using System . Threading ;
1010using System . Threading . Tasks ;
11- using CodeProject . AI . SDK . API ;
12- using CodeProject . AI . SDK . Utils ;
1311
1412using Microsoft . Extensions . Configuration ;
1513using Microsoft . Extensions . Hosting ;
1614using Microsoft . Extensions . Logging ;
1715
18- namespace CodeProject . AI . SDK
16+ using CodeProject . AI . SDK . API ;
17+ using CodeProject . AI . SDK . Utils ;
18+
19+ namespace CodeProject . AI . SDK . Backend
1920{
2021 /// <summary>
2122 /// The base class from which a module should be derived.
2223 /// </summary>
23- public abstract class ModuleWorkerBase : BackgroundService
24+ public abstract class ModuleRunnerBase : BackgroundService
2425 {
25- private readonly string [ ] _doNotLogCommands = {
26+ private readonly string [ ] _doNotLogCommands = [
2627 "list-custom" ,
2728 "get_module_status" , "status" , "get_status" , // status is deprecated alias
2829 "get_command_status"
29- } ;
30+ ] ;
3031
3132 private readonly TimeSpan _status_delay = TimeSpan . FromSeconds ( 2 ) ;
3233
@@ -61,7 +62,7 @@ public abstract class ModuleWorkerBase : BackgroundService
6162 /// <summary>
6263 /// Gets or sets the path to this Module
6364 /// </summary>
64- public string ? moduleDirPath { get ; set ; }
65+ public string ? ModuleDirPath { get ; set ; }
6566
6667 /// <summary>
6768 /// Gets or sets a value indicating whether or not this module supports GPU acceleration
@@ -95,13 +96,13 @@ public abstract class ModuleWorkerBase : BackgroundService
9596 /// <param name="logger">The Logger.</param>
9697 /// <param name="configuration">The app configuration values.</param>
9798 /// <param name="hostApplicationLifetime">The applicationLifetime object</param>
98- public ModuleWorkerBase ( ILogger logger , IConfiguration configuration ,
99+ public ModuleRunnerBase ( ILogger logger , IConfiguration configuration ,
99100 IHostApplicationLifetime hostApplicationLifetime )
100101 {
101102 // _logger = logger;
102103
103104 using ILoggerFactory factory = LoggerFactory . Create ( builder => builder . AddConsole ( ) ) ;
104- _logger = factory . CreateLogger < ModuleWorkerBase > ( ) ;
105+ _logger = factory . CreateLogger < ModuleRunnerBase > ( ) ;
105106
106107 _cancelled = false ;
107108 _appLifetime = hostApplicationLifetime ;
@@ -112,23 +113,29 @@ public ModuleWorkerBase(ILogger logger, IConfiguration configuration,
112113 // TODO: We could load the settings directly from the modulesettings files rather than rely
113114 // rely environment variables:
114115 /*
115- // Load up the module's settings and start the module
116- var config = new ConfigurationBuilder ();
117- config .AddModuleSettingsConfigFiles(moduleDirPath , false);
118- IConfiguration configuration = config .Build();
116+ // Load up the modulesettings.*.json files
117+ var configBuilder = new IConfigurationBuilder ();
118+ configBuilder .AddModuleSettingsConfigFiles(ModuleDirPath , false);
119+ IConfiguration config = configBuilder .Build();
119120
120121 // Bind the values in the configuration to a ModuleConfig object
121- string moduleId = [get module id from config]
122122 var moduleConfig = new ModuleConfig();
123- configuration.Bind($"Modules:{moduleId}", moduleConfig);
123+ try
124+ {
125+ config.Bind($"Modules:{moduleId}", moduleConfig);
126+ }
127+ catch (Exception)
128+ {
129+ moduleConfig = null;
130+ }
124131
125132 // Complete the ModuleConfig's setup.
126- if (moduleConfig.Initialise(moduleId, moduleDirPath , ModuleLocation.Internal))
133+ if (moduleConfig.Initialise(moduleId, ModuleDirPath , ModuleLocation.Internal))
127134 */
128135
129136 _moduleId = configuration . GetValue < string ? > ( "CPAI_MODULE_ID" , null ) ?? currentDirName ;
130137 ModuleName = configuration . GetValue < string ? > ( "CPAI_MODULE_NAME" , null ) ?? _moduleId ;
131- moduleDirPath = configuration . GetValue < string ? > ( "CPAI_MODULE_PATH" , null ) ?? currentModuleDirPath ;
138+ ModuleDirPath = configuration . GetValue < string ? > ( "CPAI_MODULE_PATH" , null ) ?? currentModuleDirPath ;
132139 _queueName = configuration . GetValue < string ? > ( "CPAI_MODULE_QUEUENAME" , null ) ?? _moduleId . ToLower ( ) + "_queue" ;
133140
134141 int port = configuration . GetValue < int > ( "CPAI_PORT" , 32168 ) ;
@@ -638,8 +645,6 @@ protected string GetModuleDirectoryPath()
638645 string moduleDirPath = AppContext . BaseDirectory ;
639646 DirectoryInfo ? info = new DirectoryInfo ( moduleDirPath ) ;
640647
641- // HACK: If we're running this server from the build output dir in dev environment
642- // then the root path will be wrong.
643648 if ( SystemInfo . IsDevelopmentCode )
644649 {
645650 while ( info != null )
@@ -652,6 +657,11 @@ protected string GetModuleDirectoryPath()
652657 }
653658 }
654659 }
660+ else
661+ {
662+ if ( info ? . Name . ToLower ( ) == "bin" )
663+ info = info . Parent ;
664+ }
655665
656666 if ( info != null )
657667 return info . FullName ;
0 commit comments