firebase-functions icon indicating copy to clipboard operation
firebase-functions copied to clipboard

V1 schedule.onRun doesn't call onInit

Open tecbot opened this issue 8 months ago • 6 comments

firebase-functions: 6.3.2

If creating a schedule.onRun function, onInit is not called because of this lines:

Will set label deployment-scheduled to true https://github.com/firebase/firebase-functions/blob/cce55b562f325474a2ea13d7db06392309667990/src/v1/providers/pubsub.ts#L155

makeCloudFunction is wrapping withInit on the first line https://github.com/firebase/firebase-functions/blob/cce55b562f325474a2ea13d7db06392309667990/src/v1/cloud-functions.ts#L370

However, because of the deployment-scheduled label it uses the non wrapped handler: https://github.com/firebase/firebase-functions/blob/cce55b562f325474a2ea13d7db06392309667990/src/v1/cloud-functions.ts#L411

Expected behavior

onInit callback is called

Actual behavior

onInit is not called and causes issues with the function execution

tecbot avatar Mar 19 '25 11:03 tecbot

I found a few problems with this issue:

  • I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
  • This issue does not seem to follow the issue template. Make sure you provide all the required information.

google-oss-bot avatar Mar 19 '25 11:03 google-oss-bot

Hi @tecbot,

Thanks for reporting this issue! We’ve received it and are reviewing it. We’ll provide updates as soon as possible.

CorieW avatar Mar 20 '25 10:03 CorieW

If you have time, could you produce some code that demonstrates this issue?

CorieW avatar Mar 20 '25 10:03 CorieW

I already referenced the misleading code lines. Creating a v1 pubsub trigger will cause the issue always:

import { onInit } from 'firebase-functions'
import * as functions from 'firebase-functions/v1'
import { info } from 'firebase-functions/logger'

onInit(() => {
     info("onInit called")
})

export const myFunction = functions
    .pubsub.schedule('*/10 * * * *')
    .onRun(() => {
        info("function executed")
    })

Expected log output:

  • onInit called
  • function executed

Actual:

  • function executed

tecbot avatar Mar 20 '25 10:03 tecbot

Appreciate it! It's just so whoever picks this up can easily reproduce and also use it to verify their fix works.

CorieW avatar Mar 20 '25 11:03 CorieW

Reproduced

CorieW avatar Mar 21 '25 16:03 CorieW