-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_service.php
122 lines (101 loc) · 3.31 KB
/
test_service.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
// A simple test service that does nothing.
// (C) 2016 CubicleSoft. All Rights Reserved.
if (!isset($_SERVER["argc"]) || !$_SERVER["argc"])
{
echo "This file is intended to be run from the command-line.";
exit();
}
// Temporary root.
$rootpath = str_replace("\\", "/", dirname(__FILE__));
function DisplayError($msg, $result = false, $exit = true)
{
ob_start();
echo ($exit ? "[Error] " : "") . $msg . "\n";
if ($result !== false)
{
if (isset($result["error"])) echo "[Error] " . $result["error"] . " (" . $result["errorcode"] . ")\n";
if (isset($result["info"])) var_dump($result["info"]);
}
fwrite(STDERR, ob_get_contents());
ob_end_clean();
if ($exit) exit();
}
if ($argc > 1)
{
// Helper class. Technically, none of these options nor the SDK are necessary - they just make life a little easier.
require_once $rootpath . "/sdks/servicemanager.php";
$sm = new ServiceManager($rootpath);
// $sm = new ServiceManager($rootpath . "/servicemanager/Debug");
echo "Service manager: " . $sm->GetServiceManagerRealpath() . "\n\n";
$servicename = "servicemanager-php-test";
if ($argv[1] == "install")
{
// Install the service.
$args = array();
$options = array();
$result = $sm->Install($servicename, __FILE__, $args, $options, true);
if (!$result["success"]) DisplayError("Unable to install the '" . $servicename . "' service.", $result);
}
else if ($argv[1] == "start")
{
// Start the service.
$result = $sm->Start($servicename, true);
if (!$result["success"]) DisplayError("Unable to start the '" . $servicename . "' service.", $result);
}
else if ($argv[1] == "stop")
{
// Stop the service.
$result = $sm->Stop($servicename, true);
if (!$result["success"]) DisplayError("Unable to stop the '" . $servicename . "' service.", $result);
}
else if ($argv[1] == "uninstall")
{
// Uninstall the service.
$result = $sm->Uninstall($servicename, true);
if (!$result["success"]) DisplayError("Unable to uninstall the '" . $servicename . "' service.", $result);
}
else if ($argv[1] == "dumpconfig")
{
$result = $sm->GetConfig($servicename);
if (!$result["success"]) DisplayError("Unable to retrieve the configuration for the '" . $servicename . "' service.", $result);
echo "Service configuration: " . $result["filename"] . "\n\n";
echo "Current service configuration:\n\n";
foreach ($result["options"] as $key => $val) echo " " . $key . " = " . $val . "\n";
}
else
{
echo "Command not recognized. Run the service manager directly for anything other than 'install', 'uninstall', and 'dumpconfig'.\n";
}
}
else
{
// Main service code.
$stopfilename = __FILE__ . ".notify.stop";
$reloadfilename = __FILE__ . ".notify.reload";
$lastservicecheck = time();
$running = true;
do
{
// Do regular work here.
sleep(1);
// Check the status of the two service file options.
if ($lastservicecheck <= time() - 3)
{
if (file_exists($stopfilename))
{
// Initialize termination.
echo "Stop requested.\n";
$running = false;
}
else if (file_exists($reloadfilename))
{
// Reload configuration and then remove reload file.
echo "Reload config requested.\n";
@unlink($reloadfilename);
}
$lastservicecheck = time();
}
} while ($running);
}
?>