Last active
October 3, 2018 00:21
-
-
Save noriyukitakei/a928b77c34440bbd570d24f6b21b59e9 to your computer and use it in GitHub Desktop.
クラウドデザインパターンを実践してみたシリーズ 〜 Scheduler Agent Supervisorパターン〜【rollbackTask】
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MySQLのモジュールを読み込む | |
const mysql = require('mysql2'); | |
// ファイルを読み込むためのモジュールを読み込む | |
const fs = require('fs'); | |
module.exports = function (context, myTimer) { | |
var timeStamp = new Date().toISOString(); | |
if(myTimer.isPastDue) | |
{ | |
context.log('JavaScript is running late!'); | |
} | |
context.log('JavaScript timer trigger function ran!:', timeStamp); | |
// MySQLに接続するための情報を環境変数から取得する。 | |
var conn = mysql.createConnection( | |
{host: process.env['MYSQL_HOST'], | |
user: process.env['MYSQL_USER'], | |
password: process.env['MYSQL_SECRET'], | |
database: process.env['MYSQL_DB'], | |
port: 3306, | |
ssl:{ca:fs.readFileSync(process.env['MYSQL_CA'])} | |
} | |
); | |
// テーブルt_state_storeのフィールドのfailure_count規定回数(ここでは3回)以上 | |
// 失敗しているタスクを抽出する。 | |
conn.query( | |
'SELECT * FROM `t_state_store` where failure_count <= 3', | |
function (error, results, fields) { | |
var messages = []; | |
try { | |
for (var i = 0;i < results.length;i++) { | |
// 長時間回復不能な、持続的なエラーが発生した場合の処理をここに書きます。 | |
// ロールバック、管理者に通知など業務要件によって様々です。 | |
context.log('rollback'); | |
} | |
} catch (e) { | |
context.log(e); | |
throw e; | |
} finally { | |
context.log("close"); | |
conn.end(); | |
} | |
context.done(); | |
} | |
) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment