How to stop Firebase Functions from executing local functions once per every Cloud Function?
So, I am using the spotify API to do searches. Locally, I tested my code using tsc and node; everything works as expected.
However, when I deploy my index.ts the logs indicate some odd behavior.
At the top of the file I have the following:
//Load libraries
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const firebase = require("firebase");
const stripe = require('stripe')(functions.config().stripe.token)
const SpotifyWebApi = require('spotify-web-api-node');
//Initialize app
const APP = {
apiKey: "my-API-Key",
authDomain: "My domain auth",
databaseURL: "my databse url",
storageBucket: "my bucket",
messagingSenderId: "my messenger ID"
}
//The Client Credential Flow for Spotify API
const spotifyApi = new SpotifyWebApi({
clientId: 'My cliend ID',
clientSecret: 'My-secret-Key'
});
// Retrieve an access token for Spotify.
spotifyApi.clientCredentialsGrant().then(function (data) {
console.log('All data Spotify API: ' + data.body )
console.log('The access token expires in ' + data.body['expires_in']);
console.log('The access token is ' + data.body['access_token']);
// Save the access token so that it's used in future calls
spotifyApi.setAccessToken(data.body['access_token']);
}, function (err) {
console.log('Something went wrong when retrieving an access token', err);
})
firebase.initializeApp(APP);
admin.initializeApp(APP)
The problem is that the logs indicate that the spotify access token function runs once per Firebase-Cloud Function in my index.js file. I am getting over a dozen tokens, each shows up in the log as called from one of my firebase-cloud functions.
How do I tell Firebase Functions to run this code once and only once when I upload my entire index.ts? And not run it when I upload a single function; that would be useful too.
Thanks.
javascript typescript firebase google-cloud-functions
add a comment |
So, I am using the spotify API to do searches. Locally, I tested my code using tsc and node; everything works as expected.
However, when I deploy my index.ts the logs indicate some odd behavior.
At the top of the file I have the following:
//Load libraries
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const firebase = require("firebase");
const stripe = require('stripe')(functions.config().stripe.token)
const SpotifyWebApi = require('spotify-web-api-node');
//Initialize app
const APP = {
apiKey: "my-API-Key",
authDomain: "My domain auth",
databaseURL: "my databse url",
storageBucket: "my bucket",
messagingSenderId: "my messenger ID"
}
//The Client Credential Flow for Spotify API
const spotifyApi = new SpotifyWebApi({
clientId: 'My cliend ID',
clientSecret: 'My-secret-Key'
});
// Retrieve an access token for Spotify.
spotifyApi.clientCredentialsGrant().then(function (data) {
console.log('All data Spotify API: ' + data.body )
console.log('The access token expires in ' + data.body['expires_in']);
console.log('The access token is ' + data.body['access_token']);
// Save the access token so that it's used in future calls
spotifyApi.setAccessToken(data.body['access_token']);
}, function (err) {
console.log('Something went wrong when retrieving an access token', err);
})
firebase.initializeApp(APP);
admin.initializeApp(APP)
The problem is that the logs indicate that the spotify access token function runs once per Firebase-Cloud Function in my index.js file. I am getting over a dozen tokens, each shows up in the log as called from one of my firebase-cloud functions.
How do I tell Firebase Functions to run this code once and only once when I upload my entire index.ts? And not run it when I upload a single function; that would be useful too.
Thanks.
javascript typescript firebase google-cloud-functions
add a comment |
So, I am using the spotify API to do searches. Locally, I tested my code using tsc and node; everything works as expected.
However, when I deploy my index.ts the logs indicate some odd behavior.
At the top of the file I have the following:
//Load libraries
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const firebase = require("firebase");
const stripe = require('stripe')(functions.config().stripe.token)
const SpotifyWebApi = require('spotify-web-api-node');
//Initialize app
const APP = {
apiKey: "my-API-Key",
authDomain: "My domain auth",
databaseURL: "my databse url",
storageBucket: "my bucket",
messagingSenderId: "my messenger ID"
}
//The Client Credential Flow for Spotify API
const spotifyApi = new SpotifyWebApi({
clientId: 'My cliend ID',
clientSecret: 'My-secret-Key'
});
// Retrieve an access token for Spotify.
spotifyApi.clientCredentialsGrant().then(function (data) {
console.log('All data Spotify API: ' + data.body )
console.log('The access token expires in ' + data.body['expires_in']);
console.log('The access token is ' + data.body['access_token']);
// Save the access token so that it's used in future calls
spotifyApi.setAccessToken(data.body['access_token']);
}, function (err) {
console.log('Something went wrong when retrieving an access token', err);
})
firebase.initializeApp(APP);
admin.initializeApp(APP)
The problem is that the logs indicate that the spotify access token function runs once per Firebase-Cloud Function in my index.js file. I am getting over a dozen tokens, each shows up in the log as called from one of my firebase-cloud functions.
How do I tell Firebase Functions to run this code once and only once when I upload my entire index.ts? And not run it when I upload a single function; that would be useful too.
Thanks.
javascript typescript firebase google-cloud-functions
So, I am using the spotify API to do searches. Locally, I tested my code using tsc and node; everything works as expected.
However, when I deploy my index.ts the logs indicate some odd behavior.
At the top of the file I have the following:
//Load libraries
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const firebase = require("firebase");
const stripe = require('stripe')(functions.config().stripe.token)
const SpotifyWebApi = require('spotify-web-api-node');
//Initialize app
const APP = {
apiKey: "my-API-Key",
authDomain: "My domain auth",
databaseURL: "my databse url",
storageBucket: "my bucket",
messagingSenderId: "my messenger ID"
}
//The Client Credential Flow for Spotify API
const spotifyApi = new SpotifyWebApi({
clientId: 'My cliend ID',
clientSecret: 'My-secret-Key'
});
// Retrieve an access token for Spotify.
spotifyApi.clientCredentialsGrant().then(function (data) {
console.log('All data Spotify API: ' + data.body )
console.log('The access token expires in ' + data.body['expires_in']);
console.log('The access token is ' + data.body['access_token']);
// Save the access token so that it's used in future calls
spotifyApi.setAccessToken(data.body['access_token']);
}, function (err) {
console.log('Something went wrong when retrieving an access token', err);
})
firebase.initializeApp(APP);
admin.initializeApp(APP)
The problem is that the logs indicate that the spotify access token function runs once per Firebase-Cloud Function in my index.js file. I am getting over a dozen tokens, each shows up in the log as called from one of my firebase-cloud functions.
How do I tell Firebase Functions to run this code once and only once when I upload my entire index.ts? And not run it when I upload a single function; that would be useful too.
Thanks.
javascript typescript firebase google-cloud-functions
javascript typescript firebase google-cloud-functions
asked Nov 24 '18 at 18:59
ReverseFlowReverseFlow
232312
232312
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The top-level code for your index.js
is run in each container that Cloud Functions instantiates, to ensure that container is properly initialized for running your functions. Since Cloud Functions may instantiate multiple containers, the code may run multiple times.
There is no way to control the number of containers that Cloud Functions instantiates. If you don't want the code to run in each container, don't make it top-level code in your index.js
.
For example, you could create the Spotify credentials on-demand when you function is invoked. That ensures that the credentials are only on containers where your functions are actually invoked, although it does mean that the first invocation of your function in a container will need to get the Spotify credentials, so takes longer to complete.
This is very enlightening. Is there an easy programatic way to say "run this high level code only on containers that contain this particular function"? I can hack it with conditionals, but if there is a beautiful Firebase solution for this I want to know it; also, if there isn't, is it reasonable to request one(a simple solution that is)?
– ReverseFlow
Nov 25 '18 at 4:03
Nope there isn't and way to configure that. That's why you'll have to initialize it on-demand from each function that needs it.
– Frank van Puffelen
Nov 25 '18 at 5:24
Please consider adding such a thing in the near future. It is a pain to maintain state of tokens for different APIs synchronized across many self-contained environments.
– ReverseFlow
Nov 25 '18 at 5:51
I suggest you file a feature request.
– Frank van Puffelen
Nov 25 '18 at 14:44
Done. (^_^) [^_^]
– ReverseFlow
Nov 25 '18 at 14:54
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53461425%2fhow-to-stop-firebase-functions-from-executing-local-functions-once-per-every-clo%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The top-level code for your index.js
is run in each container that Cloud Functions instantiates, to ensure that container is properly initialized for running your functions. Since Cloud Functions may instantiate multiple containers, the code may run multiple times.
There is no way to control the number of containers that Cloud Functions instantiates. If you don't want the code to run in each container, don't make it top-level code in your index.js
.
For example, you could create the Spotify credentials on-demand when you function is invoked. That ensures that the credentials are only on containers where your functions are actually invoked, although it does mean that the first invocation of your function in a container will need to get the Spotify credentials, so takes longer to complete.
This is very enlightening. Is there an easy programatic way to say "run this high level code only on containers that contain this particular function"? I can hack it with conditionals, but if there is a beautiful Firebase solution for this I want to know it; also, if there isn't, is it reasonable to request one(a simple solution that is)?
– ReverseFlow
Nov 25 '18 at 4:03
Nope there isn't and way to configure that. That's why you'll have to initialize it on-demand from each function that needs it.
– Frank van Puffelen
Nov 25 '18 at 5:24
Please consider adding such a thing in the near future. It is a pain to maintain state of tokens for different APIs synchronized across many self-contained environments.
– ReverseFlow
Nov 25 '18 at 5:51
I suggest you file a feature request.
– Frank van Puffelen
Nov 25 '18 at 14:44
Done. (^_^) [^_^]
– ReverseFlow
Nov 25 '18 at 14:54
add a comment |
The top-level code for your index.js
is run in each container that Cloud Functions instantiates, to ensure that container is properly initialized for running your functions. Since Cloud Functions may instantiate multiple containers, the code may run multiple times.
There is no way to control the number of containers that Cloud Functions instantiates. If you don't want the code to run in each container, don't make it top-level code in your index.js
.
For example, you could create the Spotify credentials on-demand when you function is invoked. That ensures that the credentials are only on containers where your functions are actually invoked, although it does mean that the first invocation of your function in a container will need to get the Spotify credentials, so takes longer to complete.
This is very enlightening. Is there an easy programatic way to say "run this high level code only on containers that contain this particular function"? I can hack it with conditionals, but if there is a beautiful Firebase solution for this I want to know it; also, if there isn't, is it reasonable to request one(a simple solution that is)?
– ReverseFlow
Nov 25 '18 at 4:03
Nope there isn't and way to configure that. That's why you'll have to initialize it on-demand from each function that needs it.
– Frank van Puffelen
Nov 25 '18 at 5:24
Please consider adding such a thing in the near future. It is a pain to maintain state of tokens for different APIs synchronized across many self-contained environments.
– ReverseFlow
Nov 25 '18 at 5:51
I suggest you file a feature request.
– Frank van Puffelen
Nov 25 '18 at 14:44
Done. (^_^) [^_^]
– ReverseFlow
Nov 25 '18 at 14:54
add a comment |
The top-level code for your index.js
is run in each container that Cloud Functions instantiates, to ensure that container is properly initialized for running your functions. Since Cloud Functions may instantiate multiple containers, the code may run multiple times.
There is no way to control the number of containers that Cloud Functions instantiates. If you don't want the code to run in each container, don't make it top-level code in your index.js
.
For example, you could create the Spotify credentials on-demand when you function is invoked. That ensures that the credentials are only on containers where your functions are actually invoked, although it does mean that the first invocation of your function in a container will need to get the Spotify credentials, so takes longer to complete.
The top-level code for your index.js
is run in each container that Cloud Functions instantiates, to ensure that container is properly initialized for running your functions. Since Cloud Functions may instantiate multiple containers, the code may run multiple times.
There is no way to control the number of containers that Cloud Functions instantiates. If you don't want the code to run in each container, don't make it top-level code in your index.js
.
For example, you could create the Spotify credentials on-demand when you function is invoked. That ensures that the credentials are only on containers where your functions are actually invoked, although it does mean that the first invocation of your function in a container will need to get the Spotify credentials, so takes longer to complete.
answered Nov 24 '18 at 23:27
Frank van PuffelenFrank van Puffelen
239k29382409
239k29382409
This is very enlightening. Is there an easy programatic way to say "run this high level code only on containers that contain this particular function"? I can hack it with conditionals, but if there is a beautiful Firebase solution for this I want to know it; also, if there isn't, is it reasonable to request one(a simple solution that is)?
– ReverseFlow
Nov 25 '18 at 4:03
Nope there isn't and way to configure that. That's why you'll have to initialize it on-demand from each function that needs it.
– Frank van Puffelen
Nov 25 '18 at 5:24
Please consider adding such a thing in the near future. It is a pain to maintain state of tokens for different APIs synchronized across many self-contained environments.
– ReverseFlow
Nov 25 '18 at 5:51
I suggest you file a feature request.
– Frank van Puffelen
Nov 25 '18 at 14:44
Done. (^_^) [^_^]
– ReverseFlow
Nov 25 '18 at 14:54
add a comment |
This is very enlightening. Is there an easy programatic way to say "run this high level code only on containers that contain this particular function"? I can hack it with conditionals, but if there is a beautiful Firebase solution for this I want to know it; also, if there isn't, is it reasonable to request one(a simple solution that is)?
– ReverseFlow
Nov 25 '18 at 4:03
Nope there isn't and way to configure that. That's why you'll have to initialize it on-demand from each function that needs it.
– Frank van Puffelen
Nov 25 '18 at 5:24
Please consider adding such a thing in the near future. It is a pain to maintain state of tokens for different APIs synchronized across many self-contained environments.
– ReverseFlow
Nov 25 '18 at 5:51
I suggest you file a feature request.
– Frank van Puffelen
Nov 25 '18 at 14:44
Done. (^_^) [^_^]
– ReverseFlow
Nov 25 '18 at 14:54
This is very enlightening. Is there an easy programatic way to say "run this high level code only on containers that contain this particular function"? I can hack it with conditionals, but if there is a beautiful Firebase solution for this I want to know it; also, if there isn't, is it reasonable to request one(a simple solution that is)?
– ReverseFlow
Nov 25 '18 at 4:03
This is very enlightening. Is there an easy programatic way to say "run this high level code only on containers that contain this particular function"? I can hack it with conditionals, but if there is a beautiful Firebase solution for this I want to know it; also, if there isn't, is it reasonable to request one(a simple solution that is)?
– ReverseFlow
Nov 25 '18 at 4:03
Nope there isn't and way to configure that. That's why you'll have to initialize it on-demand from each function that needs it.
– Frank van Puffelen
Nov 25 '18 at 5:24
Nope there isn't and way to configure that. That's why you'll have to initialize it on-demand from each function that needs it.
– Frank van Puffelen
Nov 25 '18 at 5:24
Please consider adding such a thing in the near future. It is a pain to maintain state of tokens for different APIs synchronized across many self-contained environments.
– ReverseFlow
Nov 25 '18 at 5:51
Please consider adding such a thing in the near future. It is a pain to maintain state of tokens for different APIs synchronized across many self-contained environments.
– ReverseFlow
Nov 25 '18 at 5:51
I suggest you file a feature request.
– Frank van Puffelen
Nov 25 '18 at 14:44
I suggest you file a feature request.
– Frank van Puffelen
Nov 25 '18 at 14:44
Done. (^_^) [^_^]
– ReverseFlow
Nov 25 '18 at 14:54
Done. (^_^) [^_^]
– ReverseFlow
Nov 25 '18 at 14:54
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53461425%2fhow-to-stop-firebase-functions-from-executing-local-functions-once-per-every-clo%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown