Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuravljov committed Jan 6, 2018
1 parent dd21ec6 commit 9fa037b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
28 changes: 15 additions & 13 deletions src/Behavior.php
Expand Up @@ -78,7 +78,7 @@ public function events()
public function afterPush(PushEvent $event)
{
$push = new PushRecord();
$push->sender_name = $this->getSenderName();
$push->sender_name = $this->getSenderName($event);
$push->job_uid = $event->id;
$push->setJob($event->job);
$push->ttr = $event->ttr;
Expand All @@ -102,7 +102,7 @@ public function beforeExec(ExecEvent $event)
return;
}
$this->env->db->transaction(function () use ($event, $push) {
$worker = $this->getWorkerRecord();
$worker = $this->getWorkerRecord($event);

$exec = new ExecRecord();
$exec->push_id = $push->id;
Expand Down Expand Up @@ -174,8 +174,8 @@ public function afterError(ErrorEvent $event)
public function workerStart(WorkerEvent $event)
{
$worker = new WorkerRecord();
$worker->sender_name = $this->getSenderName();
$worker->pid = $this->owner->getWorkerPid();
$worker->sender_name = $this->getSenderName($event);
$worker->pid = $event->sender->getWorkerPid();
$worker->started_at = time();
$worker->save(false);
}
Expand All @@ -186,20 +186,21 @@ public function workerStart(WorkerEvent $event)
public function workerStop(WorkerEvent $event)
{
$this->env->db->close(); // To reopen a lost connection
if ($worker = $this->getWorkerRecord()) {
if ($worker = $this->getWorkerRecord($event)) {
$worker->finished_at = time();
$worker->save(false);
}
}

/**
* @param JobEvent|WorkerEvent $event
* @throws
* @return string
*/
protected function getSenderName()
protected function getSenderName($event)
{
foreach (Yii::$app->getComponents(false) as $id => $component) {
if ($component === $this->owner) {
if ($component === $event->sender) {
return $id;
}
}
Expand All @@ -215,7 +216,7 @@ protected function getPushRecord(JobEvent $event)
if ($event->id !== null) {
return $this->env->db->useMaster(function () use ($event) {
return PushRecord::find()
->byJob($this->getSenderName(), $event->id)
->byJob($this->getSenderName($event), $event->id)
->one();
});
} else {
Expand All @@ -224,17 +225,18 @@ protected function getPushRecord(JobEvent $event)
}

/**
* @return null|WorkerRecord
* @param WorkerEvent|ExecEvent $event
* @return WorkerRecord|null
*/
protected function getWorkerRecord()
protected function getWorkerRecord($event)
{
if (!$this->canTrackWorkers || !$this->owner->getWorkerPid()) {
if (!$this->canTrackWorkers || !$event->sender->getWorkerPid()) {
return null;
}

return $this->env->db->useMaster(function () {
return $this->env->db->useMaster(function () use ($event) {
return WorkerRecord::find()
->byPid($this->owner->getWorkerPid())
->byPid($event->sender->getWorkerPid())
->active()
->one();
});
Expand Down
20 changes: 12 additions & 8 deletions src/views/layouts/_alerts.php
Expand Up @@ -13,11 +13,15 @@
'error' => 'alert-danger',
];
?>
<?php foreach (Yii::$app->session->getAllFlashes(true) as $type => $message): ?>
<?= Alert::widget([
'options' => [
'class' => isset($aliases[$type]) ? $aliases[$type] : $type,
],
'body' => Html::encode($message),
]) ?>
<?php endforeach; ?>
<div>
<?php foreach (Yii::$app->session->getAllFlashes(true) as $type => $message): ?>
<div>
<?= Alert::widget([
'options' => [
'class' => isset($aliases[$type]) ? $aliases[$type] : $type,
],
'body' => Html::encode($message),
]) ?>
</div>
<?php endforeach; ?>
</div>

0 comments on commit 9fa037b

Please sign in to comment.