Skip to content

Commit 7471c1c

Browse files
pragnagopamaiqbal11
authored andcommitted
Throw error if there are no functions matching the language set by the runtime (#3599)
* Update IsSingleLanguage to return false if there are no functions matching the FUNCTIONS_RUNTIME_WORKER * Update exception message * Addressing comments * Adding exception case to check if functions are dotnet * Fixing null argument tests * Warning user rather than throwing * Throwing argument null exception * Argument name for null exception * Using nameof to get argument name * Fixing typo
1 parent 8ad9dd8 commit 7471c1c

File tree

3 files changed

+95
-13
lines changed

3 files changed

+95
-13
lines changed

src/WebJobs.Script/Host/ScriptHost.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -714,9 +714,16 @@ internal async Task<Collection<FunctionDescriptor>> GetFunctionDescriptorsAsync(
714714
Collection<FunctionDescriptor> functionDescriptors = new Collection<FunctionDescriptor>();
715715
var httpFunctions = new Dictionary<string, HttpTriggerAttribute>();
716716

717-
if (!_scriptHostEnvironment.IsDevelopment() && !Utility.IsSingleLanguage(functions, _currentRuntimeLanguage))
717+
if (!Utility.IsSingleLanguage(functions, _currentRuntimeLanguage))
718718
{
719-
throw new HostInitializationException($"Found functions with more than one language. Select a language for your function app by specifying {LanguageWorkerConstants.FunctionWorkerRuntimeSettingName} AppSetting");
719+
if (string.IsNullOrEmpty(_currentRuntimeLanguage))
720+
{
721+
_logger.LogWarning($"Select a language for your function app by specifying {LanguageWorkerConstants.FunctionWorkerRuntimeSettingName} AppSetting");
722+
}
723+
else
724+
{
725+
_logger.LogWarning($"Did not find functions with language [{_currentRuntimeLanguage}].");
726+
}
720727
}
721728

722729
foreach (FunctionMetadata metadata in functions)

src/WebJobs.Script/Utility.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -439,17 +439,22 @@ internal static bool TryReadFunctionConfig(string scriptDir, out string json, IF
439439
return true;
440440
}
441441

442-
internal static bool IsSingleLanguage(IEnumerable<FunctionMetadata> functions, string language)
442+
internal static bool IsSingleLanguage(IEnumerable<FunctionMetadata> functions, string currentRuntimeLanguage)
443443
{
444-
if (string.IsNullOrEmpty(language))
444+
if (functions == null)
445445
{
446-
if (functions != null && functions.Any())
447-
{
448-
var functionsListWithoutProxies = functions.Where(f => f.IsProxy == false);
449-
return functionsListWithoutProxies.Select(f => f.Language).Distinct().Count() <= 1;
450-
}
446+
throw new ArgumentNullException(nameof(functions));
451447
}
452-
return true;
448+
var functionsListWithoutProxies = functions.Where(f => f.IsProxy == false).ToArray();
449+
if (functionsListWithoutProxies.Length == 0)
450+
{
451+
return true;
452+
}
453+
if (string.IsNullOrEmpty(currentRuntimeLanguage))
454+
{
455+
return functionsListWithoutProxies.Select(f => f.Language).Distinct().Count() <= 1;
456+
}
457+
return ContainsFunctionWithCurrentLanguage(functionsListWithoutProxies, currentRuntimeLanguage);
453458
}
454459

455460
internal static bool ShouldInitiliazeLanguageWorkers(IEnumerable<FunctionMetadata> functions, string currentRuntimeLanguage)
@@ -477,6 +482,10 @@ private static bool ContainsNonDotNetFunctions(IEnumerable<FunctionMetadata> fun
477482

478483
private static bool ContainsFunctionWithCurrentLanguage(IEnumerable<FunctionMetadata> functions, string currentLanguage)
479484
{
485+
if (string.Equals(currentLanguage, LanguageWorkerConstants.DotNetLanguageWorkerName, StringComparison.OrdinalIgnoreCase))
486+
{
487+
return functions.Any(f => dotNetLanguages.Any(l => l.Equals(f.Language, StringComparison.OrdinalIgnoreCase)));
488+
}
480489
if (functions != null && functions.Any())
481490
{
482491
return functions.Any(f => f.Language.Equals(currentLanguage, StringComparison.OrdinalIgnoreCase));

test/WebJobs.Script.Tests/ScriptHostTests.cs

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,47 @@ public void IsSingleLanguage_Returns_True_Proxy()
781781
}
782782

783783
[Fact]
784-
public void IsSingleLanguage_FunctionsWorkerRuntime_Set_Returns_True_()
784+
public void IsSingleLanguage_Returns_True_OnlyProxies()
785+
{
786+
FunctionMetadata proxy1 = new FunctionMetadata()
787+
{
788+
Name = "proxy",
789+
IsProxy = true
790+
};
791+
FunctionMetadata proxy2 = new FunctionMetadata()
792+
{
793+
Name = "proxy",
794+
IsProxy = true
795+
};
796+
IEnumerable<FunctionMetadata> functionsList = new Collection<FunctionMetadata>()
797+
{
798+
proxy1, proxy2
799+
};
800+
Assert.True(Utility.IsSingleLanguage(functionsList, null));
801+
}
802+
803+
[Fact]
804+
public void IsSingleLanguage_FunctionsWorkerRuntime_Set_Returns_True_OnlyProxies()
805+
{
806+
FunctionMetadata proxy1 = new FunctionMetadata()
807+
{
808+
Name = "proxy",
809+
IsProxy = true
810+
};
811+
FunctionMetadata proxy2 = new FunctionMetadata()
812+
{
813+
Name = "proxy",
814+
IsProxy = true
815+
};
816+
IEnumerable<FunctionMetadata> functionsList = new Collection<FunctionMetadata>()
817+
{
818+
proxy1, proxy2
819+
};
820+
Assert.True(Utility.IsSingleLanguage(functionsList, "python"));
821+
}
822+
823+
[Fact]
824+
public void IsSingleLanguage_FunctionsWorkerRuntime_Set_Returns_True()
785825
{
786826
FunctionMetadata funcPython1 = new FunctionMetadata()
787827
{
@@ -805,6 +845,26 @@ public void IsSingleLanguage_FunctionsWorkerRuntime_Set_Returns_True_()
805845
Assert.True(Utility.IsSingleLanguage(functionsList, "node"));
806846
}
807847

848+
[Fact]
849+
public void IsSingleLanguage_FunctionsWorkerRuntime_Set_Returns_False()
850+
{
851+
FunctionMetadata funcPython1 = new FunctionMetadata()
852+
{
853+
Name = "funcPython1",
854+
Language = "python",
855+
};
856+
FunctionMetadata funcCSharp1 = new FunctionMetadata()
857+
{
858+
Name = "funcCSharp1",
859+
Language = "CSharp",
860+
};
861+
IEnumerable<FunctionMetadata> functionsList = new Collection<FunctionMetadata>()
862+
{
863+
funcPython1, funcCSharp1
864+
};
865+
Assert.False(Utility.IsSingleLanguage(functionsList, "node"));
866+
}
867+
808868
[Fact]
809869
public void IsSingleLanguage_Returns_False()
810870
{
@@ -826,9 +886,15 @@ public void IsSingleLanguage_Returns_False()
826886
}
827887

828888
[Fact]
829-
public void IsSingleLanguage_FunctionsList_Null_Returns_False()
889+
public void IsSingleLanguage_FunctionsList_Null_FunctionsWorkerRuntime_Throws_ArgumentNullException()
890+
{
891+
Assert.Throws<ArgumentNullException>(() => Utility.IsSingleLanguage(null, "dotnet"));
892+
}
893+
894+
[Fact]
895+
public void IsSingleLanguage_FunctionsList_Null_Throws_ArgumentNullException()
830896
{
831-
Assert.True(Utility.IsSingleLanguage(null, null));
897+
Assert.Throws<ArgumentNullException>(() => Utility.IsSingleLanguage(null, null));
832898
}
833899

834900
[Fact]

0 commit comments

Comments
 (0)