This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 612962a
Showing
10 changed files
with
895 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/vendor | ||
composer.phar | ||
composer.lock | ||
.DS_Store | ||
/.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
filter: | ||
paths: | ||
- 'src/*' | ||
excluded_paths: | ||
- 'vendor/*' | ||
- 'tests/*' | ||
tools: | ||
php_cs_fixer: | ||
config: { level: psr2 } | ||
checks: | ||
php: | ||
code_rating: true | ||
duplication: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Nekrasov Ilya | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
## Not for public usage | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "arrilot/bitrix-sync", | ||
"license": "MIT", | ||
"keywords": [], | ||
"authors": [ | ||
{ | ||
"name": "Nekrasov Ilya", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"homepage": "https://github.com/arrilot/bitrix-sync", | ||
"require": { | ||
"php": ">=5.6.9" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Arrilot\\BitrixSync\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="false" | ||
> | ||
<testsuites> | ||
<testsuite name="Package Test Suite"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,296 @@ | ||
<?php | ||
|
||
namespace Arrilot\BitrixSync; | ||
|
||
use Monolog\Logger; | ||
|
||
abstract class Step | ||
{ | ||
/** | ||
* Название шага для отображения в логах. | ||
* Если не указано то будет использовано FQCN. | ||
* | ||
* @var string | ||
*/ | ||
public $name = ''; | ||
|
||
/** | ||
* Массив данных через которых можно передавать ту или иную информацию между шагами. | ||
* | ||
* @var array | ||
*/ | ||
protected $shared = []; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
protected $startTime; | ||
|
||
/** | ||
* @var Logger | ||
*/ | ||
protected $logger; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
protected $sqlLogsBitrix = false; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
protected $sqlLogsIlluminate = false; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $status; | ||
|
||
/** | ||
* Массив FQCN классов-шагов от которых данный шаг зависит. | ||
* @var array | ||
*/ | ||
public $dependsOn = []; | ||
|
||
/** | ||
* Основной метод. | ||
*/ | ||
abstract public function perform(); | ||
|
||
/** | ||
* @return string | ||
*/ | ||
protected function getName() | ||
{ | ||
return $this->name ? $this->name : get_called_class(); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getStatus() | ||
{ | ||
return $this->status; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
protected function logCurrentMemoryUsage() | ||
{ | ||
$usage = memory_get_usage(true); | ||
if ($usage < 1024) { | ||
$this->logger->info('Current memory usage: ' . $usage . ' B'); | ||
} elseif ($usage < 1024 * 1024) { | ||
$this->logger->info('Current memory usage: ' . $usage / 1024 . ' KB'); | ||
} else { | ||
$this->logger->info('Current memory usage: ' . $usage / 1024 / 1024 . ' MB'); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
protected function logPeakMemoryUsage() | ||
{ | ||
$usage = memory_get_peak_usage(true); | ||
if ($usage < 1024) { | ||
$this->logger->info('Peak memory usage: ' . $usage . ' B'); | ||
} elseif ($usage < 1024 * 1024) { | ||
$this->logger->info('Peak memory usage: ' . $usage / 1024 . ' KB'); | ||
} else { | ||
$this->logger->info('Peak memory usage: ' . $usage / 1024 / 1024 . ' MB'); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
protected function logSqlQueriesSinceStepStart() | ||
{ | ||
if ($this->sqlLogsBitrix) { | ||
$log = \Bitrix\Main\Application::getConnection()->getTracker()->getQueries(); | ||
$count = count($log); | ||
$totalTime = 0; | ||
$details = []; | ||
foreach ($log as $entry) { | ||
$type = strtok(ltrim($entry->getSql()), ' '); | ||
$type = strtolower(strtok($type, "\r\n" )); | ||
if (!isset($details[$type])) { | ||
$details[$type] = ['count' => 0, 'total_time' => 0]; | ||
} | ||
$details[$type]['count']++; | ||
$details[$type]['total_time'] += $entry->getTime(); | ||
$totalTime += $entry->getTime(); | ||
} | ||
$this->logger->info( | ||
sprintf('SQL запросов bitrix с начала шага: %s, время выполнения %s сек.', $count, $totalTime), | ||
$details | ||
); | ||
} | ||
|
||
if ($this->sqlLogsIlluminate) { | ||
$log = \Illuminate\Database\Capsule\Manager::getQueryLog(); | ||
$count = count($log); | ||
$totalTime = 0; | ||
$details = []; | ||
foreach ($log as $entry) { | ||
$type = strtok($entry['query'], ' '); | ||
if (!isset($details[$type])) { | ||
$details[$type] = ['count' => 0, 'total_time' => 0]; | ||
} | ||
$details[$type]['count']++; | ||
$details[$type]['total_time'] += $entry['time'] / 1000; | ||
$totalTime += $entry['time'] / 1000; | ||
} | ||
$this->logger->info( | ||
sprintf('SQL запросов illuminate/database с начала шага: %s, время выполнения %s сек.', $count, $totalTime), | ||
$details | ||
); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Эти методы позволяют вклиниться в этапы жизненного цикла шага. | ||
* Выполняются эти методы именно в таком порядке. | ||
* Сам метод peform выполняетс между onAfterLogStart и onBeforeLogFinish | ||
*/ | ||
public function onBeforeLogStart() { } | ||
public function onAfterLogStart() { } | ||
public function onBeforeLogFinish() { } | ||
public function onAfterLogFinish() { } | ||
|
||
/** | ||
* Завершает шаг как пропущенный. | ||
* @param string $message | ||
* @throws StopStepException | ||
*/ | ||
public function stopAsSkipped($message = '') | ||
{ | ||
$this->status = 'skipped'; | ||
throw new StopStepException($message); | ||
} | ||
|
||
/** | ||
* Завершает шаг как успешно завершенный. | ||
* @param string $message | ||
* @throws StopStepException | ||
*/ | ||
public function stopAsFinished($message = '') | ||
{ | ||
$this->status = 'finished'; | ||
throw new StopStepException($message); | ||
} | ||
|
||
/** | ||
* Завершает шаг как проваленный. | ||
* @param string $message | ||
* @throws StopStepException | ||
*/ | ||
public function stopAsFailed($message = '') | ||
{ | ||
$this->status = 'failed'; | ||
throw new StopStepException($message); | ||
} | ||
|
||
/** | ||
* Завершает всю синхронизацию. | ||
* @param string $message | ||
* @throws StopSyncException | ||
*/ | ||
public function stopEverything($message = '') | ||
{ | ||
$this->status = 'failed'; | ||
throw new StopSyncException($message); | ||
} | ||
|
||
/** | ||
* @param Logger $logger | ||
* @return $this | ||
*/ | ||
public function setLogger(Logger $logger) | ||
{ | ||
$this->logger = $logger; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param $data | ||
* @return $this | ||
*/ | ||
public function setSharedData(&$data) | ||
{ | ||
$this->shared = &$data; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function logStart() | ||
{ | ||
$this->startTime = microtime(true); | ||
$this->logger->info('=============================================='); | ||
$this->logger->info(sprintf('Шаг "%s" начат', $this->getName())); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function logFinish() | ||
{ | ||
$this->logger->info(sprintf('Шаг "%s" завершён', $this->getName())); | ||
$time = microtime(true) - $this->startTime; | ||
if ($time > 60) { | ||
$this->logger->info("Затраченное время: " . $time / 60 ." минут"); | ||
} else { | ||
$this->logger->info("Затраченное время: " . $time ." секунд"); | ||
} | ||
|
||
$this->logSqlQueriesSinceStepStart(); | ||
$this->flushSqlLogs(); | ||
$this->logCurrentMemoryUsage(); | ||
$this->logPeakMemoryUsage(); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Установка параметров для логирования SQL запросов. | ||
* | ||
* @param $sqlLogsBitrix | ||
* @param $sqlLogsIlluminate | ||
* @return $this | ||
*/ | ||
public function setSqlLoggingParams($sqlLogsBitrix, $sqlLogsIlluminate) | ||
{ | ||
$this->sqlLogsBitrix = $sqlLogsBitrix; | ||
$this->sqlLogsIlluminate = $sqlLogsIlluminate; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Обнуление sql-трэкеров после завершения шага | ||
*/ | ||
public function flushSqlLogs() | ||
{ | ||
if ($this->sqlLogsBitrix) { | ||
\Bitrix\Main\Application::getConnection()->getTracker()->reset(); | ||
} | ||
|
||
if ($this->sqlLogsIlluminate) { | ||
\Illuminate\Database\Capsule\Manager::flushQueryLog(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Arrilot\BitrixSync; | ||
|
||
use Exception; | ||
|
||
class StopStepException extends Exception | ||
{ | ||
|
||
} |
Oops, something went wrong.