Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
R
r-test
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Jobs
Commits
Open sidebar
Telenkov Ruslan
r-test
Commits
f280027c
Commit
f280027c
authored
May 24, 2022
by
Telenkov Ruslan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
drush batch
parent
31fbe39b
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
380 additions
and
6 deletions
+380
-6
drush.links.menu.yml
app/docroot/modules/custom/drush/drush.links.menu.yml
+5
-0
drush.routing.yml
app/docroot/modules/custom/drush/drush.routing.yml
+7
-0
BatchService.php
app/docroot/modules/custom/drush/src/BatchService.php
+99
-0
BatchCommands.php
...croot/modules/custom/drush/src/Commands/BatchCommands.php
+63
-6
DrushSettingsForm.php
...croot/modules/custom/drush/src/Form/DrushSettingsForm.php
+206
-0
No files found.
app/docroot/modules/custom/drush/drush.links.menu.yml
0 → 100644
View file @
f280027c
drush.admin
:
title
:
'
Drush
admin'
description
:
'
drush
ex'
parent
:
system.admin_config_development
route_name
:
drush.settings
app/docroot/modules/custom/drush/drush.routing.yml
0 → 100644
View file @
f280027c
drush.settings
:
path
:
'
/admin/config/bootcamp/drush'
defaults
:
_form
:
'
\Drupal\drush\Form\DrushSettingsForm'
_title
:
'
Drush
Settings
form'
requirements
:
_permission
:
'
administer
site
configuration'
app/docroot/modules/custom/drush/src/BatchService.php
0 → 100644
View file @
f280027c
<?php
namespace
Drupal\drush
;
use
Drupal\Core\Datetime\Element\Datetime
;
class
BatchService
{
/**
* Processor for batch operations.
*/
public
function
processItems
(
$items
,
array
&
$context
)
{
// Elements per operation.
$limit
=
5
;
$service
=
\Drupal
::
service
(
'myservice.my'
);
// Set default progress values.
// если прогресс пустой
if
(
empty
(
$context
[
'sandbox'
][
'progress'
]))
{
//$context['sandbox']['progress'] = 0;
$context
[
'sandbox'
][
'max'
]
=
count
(
$items
);
}
// если пустой массив элементов
// Save items to array which will be changed during processing.
if
(
empty
(
$context
[
'sandbox'
][
'items'
]))
{
$context
[
'sandbox'
][
'items'
]
=
$items
;
}
$counter
=
0
;
// есои массив элементов не пустой и если прогресс не равен 0, то распиливем массив и выкидываем уже проверенные элементы
//это при втором и дальнейшем заходе
if
(
!
empty
(
$context
[
'sandbox'
][
'items'
]))
{
// Remove already processed items.
if
(
$context
[
'sandbox'
][
'progress'
]
!=
0
)
{
array_splice
(
$context
[
'sandbox'
][
'items'
],
0
,
$limit
);
}
foreach
(
$context
[
'sandbox'
][
'items'
]
as
$row_item
)
{
//если счетчик не достиг лимита то закидываем каждый элемент
if
(
$counter
!=
$limit
)
{
$this
->
processItem
(
$row_item
,
$service
);
$counter
++
;
$count
++
;
$context
[
'sandbox'
][
'progress'
]
++
;
// $context['message'] = $this->t('Now processing node' . $counter);
// $context['message'] = t('Now processing Batch API overview', array('Batch API overview' => $counter));
$context
[
'message'
]
=
$this
->
t
(
'Now processing node :progress of :count'
,
[
':progress'
=>
$context
[
'sandbox'
][
'progress'
],
':count'
=>
$context
[
'sandbox'
][
'max'
],
]);
// выводим сообщение о прогрессе, сколько выполнено и сколько максимум
//$context['message'] = $this->t('Обработка элементов:');
}
}
// $context['message'] = $this->t('Now processing node :progress of :count', [
// ':progress' => $context['sandbox']['progress'],
// ':count' => $context['sandbox']['max'],
// ]);
}
// Increment total processed item values. Will be used in finished
// callback.
$context
[
'results'
][
'processed'
]
+=
$count
;
// If not finished all tasks, we count percentage of process. 1 = 100%.
if
(
$context
[
'sandbox'
][
'progress'
]
!=
$context
[
'sandbox'
][
'max'
])
{
$context
[
'finished'
]
=
$context
[
'sandbox'
][
'progress'
]
/
$context
[
'sandbox'
][
'max'
];
}
}
public
function
processItem
(
$row_item
,
$service
)
{
$type
=
$service
->
nodeType
(
$row_item
->
url
);
// получаю ID ноды для дальнейшей проверки из БД
$id
=
$service
->
nodeId
(
$row_item
->
url
);
// получаю тип ноды для дальнейшей проверки из БД
$nids
=
$service
->
existNode
(
$type
,
$id
);
// вызываю функцию проверки ноды в БД, если есть, то вернется ID
if
(
empty
(
$nids
)){
$service
->
createNode
(
$row_item
);
// вызываю метод создания ноды из сервиса
$service
->
updateNode
(
$row_item
);
}
else
{
$service
->
updateNode
(
$row_item
);
}
}
/**
* Finished callback for batch.
*/
public
function
finished
(
$success
,
$results
,
$operations
)
{
$message
=
$this
->
t
(
'Number of nodes affected by batch: @count'
,
[
'@count'
=>
$results
[
'processed'
],
]);
$this
->
messenger
()
->
addStatus
(
$message
);
}
}
app/docroot/modules/custom/drush/src/Commands/BatchCommands.php
View file @
f280027c
...
@@ -52,20 +52,19 @@ class BatchCommands extends DrushCommands
...
@@ -52,20 +52,19 @@ class BatchCommands extends DrushCommands
$request_items
=
$service
->
getResult
(
$url
);
$request_items
=
$service
->
getResult
(
$url
);
$result_items
=
$request_items
->
results
;
// выкинул служебную информацию
$result_items
=
$request_items
->
results
;
// выкинул служебную информацию
foreach
(
$result_items
as
$row_item
){
foreach
(
$result_items
as
$row_item
)
{
$timestamp_swapi
=
strtotime
(
$row_item
->
edited
);
$timestamp_swapi
=
strtotime
(
$row_item
->
edited
);
if
(
$timestamp
<
$timestamp_swapi
){
if
(
$timestamp
<
$timestamp_swapi
)
{
$type
=
$service
->
nodeType
(
$row_item
->
url
);
// получаю ID ноды для дальнейшей проверки из БД
$type
=
$service
->
nodeType
(
$row_item
->
url
);
// получаю ID ноды для дальнейшей проверки из БД
$id
=
$service
->
nodeId
(
$row_item
->
url
);
// получаю тип ноды для дальнейшей проверки из БД
$id
=
$service
->
nodeId
(
$row_item
->
url
);
// получаю тип ноды для дальнейшей проверки из БД
$nids
=
$service
->
existNode
(
$type
,
$id
);
// вызываю функцию проверки ноды в БД, если есть, то вернется ID
$nids
=
$service
->
existNode
(
$type
,
$id
);
// вызываю функцию проверки ноды в БД, если есть, то вернется ID
$this
->
output
()
->
writeln
(
'Обработана сущность: '
.
$type
.
$id
);
$this
->
output
()
->
writeln
(
'Обработана сущность: '
.
$type
.
$id
);
if
(
empty
(
$nids
)){
if
(
empty
(
$nids
))
{
$service
->
createNode
(
$row_item
);
// вызываю метод создания ноды из сервиса
$service
->
createNode
(
$row_item
);
// вызываю метод создания ноды из сервиса
$service
->
updateNode
(
$row_item
);
$service
->
updateNode
(
$row_item
);
}
}
else
{
else
{
$service
->
updateNode
(
$row_item
);
$service
->
updateNode
(
$row_item
);
}
}
}
}
...
@@ -76,6 +75,64 @@ class BatchCommands extends DrushCommands
...
@@ -76,6 +75,64 @@ class BatchCommands extends DrushCommands
}
while
(
$url
);
}
while
(
$url
);
}
}
}
}
}
/**
* A custom Drush command to displays the given text.
*
* @command drush-command-example:go-batch-api
* @param $date Argument with text to be printed
* @aliases go-batch-api
*/
public
function
goBatchApi
(
$date
=
null
)
{
if
(
$date
===
null
){
$this
->
output
()
->
writeln
(
'Вы не ввели дату'
);
}
else
{
$timestamp
=
strtotime
(
$date
);
$service
=
\Drupal
::
service
(
'myservice.my'
);
$request
=
$service
->
getResult
(
'https://swapi.dev/api/'
);
$operations
=
[];
// Prepare the operation. Here we could do other operations on nodes.
$this
->
output
()
->
writeln
(
"Preparing batch: "
);
// foreach ($request as $api_url) {
//$url = $api_url;
$url
=
'https://swapi.dev/api/people/'
;
do
{
$request_items
=
$service
->
getResult
(
$url
);
$this
->
output
()
->
writeln
(
$url
);
$result_items
=
$request_items
->
results
;
// выкинул служебную информацию
foreach
(
$result_items
as
$row_item
)
{
$timestamp_swapi
=
strtotime
(
$row_item
->
edited
);
if
(
$timestamp
<
$timestamp_swapi
){
$items
[]
=
$row_item
;
}
}
$operations
[]
=
[
'\Drupal\drush\BatchService::processItems'
,
[
$items
]
];
$items
=
[];
$url
=
$request_items
->
next
;
}
while
(
$url
);
// }
$this
->
output
()
->
writeln
(
'1'
);
// 4. Create the batch.
$batch
=
[
'title'
=>
t
(
'test drush'
),
'operations'
=>
$operations
,
'finished'
=>
'\Drupal\drush\BatchService::finished'
,
];
// 5. Add batch operations as new batch sets.
foreach
(
$operations
as
$row
){
$this
->
output
()
->
writeln
(
$row
);
}
batch_set
(
$batch
);
$this
->
output
()
->
writeln
(
'2'
);
// 6. Process the batch sets.
drush_backend_batch_process
();
}
}
}
}
}
app/docroot/modules/custom/drush/src/Form/DrushSettingsForm.php
0 → 100644
View file @
f280027c
<?php
namespace
Drupal\drush\Form
;
use
Drupal\Core\Batch\BatchBuilder
;
use
Drupal\Core\Datetime\DrupalDateTime
;
use
Drupal\Core\Entity\EntityTypeBundleInfo
;
use
Drupal\Core\Form\FormBase
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\node\NodeInterface
;
use
Drupal\node\NodeStorageInterface
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
class
DrushSettingsForm
extends
FormBase
{
public
$progressvalue
;
protected
$nodeBundles
;
/**
* Batch Builder.
*
* @var \Drupal\Core\Batch\BatchBuilder
*/
protected
$batchBuilder
;
/**
* Node storage.
*
* @var \Drupal\node\NodeStorageInterface
*/
protected
$nodeStorage
;
static
$count
=
0
;
/**
* BatchForm constructor.
*/
public
function
__construct
()
{
$this
->
batchBuilder
=
new
BatchBuilder
();
}
/**
* {@inheritdoc}
*/
public
function
getFormId
()
{
return
'drush_batch'
;
}
/**
* {@inheritdoc}
*/
public
function
buildForm
(
array
$form
,
FormStateInterface
$form_state
)
{
$form
[
'help'
]
=
[
'#markup'
=>
$this
->
t
(
'This form set entered publication date to all content of selected type.'
),
];
$form
[
'date'
]
=
[
'#type'
=>
'datetime'
,
'#title'
=>
$this
->
t
(
'Publication date'
),
'#required'
=>
TRUE
,
'#default_value'
=>
new
DrupalDateTime
(
'2000-01-01 00:00:00'
,
'Europe/Moscow'
),
];
$form
[
'actions'
]
=
[
'#type'
=>
'actions'
];
$form
[
'actions'
][
'run'
]
=
[
'#type'
=>
'submit'
,
'#value'
=>
$this
->
t
(
'Run batch'
),
'#button_type'
=>
'primary'
,
];
return
$form
;
}
/**
* {@inheritdoc}
*/
public
function
submitForm
(
array
&
$form
,
FormStateInterface
$form_state
)
{
$date
=
$form_state
->
getValue
(
'date'
);
$timestamp
=
strtotime
(
$date
);
$service
=
\Drupal
::
service
(
'myservice.my'
);
$request
=
$service
->
getResult
(
'https://swapi.dev/api/'
);
$this
->
batchBuilder
->
setTitle
(
$this
->
t
(
'Processing'
))
->
setInitMessage
(
$this
->
t
(
'Initializing.'
))
->
setProgressMessage
(
$this
->
t
(
'Completed @current of @total.'
))
->
setErrorMessage
(
$this
->
t
(
'An error has occurred.'
));
foreach
(
$request
as
$api_url
)
{
$url
=
$api_url
;
//$url = 'https://swapi.dev/api/people/';
do
{
$request_items
=
$service
->
getResult
(
$url
);
$result_items
=
$request_items
->
results
;
// выкинул служебную информацию
foreach
(
$result_items
as
$row_item
)
{
$timestamp_swapi
=
strtotime
(
$row_item
->
edited
);
if
(
$timestamp
<
$timestamp_swapi
){
$items
[]
=
$row_item
;
}
}
$this
->
batchBuilder
->
addOperation
([
$this
,
'processItems'
],
[
$items
]);
$items
=
[];
$url
=
$request_items
->
next
;
}
while
(
$url
);
}
// $this->batchBuilder->setFile(drupal_get_path('module', 'drush') . '/src/Form/DrushSettingsForm.php');
$this
->
batchBuilder
->
setFinishCallback
([
$this
,
'finished'
]);
batch_set
(
$this
->
batchBuilder
->
toArray
());
}
/**
* Processor for batch operations.
*/
public
function
processItems
(
$items
,
array
&
$context
)
{
// Elements per operation.
$limit
=
5
;
$service
=
\Drupal
::
service
(
'myservice.my'
);
// Set default progress values.
// если прогресс пустой
if
(
empty
(
$context
[
'sandbox'
][
'progress'
]))
{
//$context['sandbox']['progress'] = 0;
$context
[
'sandbox'
][
'max'
]
=
count
(
$items
);
}
// если пустой массив элементов
// Save items to array which will be changed during processing.
if
(
empty
(
$context
[
'sandbox'
][
'items'
]))
{
$context
[
'sandbox'
][
'items'
]
=
$items
;
}
$counter
=
0
;
// есои массив элементов не пустой и если прогресс не равен 0, то распиливем массив и выкидываем уже проверенные элементы
//это при втором и дальнейшем заходе
if
(
!
empty
(
$context
[
'sandbox'
][
'items'
]))
{
// Remove already processed items.
if
(
$context
[
'sandbox'
][
'progress'
]
!=
0
)
{
array_splice
(
$context
[
'sandbox'
][
'items'
],
0
,
$limit
);
}
foreach
(
$context
[
'sandbox'
][
'items'
]
as
$row_item
)
{
//если счетчик не достиг лимита то закидываем каждый элемент
if
(
$counter
!=
$limit
)
{
$this
->
processItem
(
$row_item
,
$service
);
$counter
++
;
$count
++
;
$context
[
'sandbox'
][
'progress'
]
++
;
// $context['message'] = $this->t('Now processing node' . $counter);
// $context['message'] = t('Now processing Batch API overview', array('Batch API overview' => $counter));
$context
[
'message'
]
=
$this
->
t
(
'Now processing node :progress of :count'
,
[
':progress'
=>
$context
[
'sandbox'
][
'progress'
],
':count'
=>
$context
[
'sandbox'
][
'max'
],
]);
// выводим сообщение о прогрессе, сколько выполнено и сколько максимум
//$context['message'] = $this->t('Обработка элементов:');
}
}
// $context['message'] = $this->t('Now processing node :progress of :count', [
// ':progress' => $context['sandbox']['progress'],
// ':count' => $context['sandbox']['max'],
// ]);
}
// Increment total processed item values. Will be used in finished
// callback.
$context
[
'results'
][
'processed'
]
+=
$count
;
// If not finished all tasks, we count percentage of process. 1 = 100%.
if
(
$context
[
'sandbox'
][
'progress'
]
!=
$context
[
'sandbox'
][
'max'
])
{
$context
[
'finished'
]
=
$context
[
'sandbox'
][
'progress'
]
/
$context
[
'sandbox'
][
'max'
];
}
}
public
function
processItem
(
$row_item
,
$service
)
{
$type
=
$service
->
nodeType
(
$row_item
->
url
);
// получаю ID ноды для дальнейшей проверки из БД
$id
=
$service
->
nodeId
(
$row_item
->
url
);
// получаю тип ноды для дальнейшей проверки из БД
$nids
=
$service
->
existNode
(
$type
,
$id
);
// вызываю функцию проверки ноды в БД, если есть, то вернется ID
if
(
empty
(
$nids
)){
$service
->
createNode
(
$row_item
);
// вызываю метод создания ноды из сервиса
$service
->
updateNode
(
$row_item
);
}
else
{
$service
->
updateNode
(
$row_item
);
}
}
/**
* Finished callback for batch.
*/
public
function
finished
(
$success
,
$results
,
$operations
)
{
$message
=
$this
->
t
(
'Number of nodes affected by batch: @count'
,
[
'@count'
=>
$results
[
'processed'
],
]);
$this
->
messenger
()
->
addStatus
(
$message
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment