0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Supabaseでスケジュール実行をするためのメモ

Last updated at Posted at 2025-03-13

環境

React
supabase
windows

edgefunctionsに、googleスプレッドシートから情報を取得してgoogleカレンダーに追加するという処理を作成

edgefunctionsにデプロイするには、AI assistantを使うか、ローカルでdockerをつかってdeployするか。
エラーをはきまくった結果、
ai assistantで原型を作成してから、

  1. Download the function
    $
    npx supabase functions download [scheduler]
    Deploy a new version
    $
    npx supabase functions deploy [scheduler]

このへんをつかって編集するのが一番確実。

テストで実行するには
curlコマンドを使う
curl -X POST "https://[myproject].supabase.co/functions/v1/[scheduler]" -H "Authorization: Bearer [your-anon-key]"

ローカルで編集したとき、Denoが名前解決できずにエラーになるが、Supabase側でdeployするので問題なし。
ちょっと気になるが。

supabaseにスケジュールを追加

参考 https://infigate.net/supabase%E3%81%A7edge-functions%E3%82%92%E5%AE%9A%E6%9C%9F%E5%AE%9F%E8%A1%8C%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95/

Supabaseでファンクションを毎朝9時に実行するには、PostgreSQLのcron機能(pg_cron拡張)を使用します。以下のステップで設定できます:

Supabaseダッシュボードで「SQL Editor」を開きます
次のSQLコマンドを実行してpg_cron拡張を有効化します:

sqlCopyCREATE EXTENSION IF NOT EXISTS pg_cron;

スケジュール設定を追加します。毎朝9時に実行するため、以下のSQLを実行します:

sqlCopySELECT cron.schedule(
'daily-calendar-events', -- スケジュール名
'0 9 * * *', -- cronパターン(毎日9時に実行)
$$
SELECT net.http_post(
'https://[your-project-ref].functions.supabase.co/functions/v1/create-calendar-events',
'{}',
'{"Authorization": "Bearer [your-anon-key]"}'
) as request_id;
$$
);
このコマンドで:

daily-calendar-events はスケジュールの名前です(任意の名前に変更可能)
0 9 * * * はcron表記で、毎日9時にジョブを実行します
URLの [your-project-ref] は、あなたのSupabaseプロジェクトの参照IDに置き換えてください
[your-anon-key] は、Supabaseプロジェクトの匿名キーに置き換えてください

スケジュールが正しく設定されたか確認するには、以下のSQLを実行します:

sqlCopySELECT * FROM cron.job;
この設定により、毎日朝9時にEdge Functionが自動的に呼び出され、カレンダーにイベントが追加されるようになります。

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?