Skip to content

Commit 8b159e6

Browse files
committed
Allow processing of documentation through queued job
1 parent 9700a4b commit 8b159e6

File tree

3 files changed

+121
-41
lines changed

3 files changed

+121
-41
lines changed

console/DocsProcess.php

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Winter\Docs\Console;
44

5+
use Illuminate\Support\Facades\Queue;
56
use Winter\Docs\Classes\DocsManager;
7+
use Winter\Docs\Jobs\ProcessDoc;
68
use Winter\Storm\Console\Command;
79

810
class DocsProcess extends Command
@@ -19,6 +21,7 @@ class DocsProcess extends Command
1921
*/
2022
protected $signature = 'docs:process
2123
{id? : The identifier of the documentation to process}
24+
{--j|queue= : Add as a job to the queue}
2225
{--t|token= : An authorization token to use when downloading the documentation}
2326
{--m|memory-limit= : The memory limit to use when processing documentation}';
2427

@@ -58,50 +61,23 @@ public function handle()
5861
}
5962
}
6063

61-
foreach ($ids as $id) {
62-
$this->processDoc($id);
63-
}
64-
}
65-
66-
public function processDoc(string $id)
67-
{
68-
$docsManager = DocsManager::instance();
69-
$doc = $docsManager->getDocumentation($id);
70-
71-
$this->line('');
72-
73-
if (is_null($doc)) {
74-
$this->error('No documentation by the given ID exists.');
75-
return;
76-
}
77-
78-
$this->info('Processing ' . $id);
79-
80-
// Download documentation
81-
if ($doc->isRemote()) {
82-
$this->line(' - Downloading documentation');
83-
$doc->download($this->option('token'));
84-
85-
$this->line(' - Extracting documentation');
86-
$doc->extract();
64+
if (is_null($this->option('queue'))) {
65+
$queue = false;
8766
} else {
88-
$this->line(' - Documentation is locally available, skipping download');
67+
$queue = $this->option('queue') ?: 'default';
8968
}
9069

91-
// Process documentation
92-
$this->line(' - Processing documentation');
93-
$doc->process();
94-
$doc->resetState();
95-
96-
$pageList = $doc->getPageList();
97-
$this->line(' - Processed ' . count($pageList) . ' page(s)');
98-
99-
if ($pageList->isSearchable()) {
100-
$this->line(' - Indexing documentation');
101-
$pageList->index();
70+
foreach ($ids as $id) {
71+
if ($queue === false) {
72+
(new ProcessDoc())->processDoc($id, $this->option('token'), $this);
73+
} else {
74+
Queue::push(ProcessDoc::class, [
75+
'id' => $id,
76+
'token' => $this->option('token'),
77+
'memory_limit' => $this->option('memory-limit'),
78+
], $queue);
79+
$this->info('- Added documentation processing to queue "' . $queue . '": ' . $id);
80+
}
10281
}
103-
104-
$this->line(' - Clean up downloaded files');
105-
$doc->cleanupDownload();
10682
}
10783
}

jobs/ProcessDoc.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace Winter\Docs\Jobs;
4+
5+
use Winter\Docs\Classes\DocsManager;
6+
use Winter\Storm\Console\Command;
7+
use Winter\Storm\Exception\ApplicationException;
8+
9+
class ProcessDoc
10+
{
11+
/**
12+
* The command instance, if run through the console.
13+
*/
14+
protected ?Command $command = null;
15+
16+
/**
17+
* Execute the job.
18+
*/
19+
public function fire($job, $data)
20+
{
21+
if ($data['memory_limit']) {
22+
ini_set('memory_limit', $data['memory_limit']);
23+
}
24+
25+
$this->processDoc($data['id'], $data['token']);
26+
$job->delete();
27+
}
28+
29+
/**
30+
* Processes a documentation by the given ID.
31+
*
32+
* You may optionally provide a token to use when downloading the documentation as the second argument.
33+
*
34+
* This can either be run through the `docs:process` command, or as a queued job. If run through the command, the
35+
* command instance should be passed as the third argument.
36+
*/
37+
public function processDoc(string $id, ?string $token = null, ?Command $command = null)
38+
{
39+
$this->command = $command;
40+
41+
$docsManager = DocsManager::instance();
42+
$doc = $docsManager->getDocumentation($id);
43+
44+
$this->line('');
45+
46+
if (is_null($doc)) {
47+
$this->error('No documentation by the given ID exists.');
48+
return;
49+
}
50+
51+
$this->info('Processing ' . $id);
52+
53+
// Download documentation
54+
if ($doc->isRemote()) {
55+
$this->line(' - Downloading documentation');
56+
$doc->download($token);
57+
58+
$this->line(' - Extracting documentation');
59+
$doc->extract();
60+
} else {
61+
$this->line(' - Documentation is locally available, skipping download');
62+
}
63+
64+
// Process documentation
65+
$this->line(' - Processing documentation');
66+
$doc->process();
67+
$doc->resetState();
68+
69+
$pageList = $doc->getPageList();
70+
$this->line(' - Processed ' . count($pageList) . ' page(s)');
71+
72+
if ($pageList->isSearchable()) {
73+
$this->line(' - Indexing documentation');
74+
$pageList->index();
75+
}
76+
77+
$this->line(' - Clean up downloaded files');
78+
$doc->cleanupDownload();
79+
}
80+
81+
protected function line($string): void
82+
{
83+
if ($this->command) {
84+
$this->command->line($string);
85+
}
86+
}
87+
88+
protected function info($string): void
89+
{
90+
if ($this->command) {
91+
$this->command->info($string);
92+
}
93+
}
94+
95+
protected function error($string): void
96+
{
97+
if ($this->command) {
98+
$this->command->error($string);
99+
} else {
100+
throw new ApplicationException($string);
101+
}
102+
}
103+
}

updates/version.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
"1.0.0": Initial version of the Winter Docs plugin.
2+
"1.0.1": Allow processing of documentation through job queue.

0 commit comments

Comments
 (0)