5

I am reading this https://cloud.google.com/scheduler/docs/tut-pub-sub They use the setup like below:

Cloud Scheduler -> PubSub -> Cloud Function-> external Service

and If I have a cron job for calling a service once a day, should I still need this pubsub in between?

I know there is an option for HTTP target type in Cloud Scheduler and I think the below setup without PubSub is good enough.

Cloud Scheduler -> Cloud Function-> external Service

Could you give some advice why I should/should not have the PubSub?

ikhvjs
  • 5,316
  • 2
  • 13
  • 36
  • I personally don't see any advantages, and I never use that method. Cloud Scheduler directly call the HTTP endpoint of my services (Cloud Run, Cloud Functions Workflows,....) – guillaume blaquiere Aug 31 '22 at 14:32

2 Answers2

1

The example that you are looking at is Using Pub/Sub to trigger a Cloud Function so it'll include examples with Pub/Sub there. Instead you can deploy a HTTP Cloud function and use it's URL as the target URL as in below screenshot:

enter image description here

Here, Cloud Scheduler will trigger the function without Pub/Sub.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • Thanks. I know there is a option for HTTP target type. However, I would like to know which option should I use and why. – ikhvjs Aug 31 '22 at 13:06
  • 4
    @ikhvjs Pub/Sub might be more useful if you want to listen for an event at multiple places. Using Cloud Scheduler sounds better here as you just want to trigger a function periodically. If you have a function that listens to Pub/Sub, you can publish events programmatically as well. Cloud scheduler is used there just to publish an event periodically instead of directly calling an API – Dharmaraj Aug 31 '22 at 13:14
0

The option between Pub/Sub or HTTP triggers depends on how you want to write the source code for your function.

If you use an HTTP trigger, you need to handle the HTTP request in your code - this involves adding the relevant libraries or modules for your selected programming language and sending the correct HTTP response to the request (HTTP status 200 for success, etc.).

If you use Pub/Sub, you don't need to handle HTTP requests. You can handle event data (that is by default passed as an argument to the function), but that is optional.

For a Cloud Scheduler job, Pub/Sub is the best option if you want the code to be simpler and have less dependencies. If you prefer to simplify the infrastructure instead, you can remove the Pub/Sub service, but add the logic to handle HTTP requests to your function.

So this is the trade-off: more complexity in code, with simpler infrastructure; or more complexity in infrastructure, with simpler code.

For reference on how to write HTTP functions, see:

https://cloud.google.com/functions/docs/writing/write-http-functions

For reference on how to write event-driven (Pub/Sub) functions, see:

https://cloud.google.com/functions/docs/writing/write-event-driven-functions

andreswebs
  • 123
  • 10