Skip to content

Commit

Permalink
job listing fix
Browse files Browse the repository at this point in the history
strict types update
zabbix exeption catch
  • Loading branch information
Vítězslav Dvořák committed Aug 29, 2024
1 parent 0987758 commit 379bf2d
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 23 deletions.
9 changes: 8 additions & 1 deletion src/MultiFlexi/AbraFlexi/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@ class Server extends \MultiFlexi\Engine implements \MultiFlexi\platformServer
{
/**
* SQL Table we use.
*
* @param null|mixed $identifier
* @param mixed $options
*/
public string $myTable = 'servers';
public function __construct($identifier = null, $options = [])
{
$this->myTable = 'servers';
parent::__construct($identifier, $options);
}

/**
* Connection Environment by Server.
Expand Down
8 changes: 6 additions & 2 deletions src/MultiFlexi/Action/Zabbix.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,12 @@ public function perform(): void
$packet = new \MultiFlexi\Zabbix\Request\Packet();
$packet->addMetric((new \MultiFlexi\Zabbix\Request\Metric($zabbixKey, $dataForZabbix))->withHostname($me));
$zabbixSender = new \MultiFlexi\ZabbixSender($server);
$zabbixSender->send($packet);
$this->addStatusMessage(sprintf(_('Job metric %s sent to %s as %s'), $zabbixKey, $server, $me), 'debug');

try {
$zabbixSender->send($packet);
$this->addStatusMessage(sprintf(_('Job metric %s sent to %s as %s'), $zabbixKey, $server, $me), 'debug');
} catch (\Exception $exc) {
}
} else {
$this->addStatusMessage(_('No Data For zabix provided'), 'warning');
}
Expand Down
12 changes: 10 additions & 2 deletions src/MultiFlexi/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public function newJob(int $runtemplateId, array $environment, string $scheduled
$environment['JOB_ID']['value'] = $jobId;
$this->environment = $environment;
$this->updateToSQL(['env' => serialize($environment), 'command' => $this->getCmdline()], ['id' => $jobId]);

return $jobId;
}

Expand Down Expand Up @@ -332,13 +333,20 @@ public function scheduleJobRun($when)
*
* @param array $messageData override fields
*/
public function reportToZabbix($messageData): void
public function reportToZabbix($messageData): bool
{
$packet = new ZabbixPacket();
$hostname = \Ease\Shared::cfg('ZABBIX_HOST');
$this->zabbixMessageData = array_merge($this->zabbixMessageData, $messageData);
$packet->addMetric((new ZabbixMetric('job-['.$this->company->getDataValue('code').'-'.$this->application->getDataValue('code').'-'.$this->runTemplate->getMyKey().']', json_encode($this->zabbixMessageData)))->withHostname($hostname));
$this->zabbixSender->send($packet);

try {
$result = $this->zabbixSender->send($packet);
} catch (\Exception $exc) {
$result = false;
}

return $result;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/MultiFlexi/LogToZabbix.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class LogToZabbix implements \Ease\Logger\Loggingable
/**
* @var ZabbixSender Sender Object
*/
public ZabbixSender $sender = null;
public ZabbixSender $sender;

/**
* Saves obejct instace (singleton...).
Expand Down
23 changes: 16 additions & 7 deletions src/MultiFlexi/RunTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,21 +200,30 @@ public function setPeriods(int $companyId, array $runtemplateIds, string $interv
}
}

public function notifyZabbix(array $jobInterval)
public function notifyZabbix(array $jobInfo)
{
$zabbixSender = new ZabbixSender(\Ease\Shared::cfg('ZABBIX_SERVER'));
$hostname = \Ease\Shared::cfg('ZABBIX_HOST');
$company = new Company($jobInterval['company_id']);
$application = new Application($jobInterval['app_id']);
$company = new Company($jobInfo['company_id']);
$application = new Application($jobInfo['app_id']);

$packet = new ZabbixPacket();
$packet->addMetric((new ZabbixMetric('job-['.$company->getDataValue('code').'-'.$application->getDataValue('code').'-'.$jobInterval['id'].'-interval]', $jobInterval['interv']))->withHostname($hostname));
$zabbixSender->send($packet);
$packet->addMetric((new ZabbixMetric('job-['.$company->getDataValue('code').'-'.$application->getDataValue('code').'-'.$jobInfo['id'].'-interval]', $jobInfo['interv']))->withHostname($hostname));

try {
$zabbixSender->send($packet);
} catch (\Exception $ex) {
}

$packet = new ZabbixPacket();
$packet->addMetric((new ZabbixMetric('job-['.$company->getDataValue('code').'-'.$application->getDataValue('code').'-'.$jobInterval['id'].'-interval_seconds]', Job::codeToSeconds($jobInterval['interv'])))->withHostname($hostname));
$packet->addMetric((new ZabbixMetric('job-['.$company->getDataValue('code').'-'.$application->getDataValue('code').'-'.$jobInfo['id'].'-interval_seconds]', Job::codeToSeconds($jobInfo['interv'])))->withHostname($hostname));

try {
$result = $zabbixSender->send($packet);
} catch (\Exception $ex) {
}

return $zabbixSender->send($packet);
return $result;
}

/**
Expand Down
10 changes: 6 additions & 4 deletions src/MultiFlexi/Zabbix/ZabbixSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ public function enable(): void
* @throws Exception
* @throws ZabbixNetworkException
*/
public function send(ZabbixPacket $packet): void
public function send(ZabbixPacket $packet): bool
{
if ($this->disable) {
return;
return false;
}

$payload = $this->makePayload($packet);
Expand Down Expand Up @@ -191,7 +191,7 @@ public function send(ZabbixPacket $packet): void
break;
}

$this->checkResponse($socket);
return $this->checkResponse($socket);
}

/**
Expand Down Expand Up @@ -221,7 +221,7 @@ private function makePayload(ZabbixPacket $packet): string
* @throws ZabbixNetworkException
* @throws ZabbixResponseException
*/
private function checkResponse($socket): void
private function checkResponse($socket): bool
{
$responseBuffer = '';
$responseBufferLength = 2048;
Expand Down Expand Up @@ -270,5 +270,7 @@ private function checkResponse($socket): void
'zabbix server returned non-successfull response',
);
}

