Google Scheduled functions: There was an error deploying functions?

Viewed 1911

I have a fresh project but was looking to test scheduled functions. Am I missing anything?

$ firebase deploy

=== Deploying to 'testing-db'...

i  deploying functions
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
!  functions: missing required API cloudbuild.googleapis.com. Enabling now...
+  functions: required API cloudfunctions.googleapis.com is enabled
+  functions: required API cloudbuild.googleapis.com is enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (24.45 KB) for uploading
i  functions: ensuring required API pubsub.googleapis.com is enabled...
i  functions: ensuring required API cloudscheduler.googleapis.com is enabled...
!  functions: missing required API cloudscheduler.googleapis.com. Enabling now...
+  functions: required API pubsub.googleapis.com is enabled
+  functions: required API cloudscheduler.googleapis.com is enabled
+  functions: functions folder uploaded successfully
i  functions: creating Node.js 14 function scheduledFunction(us-central1)...

Functions deploy had errors with the following functions:
        scheduledFunction(us-central1)
i  functions: cleaning up build files...

Error: There was an error deploying functions

Index.js

const functions = require('firebase-functions');

exports.scheduledFunction = functions.pubsub
  .schedule('every 1 minutes')
  .onRun((context) => {
    
    return console.log('This will be run every 1 minutes!');
  });

Firebase log shows:

Error: Failed to upsert schedule function scheduledFunction in region europe-west1
2 Answers

I ran into this issue as well, but the solution was not region related like @Priyashree's answer. Rather, when I ran firebase deploy --only functions --debug the logs revealed that my scheduler had an error.

The bottom of the log had this error: Error: Failed to upsert schedule function foo in region us-central1

But scrolling up a bit revealed: <<< HTTP RESPONSE BODY {"error":{"code":400,"message":"Schedule or time zone is invalid.","status":"INVALID_ARGUMENT"}}

I had a typo in my function schedule:

functions.pubsub.schedule("every 1 minute").onRun((context) => {}

every 1 minute should have been every 1 minutes. Note the missing 's'.

In general, I think it's useful to enable '--debug' on the firebase deploy command so you can see detailed log output of the exact error.

When you are using scheduled functions in Firebase Functions, an App Engine instance is created that is needed for Cloud Scheduler to work. You can read about it here.They use the location that has been set by default for resources. I think that you are getting that error because there is a difference between the default GCP resource location you specified and the region of your scheduled cloud function. If you click on the cogwheel next to project-overview in Firebase you can see where your resources are located.

Check your Cloud Scheduler function details and see which region it has been deployed to. By default, functions run in the us-central1 region. Check this link to see how we can change the region of the function.

Related