micronaut-projects / micronaut-gcp

Integration between Micronaut and Google Cloud Platform (GCP)
Apache License 2.0
50 stars 35 forks source link

Can't get Pub/Sub subscription to initialize #459

Open sgammon opened 3 years ago

sgammon commented 3 years ago

Hey there Micronaut authors,

I tried to setup a Pub/Sub subscriber in Micronaut, but I'm having trouble getting it to start up and receive a signal from the subscription. Basically, here is my code (in Kotlin):

package com.example

import io.micronaut.gcp.pubsub.annotation.PubSubListener
import io.micronaut.gcp.pubsub.annotation.Subscription
import io.micronaut.gcp.pubsub.exception.PubSubMessageReceiverException
import io.micronaut.gcp.pubsub.exception.PubSubMessageReceiverExceptionHandler
import io.micronaut.messaging.Acknowledgement
import org.slf4j.Logger
import org.slf4j.LoggerFactory

@PubSubListener
open class CatalogPipeline {
    /** Private log pipe. */
    val logging: Logger = LoggerFactory.getLogger(CatalogPipeline::class.java)

    init {
        logging.info("Initializing catalog pipeline...")
    }

    @Subscription(value = "v1.catalog.commit")
    fun onMessage(data: ByteArray, acknowledgement: Acknowledgement) {
        logging.info("!! Message received via Pub/Sub trigger channel. !!")
    }
}

The expectation (unless I am mistaken) is that, if CatalogPipeline is present in the classpath and designated via the .packages(*) call in my Application, Micronaut will boot up the Pub/Sub subscription and begin calling it for signals. Is that correct, or is there more setup I need to do?

I also have this in my application.yml:

micronaut:
  application:
    name: demo

gcp:
  project-id: project-id-here

The subscription is named correctly in GCP, and the project ID matches. My local credentials are authorized to publish and subscribe to the topic/subscription.

Also, how would I go about diagnosing these issues with logging? Is there a class that I can enable logging for to see why it would, or would not, create the subscription?

sgammon commented 3 years ago

I've added a repro from a vanilla demo project here: https://github.com/sgammon/micronaut-pubsub-repro

sgammon commented 3 years ago

@graemerocher / @viniciusccarvalho any ideas of what might be going wrong here?

graemerocher commented 3 years ago

Try debug https://github.com/micronaut-projects/micronaut-gcp/blob/0fe125a3c0feab78cbd5881f5c1eb1fb9fcb334e/gcp-pubsub/src/main/java/io/micronaut/gcp/pubsub/intercept/PubSubConsumerAdvice.java#L148 since that is where the subscriber is registered

viniciusccarvalho commented 3 years ago

Like @graemerocher said you can put a breakpoint there and see if it gets created. Another common issue with GCP is if you are authenticating using default credentials, Google keeps changing the IAM policies, and a bunch of services will just not work if you are not using a json credentials file and use the GOOGLE_APPLICATION_CREDENTIALS to point to the file.

sgammon commented 3 years ago

@graemerocher the breakpoint doesn't get hit

@viniciusccarvalho i don't think this is related to credentials, if only because i get no errors in TRACE mode.

i do however get this error:

Bean type interface io.micronaut.context.processor.ExecutableMethodProcessor is not compatible with candidate generic types [interface io.micronaut.gcp.pubsub.annotation.PubSubListener] of candidate Definition: io.micronaut.gcp.pubsub.intercept.PubSubConsumerAdvice

how could that possibly be the case, though?

graemerocher commented 3 years ago

Which version of Micronaut?

graemerocher commented 3 years ago

And which version of Micronaut GCP?

viniciusccarvalho commented 3 years ago

@graemerocher looks like it's micronaut 3.0.0, has anything changed on bean post processing? I have been AFK for a while, did not keep up, need to test the GCP support on 3.x version

graemerocher commented 3 years ago

@viniciusccarvalho yes that is why I asked. For Micronaut 3.0 is currently at milestone stage, you should explicitly declare a dependency on the 4.0.0 RC1 release to get a compatible version of the GCP module https://github.com/micronaut-projects/micronaut-gcp/releases/tag/v4.0.0-RC1

graemerocher commented 3 years ago

See https://github.com/micronaut-projects/micronaut-gcp/pull/448

viniciusccarvalho commented 3 years ago

Somehow the javax.inject package is missing from the depedencies: implementation group: 'javax.inject', name: 'javax.inject', version: '1'

I noticed that after adding it, all gcp beans were being loaded (for instance the GoogleCredentialsFactory). You could also check that @Singleton annotation was erroring on the IDE as the dependency is missin.

@sgammon I would follow @graemerocher advice and just change the version of your micronaut base project.

Thanks @graemerocher

sgammon commented 3 years ago

@graemerocher / @viniciusccarvalho the sample is in 3.x.x, and yes i was previously using that. downgrading to 2.5.4 gets me better TRACE logs, but I still don't get a subscription. let me get back to you guys with more information or as soon as i determine this to be an actual bug (and not just cavalier upgrades to unreleased code!).

thank you for your help both of you 😄