Skip to content

Commit 149b079

Browse files
committed
Reinstate maven-shade-plugin for building the invoker. This makes it much easier to run the invoker standalone, as a Functions Framework implementation.
Also add logic to guess a function signature type. This is only invoked if the environment variable FUNCTION_SIGNATURE_TYPE is not set. It always will be in GCF, but might not be when invoking the standalone Functions Framework. PiperOrigin-RevId: 292656253
1 parent 6544edc commit 149b079

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

invoker/core/pom.xml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,23 @@
157157
<archive>
158158
<manifest>
159159
<mainClass>com.google.cloud.functions.invoker.runner.Invoker</mainClass>
160-
<addClasspath>true</addClasspath>
161-
<classpathPrefix>lib</classpathPrefix>
162160
</manifest>
163161
</archive>
164162
</configuration>
165163
</plugin>
164+
<plugin>
165+
<groupId>org.apache.maven.plugins</groupId>
166+
<artifactId>maven-shade-plugin</artifactId>
167+
<version>3.2.1</version>
168+
<executions>
169+
<execution>
170+
<phase>package</phase>
171+
<goals>
172+
<goal>shade</goal>
173+
</goals>
174+
</execution>
175+
</executions>
176+
</plugin>
166177
</plugins>
167178
</build>
168179
</project>

invoker/core/src/main/java/com/google/cloud/functions/invoker/runner/Invoker.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ public void startServer() throws Exception {
161161
classLoader = runtimeLoader;
162162
}
163163

164+
HttpServlet servlet;
164165
if ("http".equals(functionSignatureType)) {
165-
HttpServlet servlet;
166166
Optional<NewHttpFunctionExecutor> newExecutor =
167167
NewHttpFunctionExecutor.forTarget(functionTarget, classLoader);
168168
if (newExecutor.isPresent()) {
@@ -173,9 +173,7 @@ public void startServer() throws Exception {
173173
HttpCloudFunction function = loader.loadUserFunction();
174174
servlet = new HttpFunctionExecutor(function);
175175
}
176-
context.addServlet(new ServletHolder(servlet), "/*");
177176
} else if ("event".equals(functionSignatureType)) {
178-
HttpServlet servlet;
179177
Optional<NewBackgroundFunctionExecutor> newExecutor =
180178
NewBackgroundFunctionExecutor.forTarget(functionTarget, classLoader);
181179
if (newExecutor.isPresent()) {
@@ -187,10 +185,33 @@ public void startServer() throws Exception {
187185
BackgroundCloudFunction function = loader.loadUserFunction();
188186
servlet = new BackgroundFunctionExecutor(function);
189187
}
190-
context.addServlet(new ServletHolder(servlet), "/*");
188+
} else if (functionSignatureType == null) {
189+
Optional<NewHttpFunctionExecutor> httpExecutor =
190+
NewHttpFunctionExecutor.forTarget(functionTarget, classLoader);
191+
if (httpExecutor.isPresent()) {
192+
servlet = httpExecutor.get();
193+
} else {
194+
Optional<NewBackgroundFunctionExecutor> backgroundExecutor =
195+
NewBackgroundFunctionExecutor.forTarget(functionTarget, classLoader);
196+
if (backgroundExecutor.isPresent()) {
197+
servlet = backgroundExecutor.get();
198+
} else {
199+
String error = String.format(
200+
"Could not determine function signature type from target %s. Either this should be"
201+
+ " a class implementing one of the interfaces in com.google.cloud.functions, or the"
202+
+ " environment variable FUNCTION_SIGNATURE_TYPE should be set to \"http\" or"
203+
+ " \"event\".",
204+
functionTarget);
205+
throw new RuntimeException(error);
206+
}
207+
}
191208
} else {
192-
throw new RuntimeException("Unknown function signature type: " + functionSignatureType);
209+
String error = String.format(
210+
"Function signature type %s is unknown; should be \"http\" or \"event\"",
211+
functionSignatureType);
212+
throw new RuntimeException(error);
193213
}
214+
context.addServlet(new ServletHolder(servlet), "/*");
194215

195216
server.start();
196217
logServerInfo();

0 commit comments

Comments
 (0)