Last active
October 2, 2018 23:26
-
-
Save noriyukitakei/881d77dd95ccf81d8f4a064f4e3e46b3 to your computer and use it in GitHub Desktop.
クラウドデザインパターンを実践してみたシリーズ 〜 Scheduler Agent Supervisorパターン〜【pushRequestMessage】
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'])} | |
} | |
); | |
// 未処理で、かつ、どのAgentも取得していないタスクの情報を取得する。 | |
conn.query( | |
'SELECT * FROM `t_state_store` where process_state = "00"' | |
+ ' and locked_by is NULL', | |
function (error, results, fields) { | |
var messages = []; | |
try { | |
for (var i = 0;i < results.length;i++) { | |
// テーブルt_state_storeのフィールドprocess_stateを01(処理中)、フィールドlocked_byを01、complete_byを現在時刻の10分後にします。 | |
conn.query( | |
'UPDATE `t_state_store` SET locked_by = "01",process_state = "01",complete_by = DATE_ADD(NOW(),INTERVAL 1 MINUTE)' | |
+ ' where task_id = ' + results[i].task_id, | |
function (error, results, fields) { | |
if(error) throw error; | |
} | |
) | |
context.log('query start'); | |
// Agentに渡すためのJSONを生成します。 | |
var message = { | |
"taskId": results[i].task_id, | |
"userId": results[i].user_id, | |
"taskBody": results[i].task_body | |
} | |
context.log(results[i].task_body); | |
messages.push(message); | |
} | |
} catch (e) { | |
context.log(e); | |
throw e; | |
} finally { | |
context.log("close"); | |
conn.end(); | |
} | |
// 先程作成したJSONをキューにpushします。 | |
context.bindings.outputQueueItem = messages; | |
context.done(); | |
} | |
) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment