Skip to content

Commit 9d7d401

Browse files
authored
Reverting to host initialization exception (#3855)
1 parent 7471c1c commit 9d7d401

File tree

3 files changed

+93
-11
lines changed

3 files changed

+93
-11
lines changed

src/WebJobs.Script/Host/ScriptHost.cs

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

717-
if (!Utility.IsSingleLanguage(functions, _currentRuntimeLanguage))
718-
{
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-
}
727-
}
717+
Utility.VerifyFunctionsMatchSpecifiedLanguage(functions, _currentRuntimeLanguage);
728718

729719
foreach (FunctionMetadata metadata in functions)
730720
{

src/WebJobs.Script/Utility.cs

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

442+
internal static void VerifyFunctionsMatchSpecifiedLanguage(IEnumerable<FunctionMetadata> functions, string currentRuntimeLanguage)
443+
{
444+
if (!IsSingleLanguage(functions, currentRuntimeLanguage))
445+
{
446+
if (string.IsNullOrEmpty(currentRuntimeLanguage))
447+
{
448+
throw new HostInitializationException($"Found functions with more than one language. Select a language for your function app by specifying {LanguageWorkerConstants.FunctionWorkerRuntimeSettingName} AppSetting");
449+
}
450+
else
451+
{
452+
throw new HostInitializationException($"Did not find functions with language [{currentRuntimeLanguage}].");
453+
}
454+
}
455+
}
456+
442457
internal static bool IsSingleLanguage(IEnumerable<FunctionMetadata> functions, string currentRuntimeLanguage)
443458
{
444459
if (functions == null)

test/WebJobs.Script.Tests/ScriptHostTests.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,83 @@ private static IEnumerable<FunctionMetadata> GetDotNetFunctionsMetadata()
10881088
return functionsList;
10891089
}
10901090

1091+
[Fact]
1092+
public void VerifyFunctionsMatchSpecifiedLanguage_Throws_For_UnmatchedLanguage_With_RuntimeLanguage_Specified()
1093+
{
1094+
FunctionMetadata funcJS1 = new FunctionMetadata()
1095+
{
1096+
Name = "funcJS1",
1097+
Language = "node"
1098+
};
1099+
IEnumerable<FunctionMetadata> functionsList = new Collection<FunctionMetadata>()
1100+
{
1101+
funcJS1
1102+
};
1103+
1104+
HostInitializationException ex = Assert.Throws<HostInitializationException>(() => Utility.VerifyFunctionsMatchSpecifiedLanguage(functionsList, LanguageWorkerConstants.DotNetLanguageWorkerName));
1105+
Assert.Equal($"Did not find functions with language [{LanguageWorkerConstants.DotNetLanguageWorkerName}].", ex.Message);
1106+
}
1107+
1108+
[Fact]
1109+
public void VerifyFunctionsMatchSpecifiedLanguage_NoThrow_For_MixedLanguageMatching_With_RuntimeLanguage_Specified()
1110+
{
1111+
FunctionMetadata funcJS1 = new FunctionMetadata()
1112+
{
1113+
Name = "funcJS1",
1114+
Language = "node"
1115+
};
1116+
// CSharp matches dotnet so we should be able to initialize the host
1117+
FunctionMetadata funcCS1 = new FunctionMetadata()
1118+
{
1119+
Name = "funcJS1",
1120+
Language = "csharp"
1121+
};
1122+
IEnumerable<FunctionMetadata> functionsList = new Collection<FunctionMetadata>()
1123+
{
1124+
funcJS1, funcCS1
1125+
};
1126+
1127+
Utility.VerifyFunctionsMatchSpecifiedLanguage(functionsList, LanguageWorkerConstants.DotNetLanguageWorkerName);
1128+
}
1129+
1130+
[Fact]
1131+
public void VerifyFunctionsMatchSpecifiedLanguage_NoThrow_For_SingleLanguage_Without_RuntimeLanguage_Specified()
1132+
{
1133+
FunctionMetadata funcJS1 = new FunctionMetadata()
1134+
{
1135+
Name = "funcJS1",
1136+
Language = "node"
1137+
};
1138+
IEnumerable<FunctionMetadata> functionsList = new Collection<FunctionMetadata>()
1139+
{
1140+
funcJS1
1141+
};
1142+
1143+
Utility.VerifyFunctionsMatchSpecifiedLanguage(functionsList, string.Empty);
1144+
}
1145+
1146+
[Fact]
1147+
public void VerifyFunctionsMatchSpecifiedLanguage_Throws_For_NotSingleLanguage_Without_RuntimeLanguage_Specified()
1148+
{
1149+
FunctionMetadata funcJS1 = new FunctionMetadata()
1150+
{
1151+
Name = "funcJS1",
1152+
Language = "node"
1153+
};
1154+
FunctionMetadata funcCS1 = new FunctionMetadata()
1155+
{
1156+
Name = "funcCS1",
1157+
Language = "csharp"
1158+
};
1159+
IEnumerable<FunctionMetadata> functionsList = new Collection<FunctionMetadata>()
1160+
{
1161+
funcJS1, funcCS1
1162+
};
1163+
1164+
HostInitializationException ex = Assert.Throws<HostInitializationException>(() => Utility.VerifyFunctionsMatchSpecifiedLanguage(functionsList, string.Empty));
1165+
Assert.Equal($"Found functions with more than one language. Select a language for your function app by specifying {LanguageWorkerConstants.FunctionWorkerRuntimeSettingName} AppSetting", ex.Message);
1166+
}
1167+
10911168
[Fact]
10921169
public async Task InitializeRpcService_Throws()
10931170
{

0 commit comments

Comments
 (0)