Counts the Clouds

Firebase Functionsを定期的に実行する
2021.07.08

Firebase
Firebase Functions

simon-mn0Kezx98EI-unsplash

なにやら簡単にできそうなので、試してみる。

指定した時刻に実行されるように関数をスケジュール設定する場合は、functions.pubsub.schedule().onRun() を使用します。この便利な方法により、Pub/Sub トピックが作成され、Cloud Scheduler を使用してこのトピックに関するイベントがトリガーされるため、希望するスケジュールどおりに確実に関数が実行されます。

Firebase Functionsのセットアップ

% firebase init functions

=== Project Setup

? Please select an option: Use an existing project
? Select a default Firebase project for this directory: scheduled-functions-trial (scheduled-functions-trial)
i  Using project scheduled-functions-trial (scheduled-functions-trial)

=== Functions Setup

? What language would you like to use to write Cloud Functions? JavaScript
? Do you want to use ESLint to catch probable bugs and enforce style? No
  Wrote functions/package.json
  Wrote functions/index.js
  Wrote functions/.gitignore
? Do you want to install dependencies with npm now? Yes

i  Writing configuration info to firebase.json...
i  Writing project information to .firebaserc...
i  Writing gitignore file to .gitignore...

  Firebase initialization complete!

サンプルコードを追加してデプロイ

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

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

デプロイしてみる。足りないAPIはキックしてくれる。やさしい。

% firebase deploy --only functions

=== Deploying to 'scheduled-functions-trial'...

i  deploying functions
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
  functions: required API cloudfunctions.googleapis.com is enabled
  functions: missing required API cloudbuild.googleapis.com. Enabling now...
  functions: required API cloudbuild.googleapis.com is enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (23.76 KB) for uploading
i  scheduler: ensuring required API cloudscheduler.googleapis.com is enabled...
i  pubsub: ensuring required API pubsub.googleapis.com is enabled...
  scheduler: missing required API cloudscheduler.googleapis.com. Enabling now...
  pubsub: required API pubsub.googleapis.com is enabled
  scheduler: required API cloudscheduler.googleapis.com is enabled
  functions: functions folder uploaded successfully
i  functions: creating Node.js 12 function scheduledFunction(us-central1)...
  functions[scheduledFunction(us-central1)]: Successful create operation.

Functions deploy had errors with the following functions:
	scheduledFunction(us-central1)

To try redeploying those functions, run:
    firebase deploy --only "functions:scheduledFunction"

To continue deploying other features (such as database), run:
    firebase deploy --except functions

Error: Functions did not deploy properly.

Error: Functions did not deploy properly.

エラーが出ているがダッシュボードにはfunctionsが追加されている。実行はされていないみたい。

GCPコンソールの“API とサービス”を見てみる。Pub/Sub APIとScheduler API、Cloud Build APIは動いている。

デバッグオプションを使ってみる。

% firebase deploy --only functions --debug

こんなんでました。

[2021-07-08T08:11:22.068Z] 	Error during upsert schedule for projects/scheduled-functions-trial/locations/us-central1/functions/scheduledFunction: Cloud resource location is not set for this project but scheduled functions require it. Please see this documentation for more details: https://firebase.google.com/docs/projects/locations.

[2021-07-08T08:11:22.068Z] Error during upsert schedule for projects/scheduled-functions-trial/locations/us-central1/functions/scheduledFunction: Cloud resource location is not set for this project but scheduled functions require it. Please see this documentation for more details: https://firebase.google.com/docs/projects/locations.

Firebaseプロジェクトのロケーションが選択されていなかった。

“プロジェクトを設定”→“全般”→“デフォルトの GCP リソース ロケーション”と進み、とりあえずnam5 (us-central)を設定して再デプロイ。

できた。

  Deploy complete!

関数が5分毎に実行されているのをFunctionsのログで確認できた。