App Engine Instance ID - google-app-engine

Is it possible to get info on what instance you're running on? I want to output just a simple identifier for which instance the code is currently running on for logging purposes.

Since there is no language tag, and seeing your profile history, I assume you are using GAE/J?
In that case, the instance ID information is embedded in one of the environment attributes that you could get via ApiProxy.getCurrentEnvironment() method. You could then extract the instance id from the resulting map using key BackendService.INSTANCE_ID_ENV_ATTRIBUTE.
Even though the key is stored in BackendService, this approach will also work for frontend instances. So in summary, the following code would fetch the instance ID for you:
String tInstanceId = ApiProxy.getCurrentEnvironment()
.getAttributes()
.get( BackendService.INSTANCE_ID_ENV_ATTRIBUTE )
.toString();
Please keep in mind that this approach is quite undocumented by Google, and might subject to change without warning in the future. But since your use case is only for logging, I think it would be sufficient for now.

With the advent of Modules, you can get the current instance id in a more elegant way:
ModulesServiceFactory.getModulesService().getCurrentInstanceId()

Even better, you should wrap the call in a try catch so that it will work correctly locally too.
Import this
import com.google.appengine.api.modules.ModulesException;
import com.google.appengine.api.modules.ModulesServiceFactory;
Then your method can run this
String instanceId = "unknown";
try{
instanceId = ModulesServiceFactory.getModulesService().getCurrentInstanceId();
} catch (ModulesException e){
instanceId = e.getMessage();
}
Without the try catch, you will get some nasty errors when running locally.
I have found this super useful for debugging when using endpoints mixed with pub-sub and other bits to try to determine why some things work differently and to determine if it is related to new instances.

Not sure about before, but today in 2021 the system environment variable GAE_INSTANCE appears to contain the instance id:
instanceId = System.getenv("GAE_INSTANCE")

Related

CPQ Quote API, I can't save the quote

