4 * Copyright (C) 2016-2017 Jerry Padgett <sjpadgett@gmail.com>
6 * LICENSE: This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @author Jerry Padgett <sjpadgett@gmail.com>
21 * @link http://www.open-emr.org
23 require_once(dirname(__FILE__
) . "/../library/log.inc");
27 if (!socket_status('localhost', '6661', 'status')) {
28 server_logit(1, "Execute C-CDA Service Start", 0, "Task");
31 if (socket_status('localhost', '6661', 'status')) {
32 server_logit(1, "Service Status : Started.");
34 server_logit(1, "Service Status : Failed Start.");
39 server_logit(1, "Service Status : Alive.", 0, "Sanity Check");
43 function service_shutdown($soft = 1)
45 if (socket_status('localhost', '6661', 'status')) {
46 // shut down service- this can take a few seconds on windows so throw up notice to user.
48 echo '<h3 style="position: absolute; top: 25%; left: 42%">'. xlt("Shutting Down Service ...") . '</h3><img style="position: absolute; top: 40%; left: 45%; width: 125px; height: 125px" src="../../portal/sign/assets/loading.gif" />';
51 server_logit(1, "C-CDA Service shutdown request", 0, "Task");
53 chdir(dirname(__FILE__
));
54 $cmd = 'pkill -f "nodejs serveccda.njs"';
55 exec($cmd . " > /dev/null &");
57 chdir(dirname(__FILE__
));
58 $cmd = 'node unservice';
59 pclose(popen($cmd, "r"));
63 if (!socket_status('localhost', '6661', 'status')) {
64 server_logit(1, "Service Status : " . $soft ?
"Process Terminated" : "Terminated and Disabled.");
66 return true; // Just terminate process/service and allow background to restart. Restart if you will.
69 $service_name = 'ccdaservice';
70 // with ccdaservice and background service and running = 1 bs will not attempt restart of service while still available/active.
71 // not sure if needed but here it is anyway. Otherwise, service is disabled.
72 $sql = 'UPDATE background_services SET running = ?, active = ? WHERE name = ?';
73 $res = sqlStatementNoLog($sql, array($soft, $soft, $service_name));
76 server_logit(1, "Service Status : Failed Shutdown.");
80 server_logit(1, "Service Status : Not active.", 0, "Shutdown Request");
84 function execInBackground($cmd)
87 chdir(dirname(__FILE__
));
88 $cmd = 'node winservice';
89 pclose(popen($cmd, "r"));
91 chdir(dirname(__FILE__
));
92 $cmd = 'nodejs serveccda.njs';
93 exec($cmd . " > /dev/null &");
96 function socket_status($ip, $port, $data)
99 $socket = socket_create(AF_INET
, SOCK_STREAM
, SOL_TCP
);
100 if ($socket === false) {
101 server_logit(1, "Creation of Socket Failed. Start/Restart Service");
105 $result = socket_connect($socket, $ip, $port);
106 if ($result === false) {
107 socket_close($socket);
108 server_logit(1, "Service Not Running");
112 $data = $data . "\r\n";
113 $out = socket_write($socket, $data, strlen($data));
116 $line = socket_read($socket, 1024, PHP_NORMAL_READ
);
118 } while ($line != "\r");
119 $output = substr(trim($output), 0, strlen($output) - 3);
120 socket_close($socket);
123 function service_command($ip, $port, $doaction)
126 $socket = socket_create(AF_INET
, SOCK_STREAM
, SOL_TCP
);
127 if ($socket === false) {
128 server_logit(1, "Service not resident.");
132 $result = socket_connect($socket, $ip, $port);
133 if ($result === false) {
134 socket_close($socket);
135 server_logit(1, "Service Not Running.");
139 $doaction = $doaction . "\r\n";
140 $out = socket_write($socket, $doaction, strlen($doaction));
143 $line = socket_read($socket, 1024, PHP_NORMAL_READ
);
145 } while ($line != "\r");
146 $output = substr(trim($output), 0, strlen($output) - 3);
147 socket_close($socket);
150 function server_logit($success, $text, $pid = 0, $event = "ccdaservice-manager")
152 $pid = isset($_SESSION['pid'])?
$_SESSION['pid']:$pid;
153 $event = isset($_SESSION['ptName']) ?
('Ccda Access: ' . $_SESSION['ptName']) : "Ccda Service Access";
154 $where = isset($_SESSION['ptName']) ?
"Portal Patient" : 'OpenEMR: ' . $_SESSION['authUser'];
156 newEvent($event, "Service_Manager", $where, $success, $text, $pid, 'server', 's2', 's3');