var http = require("http");
var url = require("url");
const child_process = require('child_process');

var flagOfRepair = 1;
var startRepairorder=function(){
	var child_pid = child_process.spawn('node',['./terry/repairorder.js',1]);
	flagOfRepair = 0;
	child_pid.stdout.on('data', function (data) {
		if (data!=null) {
			 console.log('stdout: ' + data);
		}
	});

	child_pid.stderr.on('data', function (data) {
	   console.log('stderr: ' + data);
	});

	child_pid.on('close', function (code) {
	   console.log('子进程已退出，退出码 '+code);
	   flagOfRepair = 1;
	});
 }

 //守护进程
 var watchProcess = function(){
	 setInterval(function(){
		 if (flagOfRepair==1) {
			 startRepairorder();
		 }
	 }, 5000);
 }

function start(app) {
	var httpServer = http.createServer(app);

	process.on('uncaughtException', function(e) {
		　　console.log(e);
		});

	var server = app.listen(8081, function () {
		var host = server.address().address
		var port = server.address().port
		watchProcess()
		console.log("应用实例，访问地址为 https://%s:%s", host, port)
	})
}

exports.start = start;