I can't save the quote.
Doing the query:
select
ApexClass.name, Id, CreatedDate, CreatedById, JobType,
ApexClassId, Status, JobItemsProcessed, TotalJobItems,
NumberOfErrors, CompletedDate, MethodName, ExtendedStatus,
ParentJobId, LastProcessed, LastProcessedOffset
from
AsyncApexJob
order by
CreatedDate desc
I get this error:
Calculation error on quote Q-13761: "UNAUTHORIZED"
Code:
public with sharing class QuoteCalculator {
public void calculate(QuoteModel quote, String callbackClass) {
system.debug('quote: ' +quote);
system.debug('callbackClass: ' +callbackClass);
QuoteCalculatorContext ctx = new QuoteCalculatorContext(quote, callbackClass);
SBQQ.ServiceRouter.load('SBQQ.QuoteAPI.QuoteCalculator', null, JSON.serialize(ctx));
system.debug('QuoteCalculator.calculate');
}
private class QuoteCalculatorContext {
private QuoteModel quote; //The quote and callbackClass properties are called
in the API code by the exact names seen here.
private String callbackClass; //Altering these property names will cause
calculator API calls to fail.
private QuoteCalculatorContext(QuoteModel quote, String callbackClass) {
this.quote = quote;
this.callbackClass = callbackClass;
}
}
}
anonymous window:
QuoteReader reader = new QuoteReader();
QuoteModel quote = reader.read('a0p1w000BhfXzAAJ');
System.debug(quote);
quote.lineItems[0].record.SBQQ__Quantity__c = 2;
QuoteCalculator calculator = new QuoteCalculator();
calculator.calculate(quote, 'MyCallback')
Preface
I had (almost) the same exact code base as yours, and got the same error message.
In my case there was an other sandbox I could test my code, and it turned out to be working properly there.
Cause
Later found out that the Salesforce CPQ's Calculation Quote API is using Heroku to do the calculations in order to avoid apex limits exhaustion.
From this it can be deducted, that it needs to have a Connected App. I checked the Apps -> Connected Apps setup, and found that no record was listed under the "Connected Apps OAuth Usage" page for the Salesforce CPQ. (On my other sandbox there was a "Steelbrick CPQ" row.)
From this I concluded that this might be the reason for this behaviour.
Seems like something went wrong during the "Authorize new Calculation Service" process. (Or there was a sandbox refresh and something else went wrong during it.)
Solution
The bad news is that the option to authorize a new calculation service is only visible for the first time you configure the package, which you might already done. (Well... if you haven't done, then this is a great news, because your problem is probably solved. :D) (Otherwise read further.)
The good news is I figured out a solution for the case when you already done this, yet that "Steelbrick CPQ" row is missing.
Created a scratch org and installed the Salesforce CPQ package, then before I clicked on the "Authorize new Calculation Service" link under the "Pricing and Calculation" tab in the Settings Editor, I checked the source code in hope of finding something of interest.
I did.
This link: https://rest-na.steelbrick.com/oauth/auth/https%3A%2F%2Ftest.salesforce.com/SBQQ
(⚠️NOTE: You might have to change it according to your location. There are several servers across the globe:
rest-au.steelbrick.com
rest-eu.steelbrick.com
rest-jp.steelbrick.com
rest-na.steelbrick.com
But for me the above pasted link was generated on the settings page. Which is only interesting, because I live in the EU, yet, for some reason I got the link to the rest-NA server... whatever.gif
So just make sure if you click on the link, in the address bar you can find the appropriate salesforce instance URL.)
Conclusion
With this link you won't have to reinstall the package, you just have to click on it, and allow the access from Steelbrick and the missing row will appear, and you will be authorized to use the Calculation API.

How to get the device fingerprint

I am trying to integrate a payment API in my application and one of the required field is the device fingerprint. I have googled and all I got was the fingerprint scanner and touch Id support which aren't what I need. Any ideas?
Realistically speaking, perhaps the simplest thing you can do is not to obtain or calculate a unique device identifier, but to assign each specific installation of your app a unique code (which will therefore change if the app is uninstalled and reinstalled).
I'll try to give you an example.
In the init() method of the main class, you can add this code:
// Register the device (assigning it an unique deviceId)
// The deviceId is used also by: https://www.codenameone.com/javadoc/com/codename1/io/Util.html#getUUID--
registerDevice();
Your registerDevice() implementation could set the deviceId to identify this app installation. The best and most reliable solution, in this case, is to get a UUID from your server, e.g. like this code:
// Sets the device unique identifier
Rest.get(Server.getRestServerURL() + "/uniqueIdentifier").fetchAsString((Response<String> response) -> {
String deviceId = response.getResponseData();
Preferences.set("deviceId", deviceId);
Log.p("DeviceId: " + deviceId, Log.INFO);
});
The server-side code is quite simple. Staying in the Java world, and assuming we have a Spring Boot server, the code could be:
#GetMapping("/uniqueIdentifier")
#ResponseBody
public String uniqueIdentifier() {
return UUID.randomUUID().toString().replace("-", "");
}
This solution should be enough. Note that UUIDs must be unique, but they are not designed to be unpredictable. You can see a discussion about this here: https://stackoverflow.com/a/41156/1277576
Alternatively, if your code is to be client-side only, read this Javadoc carefully: https://www.codenameone.com/javadoc/com/codename1/io/Util.html#getUUID--
Util.getUUID() returns a pseudo-random Universally Unique Identifier in its canonical textual representation. This could be enough in most cases.
These are just basic suggestions. Obviously you will need to develop the code so that the deviceId is requested once.

Make a solr query from Geotools through geoserver

I come here because I am searching (like the title mentionned) to do a query from geotools (through geoserver) to get feature from a solr index.
To be more precise :
I saw on geoserver user manual that i can do query on solr like this in http :
http://localhost:8080/geoserver/wfs?service=WFS&version=1.1.0&request=GetFeature
&typeName=mySolrLayer
&format="xxx"
&viewparams=q:"mySolrQuery"
The important part on this URL is the viewparams that I want to use directly from geotools.
I have already test this case (this is a part of my code):
url = new URL(
"http://localhost:8080/geoserver/wfs?request=GetCapabilities&VERSION=1.1.0";
);
Map<String, String> param = new HashMap();
params.put(WFSDataStoreFactory.URL.key, url);
param.put("viewparams","q:myquery");
Hints hints = new Hints();
hints.put(Hints.VIRTUAL_TABLE_PARAMETERS, viewParams);
query.setHints(hints);
...
featureSource.getFeatures(query);
But here, it seems to doesn't work, the url send to geoserver is a normal "GET FEATURE" request without the viewparams parameter.
I tried this with geotools-12.2 ; geotools-13.2 and geotools-15-SNAPSHOT but I didn't succeed to pass the query, geoserver send me all the feature in my database and doesn't take "viewparams" as a param.
I need to do it like this because actually the query come from another program and I would easily communicate this query to another part of the project...
If someone can help me ?
There doesn't currently seem to be a way to do this in the GeoTool's WFSDatastore implementations as the GetFeature request is constructed from the URL provided by the getCapabilities document. This is as the standard requires but it may be worth making a feature enhancement request to allow clients to override this string (as QGIS does for example) which would let you specify the additional parameter in your base URL which would then be passed to the server as you need.
Unfortunately the WFS module lives in Unsupported land at present so unless you have resources to work on this issue yourself and can provide a PR to implement it there is not a great chance of it being implemented.

Play-slick - Is default.db required?

I'm working on an application using Play and Slick. This app requires access to (at least) two databases and this is working fine when one is defined as default and other is named. Eg.,
db.default.driver = "com.mysql.jdbc.Driver"
db.default.url = "jdbc:mysql://localhost:3306/db1"
db.db2.driver = "com.mysql.jdbc.Driver"
db.db2.url = "jdbc:mysql://localhost:3306/db2"
I can then happily access each db as follows
DB.withSession { implicit session => ??? }
DB("db2").withSession { implicit session => ??? }
However, this doesn't really make sense as there is no reason DB1 should be the default. The DBs contain different types of data, neither is the default, both are important. What I would like is:
db.db1.driver = "com.mysql.jdbc.Driver"
db.db1.url = "jdbc:mysql://localhost:3306/db1"
db.db2.driver = "com.mysql.jdbc.Driver"
db.db2.url = "jdbc:mysql://localhost:3306/db2"
Play-scala barfs at this thought. It needs a default db driver and URL and it needs to be able to connect to it.
Anyone know anyway to change this behaviour or to trick play into thinking it has a default?
UPDATE
To be clear, I've greped my code to ensure that I'm not using DB.withSession anywhere. That is, every time I create a session I use DB("db1").withSession or DB("db2").withSession. However, when I run my test, I still get an exception:
Caused by: Configuration error: Configuration error[Slick error : jdbc driver not defined in application.conf for db.default.driver key]
Something somewhere is trying to load the default config.
Default is just a name, with some convenience functions (withSession and withTransaction without name), so, no you do not need to have a default connection if it does not fit your project.

WCF Data Service and Silverlight: DataServiceQuery<T> will not re-perform query

Using Silverlight 4, Oracle 11g, and Entity Framework 4.
I use a DataServiceQuery to fill a DataGrid. Then, some local (non-EF) code updates the DB. I would like to use the same query to refresh the DataGrid with the updated/new data. The problem is, when I do that, it returns the old, original results. I have verified that the changes have, in fact, been committed to the DB prior to this code running:
DataServiceContext<T> dsContext= new DataServiceContext<T>(uri);
dsContext.MergeOption = MergeOption.NoTracking;
dsContext.SaveChangesDefaultOptions = SaveChangesOptions.ReplaceOnUpdate;
DataServiceQuery<T> dsQuery = dsContext.CreateQuery<T>(typeof(T).Name);
// oldQuery is an IQueryable<T>
dsQuery = (DataServiceQuery<T>)oldQuery;
var dsQuery = (DataServiceQuery<T>)oldQuery;
dsQuery.BeginExecute(new AsyncCallback(c =>
{
IEnumerable<T> result = dsQuery.EndExecute(c);
listSelectedRecord = new List<T>();
listSelectedRecord = result.ToList();
}), dsQuery);
As far as I can tell, the new dsQuery is not even being sent to the Oracle server, even though a new DataServiceContext is being created. It is apparently discovering that it has a cached copy somewhere. If I type the query into a browser, it returns the updated results.
Any suggestions on how to force the DS to reperform the query?
Assigning oldQuery to the newly instantiated dsQuery object apparently copies cached results from somewhere. The solution I used here was to replace
dsQuery = (DataServiceQuery<T>)oldQuery;
with
dsQuery = (DataServiceQuery<T>)(dsQuery.Provider.CreateQuery<T>(oldQuery.Expression));
I would still like to understand exactly where, why, and how these results are being cached. The DataServiceQuery<T> docs don't say anything about this:
http://msdn.microsoft.com/en-us/library/cc646574(v=vs.95).aspx
You can try deleting your .suo file if you see this behavior when debugging.
The astonishing answers to this question seem to be found here:
Does Silverlight cache web service calls?
and
https://connect.microsoft.com/VisualStudio/feedback/details/340931/silverlight-webclient-does-not-download-updated-resources
There seems to be be some sort of vague consensus that SLx does indeed rely on the browser cache, not that you would know that from the DataService, DataServiceContext, or DataServiceQuery docs.
So the easiest fix for this, in IE 8 at least, is to turn off browser caching.

Resources