aboutsummaryrefslogtreecommitdiff
path: root/src/main.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.php')
-rw-r--r--src/main.php93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/main.php b/src/main.php
new file mode 100644
index 0000000..60779e5
--- /dev/null
+++ b/src/main.php
@@ -0,0 +1,93 @@
+<?php
+
+require __DIR__.'/init.php';
+
+if ($argc < 2) {
+ echo <<<EOF
+Usage: {$argv[0]} COMMAND
+
+Commands:
+ test
+ hello
+ createfile
+
+EOF;
+ exit;
+}
+
+$cmd = $argv[1];
+$func = "cmd_{$cmd}";
+if (!function_exists($func)) {
+ echo red("command '".$cmd."' is not implement")."\n";
+ exit(1);
+}
+
+call_user_func($func);
+
+
+/** Commands */
+
+function cmd_test() {
+ // MySQL
+ try {
+ $db = getMySQL();
+ $jobs_count = $db->result($db->query("SELECT COUNT(*) FROM ".JOBD_TABLE));
+ } catch (Exception $e) {
+ echo red("MySQL connection failed")."\n";
+ exit(1);
+ }
+ echo green("MySQL OK")."\n";
+
+ // jobd
+ try {
+ $jobd = getJobdMaster();
+ $status = $jobd->status(true);
+ $workers_count = count($status->getData()['workers']);
+ if ($workers_count == 2) {
+ echo green("jobd-master and jobd OK");
+ } else {
+ $message = "jobd-master OK, but ";
+ $message .= $workers_count == 1 ? "only 1 worker is connected" : "no workers are connected";
+ echo yellow($message);
+ }
+ echo "\n";
+ } catch (Exception $e) {
+ echo red("jobd-master connection failed: ".$e->getMessage())."\n";
+ exit(1);
+ }
+}
+
+function cmd_hello() {
+ $myname = input('Enter your name: ');
+ try {
+ $job_ids = [];
+ $job_server_map = [];
+
+ for ($server = 1; $server <= 2; $server++) {
+ $id = jobs::manual(job_target::high($server), jobs\Hello::class, ['name' => $myname]);
+ $job_server_map[$id] = $server;
+ $job_ids[] = $id;
+ }
+
+ $results = jobs::run($job_ids);
+ foreach ($results as $job_id => $job_result) {
+ $server = $job_server_map[$job_id];
+ echo "> server {$server}:\n";
+ if ($job_result->isFailed()) {
+ echo red("failed")."\n";
+ } else {
+ echo green($job_result->getStdoutAsJSON()['response'])."\n";
+ }
+ echo "\n";
+ }
+
+ } catch (Exception $e) {
+ echo red("error: ".$e->getMessage())."\n";
+ exit(1);
+ }
+}
+
+function cmd_createfile() {
+ $file = input('Enter file name: ');
+ jobs::add(job_target::any, jobs\CreateFile::class, ['file' => $file]);
+} \ No newline at end of file