LoginSignup
0
1

More than 1 year has passed since last update.

Cloud Functions (Go言語) & PubSub & Cloud Scheduler を利用してcronを実現する

Posted at

方法を3ステップで解説します

STEP1 トピックを作成する

GCP の PubSub のコンソールからトピックを作成する

名前は、「hello-functions」にしました

image.png

STEP2 メッセージを受信して起動するアプリケーションを Cloud Functions にデプロイする

コード

GCP の Cloud Functions のコンソールから「関数の作成」を選択して出てくるテンプレートと同じ内容です

hello_functions.go
package p

import (
	"context"
	"log"
)

type PubSubMessage struct {
	Data []byte `json:"data"`
}

func HelloPubSub(ctx context.Context, m PubSubMessage) error {
	log.Println(string(m.Data))
	return nil
}

gcloud (CLI) でデプロイ

ここで--entry-pointを指定しないといけなそうですが、指定しなくても動きました(エクスポート(関数名の先頭が大文字)している関数が1つだからかな?)

gcloud functions deploy HelloPubSub --runtime go116 --trigger-topic=hello-functions --project <firebaseのプロジェクト名>

STEP3 Cloud Scheduler で定期的にメッセージを送信する

以下のページを参考に、Cloud Scheduler を設定する

Cloud Functionsを定期実行させてみる | DevelopersIO

参考

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1