Implemented Dynamic Items for data to be sent to polled systems based on data in db, like stats/nodelists

This commit is contained in:
2023-12-03 18:18:05 +11:00
parent 8f3d77b04d
commit 1890b66dc7
10 changed files with 366 additions and 4 deletions

View File

@@ -6,6 +6,7 @@ use Exception;
use Illuminate\Support\Facades\Log;
use League\Flysystem\UnreadableFileEncountered;
use App\Classes\File\Send\Dynamic;
use App\Classes\Node;
use App\Models\Address;
@@ -129,6 +130,31 @@ class Send extends Base
$this->index = NULL;
}
public function dynamic(Address $ao): bool
{
$file = FALSE;
// If the node is marked as hold - dont send any files.
if ($ao->system->hold) {
Log::info(sprintf('%s: - System [%d] is marked as hold - not checking for files.',self::LOGKEY,$ao->system_id));
return FALSE;
}
// Files
if (($x=$ao->dynamicWaiting())->count()) {
Log::debug(sprintf('%s:- [%d] Dynamic Files(s) added for sending to [%s]',self::LOGKEY,$x->count(),$ao->ftn));
// Add Files
foreach ($x as $do)
$this->list->push(new Dynamic($do,$ao,self::T_FILE));
$file = TRUE;
}
return $file;
}
/*
private function compress(string $comp_mode): void
{

View File

@@ -0,0 +1,124 @@
<?php
namespace App\Classes\File\Send;
use Carbon\Carbon;
use App\Classes\File\Send;
use App\Classes\Node;
use App\Models\Address;
use App\Models\Dynamic as Model;
use App\Classes\Dynamic as Item;
/**
* Dynamic files that are sent to systems during a mailer session
*/
final class Dynamic extends Send
{
private const LOGKEY = 'FSD';
/** @var int Our internal position counter */
private int $readpos = 0;
private string $buffer;
private Item $item;
private Carbon $sent;
/**
* @throws \Exception
*/
public function __construct(private Model $do,Address $ao,int $type)
{
parent::__construct();
$this->ftype = ((($type&0xff)<<8)|self::IS_FILE);
$this->item = new $this->do->model($ao,$this->do->arguments);
$this->sent = Carbon::now();
}
public function __get($key) {
switch ($key) {
case 'dbids':
return collect([$this->do->id]);
case 'nameas':
return $this->item->getName();
case 'mtime':
return $this->sent->timestamp;
case 'size':
return strlen($this->buffer);
default:
return NULL;
}
}
public function close(bool $successful,Node $node): void
{
if ($successful) {
$this->complete = TRUE;
$next_at = $this->do->next_at
->startOfDay()
->addHours($this->do->start_time->hour)
->addMinutes($this->do->start_time->minute);
switch ($this->do->frequency) {
case 'ONCE':
$this->do->active = FALSE;
break;
case 'DAILY':
$this->do->next_at = $next_at
->addDay();
break;
case 'WEEKLY':
$this->do->next_at = $next_at
->addWeek();
break;
case 'MONTHLY':
$this->do->next_at = $next_at
->addMonth();
break;
default:
throw new \Exception(sprintf('%s:! Unknown frequency [%s] for [%d]',self::LOGKEY,$this->do->frequency,$this->do->id));
}
$this->do->save();
}
}
public function feof(): bool
{
return ($this->readpos === $this->size);
}
public function open(string $compress=''): bool
{
$this->buffer = (string)$this->item;
return TRUE;
}
public function read(int $length): string
{
$result = substr($this->buffer,$this->readpos,$length);
$this->readpos += strlen($result);
return $result;
}
public function seek(int $pos): bool
{
$this->readpos = ($pos < $this->size) ? $pos : $this->size;
return TRUE;
}
}