flexbiz-server
Version:
Flexible Server
6 lines (5 loc) • 1.88 kB
JavaScript
'use strict';const axios=require("axios"),moment=require("moment"),numeral=require("numeral");require("../global");
const {evalute}=require("../libs/utils"),{parentPort,workerData}=require("worker_threads"),_=require("lodash"),runWebhook=async $msg$$=>{if(!$msg$$.webhook_configs||!$msg$$.webhook_configs.url)return parentPort.postMessage({error:"configs webhook is not valid"});console.info("running webhook",$msg$$.webhook_configs.url,$msg$$.webhook_configs.action);let $data$$=$msg$$.data,$custom_data_send$$=$msg$$.webhook_configs.custom_data_send;if($custom_data_send$$)try{let $str_func$$=$custom_data_send$$;if(0<=
$str_func$$.indexOf("async "))return parentPort.postMessage({error:"Async is not allow to use when customing data"});0!==$str_func$$.indexOf("return ")&&($str_func$$=`return ${$str_func$$}`);$data$$=evalute($str_func$$,{data:$data$$,moment,numeral})}catch($e$$){return console.error($e$$,$custom_data_send$$),parentPort.postMessage({error:$e$$.message||$e$$})}try{axios.post($msg$$.webhook_configs.url,$data$$).then(function($response$$){parentPort.postMessage({result:$response$$.data,id_task:$msg$$.id_task})}).catch(function($error$$){parentPort.postMessage({error:$error$$.data||
$error$$.message||$error$$,id_task:$msg$$.id_task})})}catch($e$$){parentPort.postMessage({error:$e$$,id_task:$msg$$.id_task})}};parentPort.on("message",$msg$$=>{if($msg$$){if(_.isString($msg$$))try{$msg$$=JSON.parse($msg$$)}catch($e$$){console.error($e$$);parentPort.postMessage({error:"msg is not valid"});return}runWebhook($msg$$)}else parentPort.postMessage({error:"msg is not valid"})});
workerData&&(()=>{let $msg$$=workerData;if(_.isString($msg$$))try{$msg$$=JSON.parse($msg$$)}catch($e$$){console.error($e$$);parentPort.postMessage({error:"msg is not valid"});return}$msg$$.webhook_configs&&runWebhook($msg$$)})();setInterval(()=>{parentPort.postMessage("pong")},5E3);