LoginSignup
1
0

More than 5 years have passed since last update.

Alibaba Function Compute による kintone 定期実行

Last updated at Posted at 2018-10-30

kintone の定期実行をAlibaba Function Compute (node.js)で試してみました。
今回は 1時間ごとに kintone アプリのレコード件数を取得する処理です。
基本的なしくみは、Azure Functions など他のサーバーレスと同様で簡単に定期実行が実現できます。

開発環境

kintone 連携の Alibaba Function 開発メモと同じです。

kintone API トークン

kintone アプリ設定画面で、REST API で使用するAPI トークンを発行します。
試用するAPI によって、アクセス権を設定できます。

2018-10-30_11h50_32.png

Function Compute の構成

今回は定期実行をするため、タイムトリガーを定義します

  • リージョン :東京、上海などが選択可能
    • サービス:Function Compute のリソースを管理するための単位
      • 関数:システムのスケジューリングと操作の単位
        • トリガー:タイムトリガーはここで定義

作成した関数の概要

kintone のレコード数が少なければ、メモリ 128 MB, タイムアウト60秒で十分です。

2018-10-30_11h22_57.png

タイムトリガー

設定時は、間隔60分を指定しましたが、保存後に見てみると Cron 式に変換されていました。
2018-10-30_11h26_27.png

環境変数

環境変数を設定して、関数内で使用できます。
kintone URL, APPID, APIトークンなどを環境変数に設定して使います。

node 参照例
process.env['KINTONE_URL']

コード

request, request-promise を使用して、kintone REST API を呼び出しています。

f_check_records.js
/*
 * Alibaba Function : kintone get records 
 * 2018.10.30 by rex0220
 */
'use strict';
const rp = require('request-promise')
module.exports.handler = function(event, context, callback) {

    // get kintone records
    var getRecords = function(query) {
        var req = {
            "method": "GET",
            "url": process.env['KINTONE_URL'] + '/k/v1/records.json',
            "headers": {
                "X-Cybozu-API-Token": process.env['KINTONE_API_KEY']
            },
            "qs": {
                app: process.env['KINTONE_APPID'],
                query: query,
                totalCount: true
            },
            "json": true,
        };
        // console.log('get record req: ', JSON.stringify(req));
        return req;
    };

    // main
    rp(getRecords('対応日時 >= "2018-06-01" limit 1')).then(function(resp) {
        console.log('get record done: ', JSON.stringify(resp));
        callback(null, 'records count: ' + resp.totalCount);
    }).catch(function(error) {
        console.log('error: ' + error.message);
        callback('Error get kintone records, ' + err.message, null);
    });
};

実行結果

1時間ごとの定期実行であれば、無料枠内で済みそうです。

課金された時間 300 ms
最大使用メモリ 128 MB
メモリ 30.71 MB

2018-10-30_12h05_24.png

参考URL

kintone REST APIの共通仕様
Function Compute の概要

あとがき

kintone で定期実行するしくみを簡単に JavaScript で構築できるのはうれしいですね。

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