1 | <?php |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
2 | |||
3 | namespace Gewaer\Cli\Tasks; |
||
4 | |||
5 | use function Gewaer\Core\appPath; |
||
6 | use Phalcon\Cache\Backend\Libmemcached; |
||
7 | use Phalcon\Cli\Task as PhTask; |
||
8 | use RecursiveDirectoryIterator; |
||
9 | use RecursiveIteratorIterator; |
||
10 | use Phalcon\Queue\Beanstalk\Extended as BeanstalkExtended; |
||
11 | use Phalcon\Queue\Beanstalk\Job; |
||
12 | |||
13 | /** |
||
14 | * Class ClearcacheTask |
||
15 | * |
||
16 | * @package Niden\Cli\Tasks |
||
17 | * |
||
18 | * @property Libmemcached $cache |
||
19 | */ |
||
20 | class ClearcacheTask extends PhTask |
||
21 | { |
||
22 | /** |
||
23 | * Clears the data cache from the application |
||
24 | */ |
||
25 | 1 | public function mainAction() |
|
26 | { |
||
27 | 1 | $this->clearFileCache(); |
|
28 | 1 | $this->clearMemCached(); |
|
29 | 1 | } |
|
30 | |||
31 | /** |
||
32 | * Clears file based cache |
||
33 | */ |
||
34 | 1 | private function clearFileCache() : void |
|
35 | { |
||
36 | 1 | echo 'Clearing Cache folders' . PHP_EOL; |
|
37 | |||
38 | 1 | $fileList = []; |
|
39 | 1 | $whitelist = ['.', '..', '.gitignore']; |
|
40 | 1 | $path = appPath('storage/cache'); |
|
41 | 1 | $dirIterator = new RecursiveDirectoryIterator($path); |
|
42 | 1 | $iterator = new RecursiveIteratorIterator( |
|
43 | 1 | $dirIterator, |
|
44 | 1 | RecursiveIteratorIterator::CHILD_FIRST |
|
45 | ); |
||
46 | |||
47 | /** |
||
48 | * Get how many files we have there and where they are |
||
49 | */ |
||
50 | 1 | foreach ($iterator as $file) { |
|
51 | 1 | if (true !== $file->isDir() && true !== in_array($file->getFilename(), $whitelist)) { |
|
52 | 1 | $fileList[] = $file->getPathname(); |
|
53 | } |
||
54 | } |
||
55 | |||
56 | 1 | echo sprintf('Found %s files', count($fileList)) . PHP_EOL; |
|
57 | 1 | foreach ($fileList as $file) { |
|
58 | 1 | echo '.'; |
|
59 | 1 | unlink($file); |
|
60 | } |
||
61 | |||
62 | 1 | echo PHP_EOL . 'Cleared Cache folders' . PHP_EOL; |
|
63 | 1 | } |
|
64 | |||
65 | /** |
||
66 | * Clears memcached data cache |
||
67 | */ |
||
68 | 1 | private function clearMemCached() : void |
|
69 | { |
||
70 | 1 | echo 'Clearing data cache' . PHP_EOL; |
|
71 | 1 | $options = $this->cache->getOptions(); |
|
72 | 1 | $servers = $options['servers'] ?? []; |
|
73 | 1 | $memcached = new \Memcached(); |
|
74 | 1 | foreach ($servers as $server) { |
|
75 | 1 | $memcached->addServer($server['host'], $server['port'], $server['weight']); |
|
76 | } |
||
77 | |||
78 | 1 | $keys = $memcached->getAllKeys(); |
|
79 | //print_r($keys); |
||
80 | 1 | echo sprintf('Found %s keys', count($keys)) . PHP_EOL; |
|
81 | 1 | foreach ($keys as $key) { |
|
82 | if ('bakaapi-' === substr($key, 0, 8)) { |
||
83 | $server = $memcached->getServerByKey($key); |
||
84 | $result = $memcached->deleteByKey($server['host'], $key); |
||
85 | $resultCode = $memcached->getResultCode(); |
||
86 | if (true === $result && $resultCode !== \Memcached::RES_NOTFOUND) { |
||
87 | echo '.'; |
||
88 | } else { |
||
89 | echo 'F'; |
||
90 | } |
||
91 | } |
||
92 | } |
||
93 | |||
94 | 1 | echo PHP_EOL . 'Cleared data cache' . PHP_EOL; |
|
95 | 1 | } |
|
96 | |||
97 | /** |
||
98 | * Clean user session |
||
99 | * |
||
100 | * @return void |
||
101 | */ |
||
102 | public function sessionsAction() : void |
||
103 | { |
||
104 | //call queue |
||
105 | $queue = new BeanstalkExtended([ |
||
106 | 'host' => $this->config->beanstalk->host, |
||
107 | 'prefix' => $this->config->beanstalk->prefix, |
||
108 | ]); |
||
109 | |||
110 | //call que que tube |
||
111 | $queue->addWorker(getenv('SESSION_QUEUE'), function (Job $job) { |
||
112 | // Here we should collect the meta information, make the screenshots, convert the video to the FLV etc. |
||
113 | |||
114 | $sessionId = $job->getBody(); |
||
115 | echo "\nProccessing: {$sessionId}\n"; |
||
116 | |||
117 | $session = new \Baka\Auth\Models\Sessions(); |
||
118 | $session->clean($sessionId, true); |
||
119 | |||
120 | // It's very important to send the right exit code! |
||
121 | exit(0); |
||
122 | }); |
||
123 | |||
124 | // Start processing queues |
||
125 | $queue->doWork(); |
||
126 | } |
||
127 | } |
||
128 |