return true;
}
}
14 changes: 10 additions & 4 deletions src/MultiFlexi/ZabbixSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,13 @@ public static function zbxCreateHeader($plain_data_size, $compressed_data_size =
*
* @throws \Exception
* @throws ZabbixNetworkException
*
* @return bool Success
*/
public function send(ZabbixPacket $packet): void
public function send(ZabbixPacket $packet): bool
{
if ($this->disable) {
return;
return false;
}

$payload = $this->preparePayload($packet);
Expand Down Expand Up @@ -173,7 +175,7 @@ public function send(ZabbixPacket $packet): void
break;
}

$this->checkResponse($socket);
return $this->checkResponse($socket);
}

/**
Expand All @@ -183,8 +185,10 @@ public function send(ZabbixPacket $packet): void
*
* @throws ZabbixNetworkException
* @throws ZabbixResponseException
*
* @return bool Success
*/
private function checkResponse($socket): void
private function checkResponse($socket): bool
{
$responseBuffer = '';
$responseBufferLength = 2048;
Expand Down Expand Up @@ -232,5 +236,7 @@ private function checkResponse($socket): void
'zabbix server returned non-successfull response',
);
}

return $zabbixResponse->isSuccess();
}
}
5 changes: 3 additions & 2 deletions src/company.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@

$jobs = $jobber->listingQuery()->select(['apps.name AS appname', 'apps.image AS appimage', 'job.id', 'begin', 'exitcode', 'launched_by', 'job.executor', 'login', 'job.app_id AS app_id', 'runtemplate.id AS runtemplate_id', 'runtemplate.name AS runtemplate_name', 'schedule AS scheduled'], true)->leftJoin('apps ON apps.id = job.app_id')->leftJoin('user ON user.id = job.launched_by')->leftJoin('runtemplate ON runtemplate.company_id = job.company_id AND runtemplate.app_id = job.app_id')->where('job.company_id', $companies->getMyKey())->limit(20)->orderBy('job.id DESC')->where($condition)->fetchAll();
$jobList = new \Ease\TWB4\Table();
$jobList->addRowHeaderColumns([_('Run template').' / '._('Application'), _('Job ID'), _('Launch time'), _('Exit Code'), _('Launcher'), _('Launch now'), _('Launch in Background')]);
$jobList->addRowHeaderColumns([_('Run template').' / '._('Application'), _('Job ID'), _('Launch time'), _('Exit Code'), _('Launcher'), _('Schedule Launch')]);

foreach ($jobs as $job) {
$job['appname'] = '<strong>'.(empty($job['runtemplate_name']) ? '#'.$job['runtemplate_id'] : $job['runtemplate_name']).'</strong>&nbsp;/&nbsp;'.$job['appname'];
$job['launch'] = new \Ease\TWB4\LinkButton('launch.php?id='.$job['runtemplate_id'].'&app_id='.$job['app_id'].'&company_id='.$companies->getMyKey(), [_('Launch').'&nbsp;&nbsp;', new \Ease\Html\ImgTag('images/rocket.svg', _('Launch'), ['height' => '30px'])], 'warning btn-lg');
unset($job['runtemplate_name']);
// $job['launch'] = new \Ease\TWB4\LinkButton('launch.php?id='.$job['runtemplate_id'].'&app_id='.$job['app_id'].'&company_id='.$companies->getMyKey(), [_('Launch').'&nbsp;&nbsp;', new \Ease\Html\ImgTag('images/rocket.svg', _('Launch'), ['height' => '30px'])], 'warning btn-lg');

if ($job['begin']) {
$job['schedule'] = new \Ease\TWB4\LinkButton('schedule.php?id='.$job['runtemplate_id'].'&app_id='.$job['app_id'].'&company_id='.$companies->getMyKey(), [_('Schedule').'&nbsp;&nbsp;', new \Ease\Html\ImgTag('images/launchinbackground.svg', _('Launch'), ['height' => '30px'])], 'primary btn-lg');
Expand Down

0 comments on commit 379bf2d

Please sign in to comment.