This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
/
worker.php
153 lines (131 loc) · 5.08 KB
/
worker.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/*
* Copyright 2007-2015 Abstrium <contact (at) pydio.com>
* This file is part of Pydio.
*
* Pydio is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <http://pyd.io/>.
*/
include_once("base.conf.php");
//set_error_handler(array("AJXP_XMLWriter", "catchError"), E_ALL & ~E_NOTICE & ~E_STRICT );
//set_exception_handler(array("AJXP_XMLWriter", "catchException"));
$pServ = AJXP_PluginsService::getInstance();
ConfService::$useSession = false;
AuthService::$useSession = false;
ConfService::init();
ConfService::start();
$confStorageDriver = ConfService::getConfStorageImpl();
require_once($confStorageDriver->getUserClassFileName());
$authDriver = ConfService::getAuthDriverImpl();
ConfService::currentContextIsRestAPI("api");
AJXP_PluginsService::getInstance()->initActivePlugins();
function applyTask($userId, $repoId, $actionName, $parameters){
print($userId." - ".$repoId." - ".$actionName." - ");
print("Log User\n");
AuthService::logUser($userId, "", true);
print("Find Repo\n");
if($repoId == 'pydio'){
ConfService::switchRootDir();
$repo = ConfService::getRepository();
}else{
$repo = ConfService::findRepositoryByIdOrAlias($repoId);
if ($repo == null) {
throw new Exception("Cannot find repository with ID ".$repoId);
}
ConfService::switchRootDir($repo->getId());
}
// DRIVERS BELOW NEED IDENTIFICATION CHECK
print("Load Driver\n");
if (!AuthService::usersEnabled() || ConfService::getCoreConf("ALLOW_GUEST_BROWSING", "auth") || AuthService::getLoggedUser()!=null) {
ConfService::getConfStorageImpl();
ConfService::loadDriverForRepository($repo);
}
print("Init plugins\n");
AJXP_PluginsService::getInstance()->initActivePlugins();
//print "Current repos are" . implode(", ", array_keys(ConfService::getAccessibleRepositories()))."\n";
print("Apply Action\n");
$xmlResult = AJXP_Controller::findActionAndApply($actionName, $parameters, []);
if (!empty($xmlResult) && !headers_sent()) {
AJXP_XMLWriter::header();
print($xmlResult);
AJXP_XMLWriter::close();
}
print("Empty ShutdownScheduler!\n");
AJXP_ShutdownScheduler::getInstance()->callRegisteredShutdown();
print("Invalidate\n");
ConfService::getInstance()->invalidateLoadedRepositories();
print("Disconnect\n");
AuthService::disconnect();
AJXP_PluginsService::updateXmlRegistry(null, true);
}
function deQueue(){
$fName = AJXP_DATA_PATH."/plugins/mq.serial/worker-queue";
if(file_exists($fName)){
$data = file_get_contents($fName);
if(!empty($data)){
$decoded = json_decode($data, true);
if(!empty($decoded) && is_array($decoded) && count($decoded)){
$task = array_pop($decoded);
file_put_contents($fName, json_encode($decoded));
return $task;
}
}
}
return false;
}
$method = isset($argv[1]) ? $argv[1] : 'nsq';
if($method == "nsq"){
include("plugins/core.mq/vendor/autoload.php");
$logger = new nsqphp\Logger\Stderr;
$dedupe = new nsqphp\Dedupe\OppositeOfBloomFilterMemcached;
$lookup = new nsqphp\Lookup\FixedHosts('localhost:4150');
$requeueStrategy = new nsqphp\RequeueStrategy\FixedDelay;
$nsq = new nsqphp\nsqphp($lookup, $dedupe, $requeueStrategy, $logger);
$channel = isset($argv[1]) ? $argv[1] : 'foo';
$nsq->subscribe('pydio', $channel, function($msg) {
echo "READ\t" . $msg->getId() . "\t" . $msg->getPayload() . "\n";
$payload = json_decode($msg->getPayload(), true);
$data = $payload["data"];
applyTask($data["user_id"], $data["repository_id"], $data["action"], $data["parameters"]);
});
$nsq->run();
}else{
while(true){
$task = deQueue();
if($task !== false){
try{
print "--------------------------------------\n";
print "Applying task ".$task["actionName"]."\n";
print_r($task);
applyTask($task["userId"], $task["repoId"], $task["actionName"], $task["parameters"]);
}catch (Exception $e){
print "Error : ".$e->getMessage()."\n";
}
flush();
}else{
print "--------- nothing to do \n";
}
print("5\r");
sleep(1);
print("4\r");
sleep(1);
print("3\r");
sleep(1);
print("2\r");
sleep(1);
print("1\r");
sleep(1);
}
}