private static Task reconstructTask(HttpServletRequest request) { Properties properties = new Properties(); Enumeration<?> paramNames = request.getParameterNames(); while (paramNames.hasMoreElements()) { String paramName = (String) paramNames.nextElement(); String paramValue = request.getParameter(paramName); properties.setProperty(paramName, paramValue); } String taskName = request.getHeader(TASK_NAME_REQUEST_HEADER); Task task = Task.fromProperties(taskName, properties); task.getQueueSettings().setDelayInSeconds(null); String queueName = request.getHeader(TASK_QUEUE_NAME_HEADER); if (queueName != null && !queueName.isEmpty()) { String onQueue = task.getQueueSettings().getOnQueue(); if (onQueue == null || onQueue.isEmpty()) { task.getQueueSettings().setOnQueue(queueName); } Map<String, Object> attributes = ApiProxy.getCurrentEnvironment().getAttributes(); attributes.put(TASK_QUEUE_NAME_HEADER, queueName); } return task; } }
/** * Wait for caching to finish, if there is time left in this request. */ @Override public void waitForCachingToFinish(List<Future<Integer>> pendingCachePuts) { Stopwatch stopwatch = Stopwatch.createStarted(); int columnCount = 0; for (Future<Integer> future : pendingCachePuts) { if (!future.isDone()) { long remainingMillis = ApiProxy.getCurrentEnvironment().getRemainingMillis(); if (remainingMillis > 100) { try { Integer cachedCount = future.get(remainingMillis - 50, TimeUnit.MILLISECONDS); columnCount += cachedCount; } catch (InterruptedException | TimeoutException e) { LOGGER.warning("Ran out of time while waiting for caching of results to complete."); return; } catch (ExecutionException e) { LOGGER.log(Level.WARNING, "Exception caching results of query", e); } } } } LOGGER.info("Waited " + stopwatch + " for " + columnCount + " columns to finish caching."); }
@Test public void testGetDefaultGcsBucketName() { ApiProxy.Environment env = ApiProxy.getCurrentEnvironment(); String expectedBucketName; Property property = property("testGetDefaultGcsBucketName"); if (property.exists()) { expectedBucketName = property.getPropertyValue(); } else { expectedBucketName = (String) env.getAttributes().get("com.google.appengine.runtime.default_version_hostname"); } try { String bucketName = appIdentity.getDefaultGcsBucketName(); Assert.assertEquals(expectedBucketName, bucketName); } catch (AppIdentityServiceFailureException aisfe) { //TODO: This means that there is no default bucket setup for this project. Have a better way to verify this. } }
/** * @return {@code true} if there is time and space left to continue adding more snapshots, * or if we should stop here. */ public boolean timeAndSpaceRemaining() { // Do we have enough time left in this request to do any more work? long timeRemaining = ApiProxy.getCurrentEnvironment().getRemainingMillis(); if(timeRemaining < BUFFER_MS) { return false; } if(estimatedSizeInBytes > MAX_RESPONSE_SIZE) { return false; } return true; }
ApiProxy.Environment proxyEnvironment = ApiProxy.getCurrentEnvironment(); Map<String, Object> attributes = proxyEnvironment.getAttributes(); String hostname = (String) attributes.get(LocalEnvironment.DEFAULT_VERSION_HOSTNAME);
public String getApp() { return ApiProxy.getCurrentEnvironment().getAppId(); }
public static String getCurrentRequestId() { return (String) ApiProxy.getCurrentEnvironment().getAttributes().get("com.google.appengine.runtime.request_log_id"); } }
public static ListeningExecutorService currentRequestExecutorService() { ThreadFactory factory = checkNotNull(ThreadManager.currentRequestThreadFactory(), "ThreadManager.currentRequestThreadFactory()"); // GAE requests cannot exceed 10 threads per request int maxThreads = 10; long keepAlive = ApiProxy.getCurrentEnvironment().getRemainingMillis(); ExecutorService pool = newScalingThreadPool(0, maxThreads, keepAlive, factory); return WithSubmissionTrace.wrap(MoreExecutors.listeningDecorator(pool)); }
@Test public void testGetAppId() { String expectedAppId = getExpectedAppId("testGetAppId"); String appId = ApiProxy.getCurrentEnvironment().getAppId(); // AppIds in the US are prefixed with s~ and in Europe e~ so just match the end. String errMsg = "The appId should end with " + expectedAppId + ", but was " + appId; Assert.assertTrue(errMsg, appId.endsWith(expectedAppId)); }
protected static URL getUrl(String path) throws MalformedURLException { ApiProxy.Environment env = ApiProxy.getCurrentEnvironment(); Object localhost = env.getAttributes().get("com.google.appengine.runtime.default_version_hostname"); return new URL("http://" + localhost + "/" + path); }
@Test public void testRequestId() { ApiProxy.Environment env = ApiProxy.getCurrentEnvironment(); String requestId = (String) env.getAttributes().get("com.google.appengine.runtime.request_log_id"); String errMsg = "The request id should not be null"; Assert.assertNotNull(errMsg, requestId); } }
@Test public void testInstanceId() { ApiProxy.Environment env = ApiProxy.getCurrentEnvironment(); String instanceId = (String) env.getAttributes().get("com.google.appengine.instance.id"); String errMsg = "The instance id should not be null"; Assert.assertNotNull(errMsg, instanceId); }
import com.google.apphosting.api.ApiProxy; ... final ApiProxy.Environment env = ApiProxy.getCurrentEnvironment(); final Map<String, Object> attributes = env.getAttributes(); final String hostAndPort = (String) attributes.get("com.google.appengine.runtime.default_version_hostname"); final String url = "http://" + hostAndPort + "/";
@Test public void testParseFullAppId() { // [(partition)~][(domain):](display-app-id) ApiProxy.Environment env = ApiProxy.getCurrentEnvironment(); String hostname = (String) env.getAttributes().get("com.google.appengine.runtime.default_version_hostname"); AppIdentityService.ParsedAppId parsed = appIdentity.parseFullAppId(hostname); String message = createParsed(parsed); Assert.assertEquals(message, property("testParseFullAppId_partition").getPropertyValue(), parsed.getPartition()); Assert.assertEquals(message, getExpectedAppHostname("testParseFullAppId_domain"), parsed.getDomain()); Assert.assertEquals(message, getExpectedAppId("testParseFullAppId_id"), parsed.getId()); }
private static Queue getQueue(String queueName) { if (queueName == null) { Map<String, Object> attributes = ApiProxy.getCurrentEnvironment().getAttributes(); queueName = (String) attributes.get(TaskHandler.TASK_QUEUE_NAME_HEADER); } return queueName == null ? QueueFactory.getDefaultQueue() : QueueFactory.getQueue(queueName); }
public static ListeningExecutorService currentRequestExecutorService() { ThreadFactory factory = checkNotNull(ThreadManager.currentRequestThreadFactory(), "ThreadManager.currentRequestThreadFactory()"); // GAE requests cannot exceed 10 threads per request int maxThreads = 10; long keepAlive = ApiProxy.getCurrentEnvironment().getRemainingMillis(); ExecutorService pool = newScalingThreadPool(0, maxThreads, keepAlive, factory); return WithSubmissionTrace.wrap(MoreExecutors.listeningDecorator(pool)); }
@Test public void testGetVersionedHostname() { String expectedHostname = getExpectedAppHostname("testGetVersionedHostname"); ApiProxy.Environment env = ApiProxy.getCurrentEnvironment(); String hostname = (String) env.getAttributes().get("com.google.appengine.runtime.default_version_hostname"); String errMsg = "The versioned hostname should end with " + expectedHostname + ", but was " + hostname; Assert.assertTrue(errMsg, hostname.endsWith(expectedHostname)); }