File: /var/www/heifetz/heifetz-app/models/Job.php
<?php
declare(strict_types=1);
namespace Models;
use Core\DbLib\NewDbModel;
/**
* @property int $id
* @property string $name
* @property string $data
* @property int $status
* @property string $execute_after
* @property string $last_error
* @property string $created_at
* @property string $updated_at
* @property int $company_id
*/
final class Job extends NewDbModel
{
static string $tableName = 'jobs';
const STATUS_NEW = 0;
const STATUS_IN_PROGRESS = 1;
const STATUS_ERROR = 2;
/** @return self[] */
public static function getNew(?int $limit = null): array
{
$query = self::model()->where('status = ?', self::STATUS_NEW)
->where('execute_after IS NULL OR execute_after <= ?', date('Y-m-d H:i:s'));
if (!empty($limit)) {
$query->limit($limit);
}
return $query->order('created_at')->order('id')->fetchAll();
}
public function setStatus(int $status, ?string $error = null): void
{
$this->status = $status;
if (!empty($error)) {
$this->last_error = $error;
}
$this->save();
}
public function setStatusNew(?string $error = null): void
{
$this->setStatus(self::STATUS_NEW, $error);
}
public function setStatusInProgress(): void
{
$this->setStatus(self::STATUS_IN_PROGRESS);
}
public function setStatusError(?string $error = null): void
{
$this->setStatus(self::STATUS_ERROR, $error);
}
}