Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] optionally load only configured indexers. #9224

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions src/Jackett.Common/Content/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ function loadJackettSettings() {
$("#jackett-allowupdate").attr('checked', data.updatedisabled);
$("#jackett-prerelease").attr('checked', data.prerelease);
$("#jackett-logging").attr('checked', data.logging);
$("#jackett-loadonlyconfiguredindexers").attr('checked', data.loadonlyconfiguredindexers);


$("#jackett-cache-enabled").attr('checked', data.cache_enabled);
$("#jackett-cache-ttl").val(data.cache_ttl);
Expand All @@ -107,6 +109,7 @@ function loadJackettSettings() {
}

$("#jackett-flaresolverrurl").val(data.flaresolverrurl);

$("#jackett-omdbkey").val(data.omdbkey);
$("#jackett-omdburl").val(data.omdburl);
var password = data.password;
Expand Down Expand Up @@ -1135,6 +1138,7 @@ function bindUIButtons() {
var jackett_update = $("#jackett-allowupdate").is(':checked');
var jackett_prerelease = $("#jackett-prerelease").is(':checked');
var jackett_logging = $("#jackett-logging").is(':checked');
var jackett_loadonlyconfiguredindexers = $("#jackett-loadonlyconfiguredindexers").is(':checked');
var jackett_cache_enabled = $("#jackett-cache-enabled").is(':checked');
var jackett_cache_ttl = $("#jackett-cache-ttl").val();
var jackett_cache_max_results_per_indexer = $("#jackett-cache-max-results-per-indexer").val();
Expand All @@ -1155,6 +1159,7 @@ function bindUIButtons() {
prerelease: jackett_prerelease,
blackholedir: $("#jackett-savedir").val(),
logging: jackett_logging,
loadonlyconfiguredindexers:jackett_loadonlyconfiguredindexers,
basepathoverride: jackett_basepathoverride,
logging: jackett_logging,
cache_enabled: jackett_cache_enabled,
Expand Down
4 changes: 4 additions & 0 deletions src/Jackett.Common/Content/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ <h3>Jackett Configuration</h3>
<span class="input-header">OMDB API Url: </span>
<input id="jackett-omdburl" class="form-control input-right" type="text" value="" placeholder="Blank for default">
</div>
<div class="input-area">
<span class="input-header">Load only configured indexers (conserve memory, requires restart): </span>
<input id="jackett-loadonlyconfiguredindexers" class="form-control input-right" type="checkbox" />
</div>
<hr />
<div id="footer">
<a href="https://github.com/Jackett/Jackett" target="_blank" title="Jackett on GitHub">Jackett</a> Version <span id="app-version"></span>
Expand Down
2 changes: 2 additions & 0 deletions src/Jackett.Common/Models/Config/ServerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ internal void OnDeserializedMethod(StreamingContext context)

public bool ProxyIsAnonymous => string.IsNullOrWhiteSpace(ProxyUsername) || string.IsNullOrWhiteSpace(ProxyPassword);

public bool LoadOnlyConfiguredIndexers { get; set; }

public string GetProxyAuthString() =>
!ProxyIsAnonymous
? $"{ProxyUsername}:{ProxyPassword}"
Expand Down
4 changes: 4 additions & 0 deletions src/Jackett.Common/Models/DTO/ServerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public class ServerConfig
[DataMember]
public bool can_run_netcore { get; set; }

[DataMember]
public bool loadonlyconfiguredindexers { get; set; }

[DataMember]
public ProxyType proxy_type { get; set; }
[DataMember]
Expand Down Expand Up @@ -77,6 +80,7 @@ public ServerConfig(IEnumerable<string> notices, Models.Config.ServerConfig conf
omdburl = config.OmdbApiUrl;
app_version = version;
can_run_netcore = canRunNetCore;
loadonlyconfiguredindexers = config.LoadOnlyConfiguredIndexers;

proxy_type = config.ProxyType;
proxy_url = config.ProxyUrl;
Expand Down
25 changes: 24 additions & 1 deletion src/Jackett.Common/Services/IndexerConfigurationService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Jackett.Common.Indexers;
using Jackett.Common.Services.Interfaces;
using Jackett.Common.Utils;
using Newtonsoft.Json.Linq;
using NLog;

Expand All @@ -10,6 +13,7 @@ namespace Jackett.Common.Services

public class IndexerConfigurationService : IIndexerConfigurationService
{
private const string ConfigFileSuffix = ".json";

//public override void LoadFromSavedConfiguration(JToken jsonConfig)
//{
Expand Down Expand Up @@ -126,8 +130,27 @@ public void Save(IIndexer indexer, JToken obj)
}
}

public IEnumerable<string> FindConfiguredIndexerIds()
{
var directoryInfo = new DirectoryInfo(configService.GetIndexerConfigDir());

if (!directoryInfo.Exists)
{
return new List<string>();
}

var fileNames = directoryInfo
.GetFiles().Select(file => file.Name)
.Where(fileName => fileName.EndsWith(ConfigFileSuffix))
.ToList();

return fileNames.Select(FilesystemUtil.getFileNameWithoutExtension)
.Where(indexId => indexId != null)
.ToList();
}

public string GetIndexerConfigFilePath(string indexerId)
=> Path.Combine(configService.GetIndexerConfigDir(), indexerId + ".json");
=> Path.Combine(configService.GetIndexerConfigDir(), indexerId + ConfigFileSuffix);

private readonly IConfigurationService configService;
private readonly Logger logger;
Expand Down
56 changes: 47 additions & 9 deletions src/Jackett.Common/Services/IndexerManagerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Jackett.Common.Models;
using Jackett.Common.Models.Config;
using Jackett.Common.Services.Interfaces;
using Jackett.Common.Utils;
using Jackett.Common.Utils.Clients;
using NLog;
using YamlDotNet.Serialization;
Expand Down Expand Up @@ -72,12 +73,33 @@ public void InitIndexers(IEnumerable<string> path)
logger.Info($"Using HTTP Client: {webClient.GetType().Name}");

MigrateRenamedIndexers();
InitIndexers();
InitCardigannIndexers(path);
var indexersToLoad = GetIndexersToLoad();
InitIndexers(indexersToLoad);
InitCardigannIndexers(GetIndexerDefinitionFiles(path), indexersToLoad);
InitAggregateIndexer();
RemoveLegacyConfigurations();
}

private ISet<string> GetIndexersToLoad()
{
if (serverConfig.LoadOnlyConfiguredIndexers)
{
return new HashSet<string>(configService.FindConfiguredIndexerIds());
}

return null;
}

private bool GetIndexerShouldBeLoaded(string indexerId, ICollection<string> configuredIndexers)
{
if (!serverConfig.LoadOnlyConfiguredIndexers)
{
return true;
}

return configuredIndexers != null && configuredIndexers.Contains(indexerId);
}

private void MigrateRenamedIndexers()
{
foreach (var oldId in renamedIndexers.Keys)
Expand Down Expand Up @@ -105,7 +127,7 @@ private void MigrateRenamedIndexers()
}
}

private void InitIndexers()
private void InitIndexers(ISet<string> configuredIndexers)
{
logger.Info("Loading Native indexers ...");

Expand All @@ -125,7 +147,13 @@ private void InitIndexers()

var arguments = new object[] { configService, indexerWebClientInstance, logger, protectionService, cacheService };
var indexer = (IIndexer)constructor.Invoke(arguments);
return indexer;

if (GetIndexerShouldBeLoaded(indexer.Id, configuredIndexers))
{
return indexer;
}

return null;
}

logger.Error($"Cannot instantiate Native indexer: {type.Name}");
Expand All @@ -141,7 +169,7 @@ private void InitIndexers()
logger.Info($"Loaded {nativeIndexers.Count} Native indexers: {string.Join(", ", nativeIndexers.Select(i => i.Id))}");
}

