mirror of
https://github.com/opensourcepos/opensourcepos.git
synced 2026-09-21 10:15:01 -04:00
Introduce CodeIgniter Tasks-based job scheduling to support long-running background work (e.g. large CSV imports) without blocking requests. - composer.json/composer.lock: add codeigniter4/tasks (and its dependencies codeigniter4/queue, codeigniter4/settings) to power the task scheduler and settings-backed configuration. - app/Models/JobThrottle.php: new model for job_throttles table, providing exists/save/getAll/delete helpers to rate-limit or throttle job execution per throttle_id. - INSTALL.md: document the three trigger modes (Web default via request-hook processing, Auto via cron/Task Scheduler, Manual via admin UI button), including step-by-step Linux/Mac cron and Windows Task Scheduler setup, and Docker considerations (worker container, supervisor, or cron sidecar) since cron does not run inside a single-process container by default. Signed-off-by: objecttothis <17935339+objecttothis@users.noreply.github.com>
81 lines
1.9 KiB
PHP
81 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Database\ResultInterface;
|
|
use CodeIgniter\Model;
|
|
|
|
/**
|
|
* JobThrottle class
|
|
*/
|
|
class JobThrottle extends Model
|
|
{
|
|
protected $table = 'job_throttles';
|
|
protected $primaryKey = 'throttle_id';
|
|
protected $useAutoIncrement = true;
|
|
protected $useSoftDeletes = false;
|
|
protected $allowedFields = [
|
|
'max_count',
|
|
'period',
|
|
'deleted'
|
|
];
|
|
|
|
/**
|
|
* @param int $throttleId
|
|
* @return bool
|
|
*/
|
|
public function exists(int $throttleId): bool
|
|
{
|
|
$builder = $this->db->table('job_throttles');
|
|
$builder->where('throttle_id', $throttleId);
|
|
|
|
return ($builder->get()->getNumRows() >= 1);
|
|
}
|
|
|
|
/**
|
|
* @param array $throttleData
|
|
* @param int $throttleId
|
|
* @return bool
|
|
*/
|
|
public function saveValue(array $throttleData, int $throttleId): bool
|
|
{
|
|
$throttleDataToSave = [
|
|
'max_count' => $throttleData['max_count'],
|
|
'period' => $throttleData['period'],
|
|
'deleted' => 0
|
|
];
|
|
|
|
if (!$this->exists($throttleId)) {
|
|
$builder = $this->db->table('job_throttles');
|
|
return $builder->insert($throttleDataToSave);
|
|
}
|
|
|
|
$builder = $this->db->table('job_throttles');
|
|
$builder->where('throttle_id', $throttleId);
|
|
|
|
return $builder->update($throttleDataToSave);
|
|
}
|
|
|
|
/**
|
|
* @return ResultInterface
|
|
*/
|
|
public function getAll(): ResultInterface
|
|
{
|
|
$builder = $this->db->table('job_throttles');
|
|
$builder->where('deleted', 0);
|
|
|
|
return $builder->get();
|
|
}
|
|
|
|
/**
|
|
* Deletes one throttle
|
|
*/
|
|
public function delete($throttleId = null, bool $purge = false): bool
|
|
{
|
|
$builder = $this->db->table('job_throttles');
|
|
$builder->where('throttle_id', $throttleId);
|
|
|
|
return $builder->update(['deleted' => 1]);
|
|
}
|
|
}
|