-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.php
101 lines (81 loc) · 3.46 KB
/
main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
//Main file, here it all starts
$kill = function($pid){ return stripos(php_uname('s'), 'win')>-1
? exec("taskkill /F /PID $pid") : exec("kill -9 $pid");
};
require_once('init.php');
$filesCls = new Models\BotComponent\FilesWork();
$filesCls->register_pid_to_file("I am main.php instance");
echo $colors->info("started main.php".PHP_EOL, "purple", "yellow");
//echo $colors->getColoredString(, "red", "black");//kills color
//echo $colors->getColoredString("Testing Colors class, this is purple string on yellow background.", "purple", "yellow"); regular info
function remove_pid_from_file($pid,$file){
global $settings;
$pids = json_decode(file_get_contents($file),true);
unset($pids[$pid]);
file_put_contents($file,json_encode($pids));
}
register_shutdown_function('remove_pid_from_file',getmypid(),$bot_settings->all_pids_file);
$flag = true;
//fire up bot_inst.php
//create a loop for contsant run.
/*start loop{
sleep(60) // cehck every minute(or less..?) for certain things.. like:
*is waiting for sell signal already sold (api calls to binance to get latest closed orders, see if uuid is therethere)? if so, change status in db
*check if bot instance is still running (grab pid from all_pids.json file){
if not start a new instance.
}
*check if something major changed (config file.. coins etc.. and update the bot(terminate and reboot Executables".DIRECTORY_SEPARATOR."BotInstance.php))
//check if there is terminate command waiting in terminate.me file, if so, kill bot pid, and all other pids and exit.
// if run simultaion command, start bot instance with simulation settings
}
*/
//get this server ip
$externalContent = file_get_contents('http://checkip.dyndns.com/');
preg_match('/Current IP Address: \[?([:.0-9a-fA-F]+)\]?/', $externalContent, $m);
$_SERVER['SERVER_ADDR'] = $m[1];
$finished_signals=0;
$pids_cls = new Models\PIDs($bot_settings->all_pids_file);
$filename = $bot_settings->all_pids_file;
$main = new Models\Console\Main();
//start ws
$main->bgExec("php Executables".DIRECTORY_SEPARATOR."FrontEndWebSocketServer.php");//
echo 'before web server start';
//start web server
$main->bgExec("php -S ".$_SERVER['SERVER_ADDR'].":8080 -t interface/ Executables".DIRECTORY_SEPARATOR."FrontEndWebServer.php > /dev/null 2>&1");
//start bot instances
echo 'before instances start';
$main->startInstances();
echo 'after instances start, before loop';
$timer = 0;
while(true){
//echo "loop start, loops: $timer";
//check for termination file
$main->checkForProgramEnd();
//echo "checked program end";
//get messages from bots pids and print;
$main->printToConsoleFromBots();
//echo "checked print to console.";
//execute commands from font-end
$main->execCommandsFromFE();
//echo "chekced print to console";
//create_instances on the fly
$main->startOnTheFlyInst();
//echo "check on the fly instnaces";
// add btc balance to db each 30 minutes
$main->addBtcBalanceToDBPeri();
//echo "some db in main";
if($timer > 30 && !$main->checkSocketStatus()){//reload websocket if connection dropped
foreach($pids_cls->pids as $pid=>$name){
if(strpos(strtolower($name),"websocketserver")!==false){
$kill($pid);
$main->remove_pid_from_file($pid, $filename);
}
}
sleep(1);
$main->bgExec("php Executables".DIRECTORY_SEPARATOR."FrontEndWebSocketServer.php");
$timer=0;
}
//$timer++;
sleep(1);
}