private void InitCardigannIndexers(IEnumerable<string> path)
private void InitCardigannIndexers(IEnumerable<FileInfo> files, ISet<string> indexersToLoad)
{
logger.Info("Loading Cardigann indexers from: " + string.Join(", ", path));

Expand All @@ -152,17 +180,19 @@ private void InitCardigannIndexers(IEnumerable<string> path)

try
{
var directoryInfos = path.Select(p => new DirectoryInfo(p));
var existingDirectories = directoryInfos.Where(d => d.Exists);
var files = existingDirectories.SelectMany(d => d.GetFiles("*.yml"));
var definitions = files.Select(file =>
{
logger.Debug("Loading Cardigann definition " + file.FullName);
try
{
var definitionString = File.ReadAllText(file.FullName);
var definition = deserializer.Deserialize<IndexerDefinition>(definitionString);
return definition;
if (GetIndexerShouldBeLoaded(definition.Id, indexersToLoad))
{
return definition;
}

return null;
}
catch (Exception e)
{
Expand Down Expand Up @@ -213,6 +243,14 @@ private void InitCardigannIndexers(IEnumerable<string> path)
logger.Info($"Loaded {indexers.Count} indexers in total");
}

private static IEnumerable<FileInfo> GetIndexerDefinitionFiles(IEnumerable<string> path)
{
var directoryInfos = path.Select(p => new DirectoryInfo(p));
var existingDirectories = directoryInfos.Where(d => d.Exists);
var files = existingDirectories.SelectMany(d => d.GetFiles("*.yml"));
return files;
}

public void InitAggregateIndexer()
{
var omdbApiKey = serverConfig.OmdbApiKey;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using Jackett.Common.Indexers;
using Newtonsoft.Json.Linq;

Expand All @@ -9,5 +10,6 @@ public interface IIndexerConfigurationService
void Save(IIndexer indexer, JToken config);
void Delete(IIndexer indexer);
string GetIndexerConfigFilePath(string indexerId);
IEnumerable<string> FindConfiguredIndexerIds();
}
}
17 changes: 17 additions & 0 deletions src/Jackett.Common/Utils/FilesystemUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace Jackett.Common.Utils
{
public static class FilesystemUtil
{
public static string getFileNameWithoutExtension(string fileName)
{
var fileParts = fileName.Split(".".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
if (fileParts.Length < 2)
{
return null;
}
return fileParts[0];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public IActionResult UpdateConfig([FromBody]Common.Models.DTO.ServerConfig confi
var saveDir = config.blackholedir;
var updateDisabled = config.updatedisabled;
var preRelease = config.prerelease;
var loadOnlyConfiguredIndexers = config.loadonlyconfiguredindexers;
var enhancedLogging = config.logging;

var basePathOverride = config.basepathoverride;
Expand All @@ -103,6 +104,7 @@ public IActionResult UpdateConfig([FromBody]Common.Models.DTO.ServerConfig confi
webHostRestartNeeded = true;
}

serverConfig.LoadOnlyConfiguredIndexers = loadOnlyConfiguredIndexers;
serverConfig.UpdateDisabled = updateDisabled;
serverConfig.UpdatePrerelease = preRelease;
serverConfig.BasePathOverride = basePathOverride;
Expand Down