diff --git a/src/Console/Command.php b/src/Console/Command.php index ce69dccb..bc76a469 100644 --- a/src/Console/Command.php +++ b/src/Console/Command.php @@ -29,7 +29,7 @@ use Bow\Console\Command\Generator\GenerateNotificationCommand; use Bow\Console\Command\Generator\GenerateConfigurationCommand; use Bow\Console\Command\Generator\GenerateEventListenerCommand; -use Bow\Console\Command\Generator\GenerateJobCommand; +use Bow\Console\Command\Generator\GenerateTaskCommand; use Bow\Console\Command\Generator\GenerateRouterResourceCommand; class Command extends AbstractCommand @@ -57,7 +57,7 @@ class Command extends AbstractCommand "add:validation" => GenerateValidationCommand::class, "add:event" => GenerateAppEventCommand::class, "add:listener" => GenerateEventListenerCommand::class, - "add:job" => GenerateJobCommand::class, + "add:task" => GenerateTaskCommand::class, "add:command" => GenerateConsoleCommand::class, "add:notifier" => GenerateNotifierCommand::class, "run:console" => ReplCommand::class, diff --git a/src/Console/Command/Generator/GenerateJobCommand.php b/src/Console/Command/Generator/GenerateJobCommand.php deleted file mode 100644 index 92a6b5a7..00000000 --- a/src/Console/Command/Generator/GenerateJobCommand.php +++ /dev/null @@ -1,38 +0,0 @@ -setting->getJobDirectory(), - $job - ); - - if ($generator->fileExists()) { - echo Color::red("The job already exists"); - exit(1); - } - - $generator->write('job', [ - 'baseNamespace' => $this->namespaces['job'] ?? 'App\\Jobs' - ]); - - echo Color::green("The job has been well created."); - exit(0); - } -} diff --git a/src/Console/Command/Generator/GenerateTaskCommand.php b/src/Console/Command/Generator/GenerateTaskCommand.php new file mode 100644 index 00000000..385c6334 --- /dev/null +++ b/src/Console/Command/Generator/GenerateTaskCommand.php @@ -0,0 +1,38 @@ +setting->getTaskDirectory(), + $task + ); + + if ($generator->fileExists()) { + echo Color::red("The task already exists"); + exit(1); + } + + $generator->write('task', [ + 'baseNamespace' => $this->namespaces['task'] ?? 'App\\Tasks' + ]); + + echo Color::green("The task has been well created."); + exit(0); + } +} diff --git a/src/Console/Console.php b/src/Console/Console.php index 58afd969..57ee334d 100644 --- a/src/Console/Console.php +++ b/src/Console/Console.php @@ -60,7 +60,7 @@ class Console 'service', 'exception', 'event', - 'job', + 'task', 'command', 'listener', 'notifier' @@ -540,7 +540,7 @@ private function help(?string $command = null): int \033[0;33madd:migration\033[00m Create a new migration \033[0;33madd:event\033[00m Create a new event \033[0;33madd:listener\033[00m Create a new event listener - \033[0;33madd:job\033[00m Create a new job + \033[0;33madd:task\033[00m Create a new task \033[0;33madd:command\033[00m Create a new console command \033[0;33madd:notifier\033[00m Create a new messaging handler @@ -564,7 +564,7 @@ private function help(?string $command = null): int \033[0;32mRUN\033[00m Launch development tools \033[0;33mrun:console\033[00m Show PsySH PHP REPL for debugging code \033[0;33mrun:server\033[00m Start local development server - \033[0;33mrun:worker\033[00m Start consumer/worker to handle queue jobs + \033[0;33mrun:worker\033[00m Start consumer/worker to handle queue tasks USAGE; echo $usage; @@ -593,7 +593,7 @@ private function help(?string $command = null): int \033[0;33m$\033[00m php \033[0;34mbow\033[00m add:seeder name [--seed=n] Create a new seeder \033[0;33m$\033[00m php \033[0;34mbow\033[00m add:migration name Create a new migration \033[0;33m$\033[00m php \033[0;34mbow\033[00m add:event name Create a new event listener - \033[0;33m$\033[00m php \033[0;34mbow\033[00m add:job name Create a new queue job + \033[0;33m$\033[00m php \033[0;34mbow\033[00m add:task name Create a new queue task \033[0;33m$\033[00m php \033[0;34mbow\033[00m add:command name Create a new console command \033[0;33m$\033[00m php \033[0;34mbow\033[00m add:notifier name Create a new messaging handler \033[0;33m$\033[00m php \033[0;34mbow\033[00m add help Display this help @@ -642,7 +642,7 @@ private function help(?string $command = null): int \033[0;33m$\033[00m php \033[0;34mbow\033[00m run:console Show PsySH PHP REPL for debugging code \033[0;33m$\033[00m php \033[0;34mbow\033[00m run:server [option] Start local development server - \033[0;33m$\033[00m php \033[0;34mbow\033[00m run:worker [option] Start worker to handle queue jobs + \033[0;33m$\033[00m php \033[0;34mbow\033[00m run:worker [option] Start worker to handle queue tasks U; // phpcs:enable break; diff --git a/src/Console/Setting.php b/src/Console/Setting.php index a2d13971..92ad0703 100644 --- a/src/Console/Setting.php +++ b/src/Console/Setting.php @@ -137,11 +137,11 @@ class Setting private string $service_directory; /** - * The job directory + * The task directory * * @var string */ - private string $job_directory; + private string $task_directory; /** * The command directory * @@ -402,24 +402,24 @@ public function setServiceDirectory(string $service_directory): void } /** - * Get the job directory + * Get the task directory * * @return string */ - public function getJobDirectory(): string + public function getTaskDirectory(): string { - return $this->job_directory; + return $this->task_directory; } /** - * Set the job directory + * Set the task directory * - * @param string $job_directory + * @param string $task_directory * @return void */ - public function setJobDirectory(string $job_directory): void + public function setTaskDirectory(string $task_directory): void { - $this->job_directory = $job_directory; + $this->task_directory = $task_directory; } /** diff --git a/src/Console/stubs/job.stub b/src/Console/stubs/task.stub similarity index 83% rename from src/Console/stubs/job.stub rename to src/Console/stubs/task.stub index 7bc0238b..a68b64ae 100644 --- a/src/Console/stubs/job.stub +++ b/src/Console/stubs/task.stub @@ -2,9 +2,9 @@ namespace {baseNamespace}{namespace}; -use Bow\Queue\QueueJob; +use Bow\Queue\QueueTask; -class {className} extends QueueJob +class {className} extends QueueTask { /** * {className} constructor diff --git a/src/Database/Barry/Model.php b/src/Database/Barry/Model.php index 7c02302c..d41df871 100644 --- a/src/Database/Barry/Model.php +++ b/src/Database/Barry/Model.php @@ -337,7 +337,7 @@ public static function retrieveAndDelete( } if ($model instanceof Collection) { - $model->delete(); + $model->map(fn ($m) => $m->delete()); return $model; } @@ -861,9 +861,7 @@ public function toArray(): array { return array_filter( $this->attributes, - function ($key) { - return !in_array($key, $this->hidden); - }, + fn ($key) => !in_array($key, $this->hidden), ARRAY_FILTER_USE_KEY ); } @@ -875,9 +873,7 @@ public function jsonSerialize(): array { return array_filter( $this->attributes, - function ($key) { - return !in_array($key, $this->hidden); - }, + fn ($key) => !in_array($key, $this->hidden), ARRAY_FILTER_USE_KEY ); } diff --git a/src/Database/Database.php b/src/Database/Database.php index 0edd7f50..22c9d254 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -426,9 +426,9 @@ public static function inTransaction(): bool */ public static function commit(): void { - static::ensureDatabaseConnection(); - - static::$adapter->getConnection()->commit(); + if (static::inTransaction()) { + static::$adapter->getConnection()->commit(); + } } /** @@ -436,9 +436,9 @@ public static function commit(): void */ public static function rollback(): void { - static::ensureDatabaseConnection(); - - static::$adapter->getConnection()->rollBack(); + if (static::inTransaction()) { + static::$adapter->getConnection()->rollBack(); + } } /** diff --git a/src/Event/EventQueueJob.php b/src/Event/EventQueueTask.php similarity index 85% rename from src/Event/EventQueueJob.php rename to src/Event/EventQueueTask.php index 1b97213c..6335c67d 100644 --- a/src/Event/EventQueueJob.php +++ b/src/Event/EventQueueTask.php @@ -4,12 +4,12 @@ use Bow\Event\Contracts\EventListener; use Bow\Event\Contracts\EventShouldQueue; -use Bow\Queue\QueueJob; +use Bow\Queue\QueueTask; -class EventQueueJob extends QueueJob +class EventQueueTask extends QueueTask { /** - * EventQueueJob constructor + * EventQueueTask constructor * * @param EventListener|EventShouldQueue $event * @param mixed $payload diff --git a/src/Event/Listener.php b/src/Event/Listener.php index dd8d593a..bf9aa729 100644 --- a/src/Event/Listener.php +++ b/src/Event/Listener.php @@ -50,7 +50,7 @@ public function call(array $data = []): mixed $instance = app($callable); if ($instance instanceof EventListener) { if ($instance instanceof EventShouldQueue) { - queue(new EventQueueJob($instance, $data)); + queue(new EventQueueTask($instance, $data)); return null; } $callable = [$instance, 'process']; diff --git a/src/Mail/Mail.php b/src/Mail/Mail.php index e361b8ba..d742d50e 100644 --- a/src/Mail/Mail.php +++ b/src/Mail/Mail.php @@ -169,7 +169,7 @@ public static function queue(string $template, array $data, callable $cb): void call_user_func_array($cb, [$envelop]); - $producer = new MailQueueJob($template, $data, $envelop); + $producer = new MailQueueTask($template, $data, $envelop); queue($producer); } @@ -189,7 +189,7 @@ public static function queueOn(string $queue, string $template, array $data, cal call_user_func_array($cb, [$envelop]); - $producer = new MailQueueJob($template, $data, $envelop); + $producer = new MailQueueTask($template, $data, $envelop); $producer->setQueue($queue); @@ -211,7 +211,7 @@ public static function later(int $delay, string $template, array $data, callable call_user_func_array($cb, [$envelop]); - $producer = new MailQueueJob($template, $data, $envelop); + $producer = new MailQueueTask($template, $data, $envelop); $producer->setDelay($delay); @@ -234,7 +234,7 @@ public static function laterOn(int $delay, string $queue, string $template, arra call_user_func_array($cb, [$envelop]); - $producer = new MailQueueJob($template, $data, $envelop); + $producer = new MailQueueTask($template, $data, $envelop); $producer->setQueue($queue); $producer->setDelay($delay); diff --git a/src/Mail/MailQueueJob.php b/src/Mail/MailQueueTask.php similarity index 91% rename from src/Mail/MailQueueJob.php rename to src/Mail/MailQueueTask.php index 99697161..b6cc2102 100644 --- a/src/Mail/MailQueueJob.php +++ b/src/Mail/MailQueueTask.php @@ -2,11 +2,11 @@ namespace Bow\Mail; -use Bow\Queue\QueueJob; +use Bow\Queue\QueueTask; use Bow\View\View; use Throwable; -class MailQueueJob extends QueueJob +class MailQueueTask extends QueueTask { /** * The message bag @@ -16,7 +16,7 @@ class MailQueueJob extends QueueJob private array $bags = []; /** - * MailQueueJob constructor + * MailQueueTask constructor * * @param string $view * @param array $data diff --git a/src/Notifier/Adapters/SlackChannelAdapter.php b/src/Notifier/Adapters/SlackChannelAdapter.php index 1e87981c..5789d729 100644 --- a/src/Notifier/Adapters/SlackChannelAdapter.php +++ b/src/Notifier/Adapters/SlackChannelAdapter.php @@ -3,10 +3,9 @@ namespace Bow\Notifier\Adapters; use Bow\Database\Barry\Model; +use Bow\Http\Client\HttpClient; use Bow\Notifier\Contracts\ChannelAdapterInterface; use Bow\Notifier\Notifier; -use GuzzleHttp\Client; -use GuzzleHttp\Exception\GuzzleException; class SlackChannelAdapter implements ChannelAdapterInterface { @@ -16,7 +15,7 @@ class SlackChannelAdapter implements ChannelAdapterInterface * @param Model $context * @param Notifier $notifier * @return void - * @throws GuzzleException + * @throws \Exception */ public function send(Model $context, Notifier $notifier): void { @@ -36,15 +35,10 @@ public function send(Model $context, Notifier $notifier): void throw new \InvalidArgumentException('The webhook URL is required for Slack'); } - $client = new Client(); + $client = new HttpClient(); try { - $client->post($webhook_url, [ - 'json' => $data['content'], - 'headers' => [ - 'Content-Type' => 'application/json' - ] - ]); + $client->acceptJson()->post($webhook_url, $data['content']); } catch (\Exception $e) { throw new \RuntimeException('Error while sending Slack notifier: ' . $e->getMessage()); } diff --git a/src/Notifier/Adapters/SmsChannelAdapter.php b/src/Notifier/Adapters/SmsChannelAdapter.php index a129d5b6..db13b996 100644 --- a/src/Notifier/Adapters/SmsChannelAdapter.php +++ b/src/Notifier/Adapters/SmsChannelAdapter.php @@ -39,8 +39,8 @@ class SmsChannelAdapter implements ChannelAdapterInterface public function __construct() { $config = config('notifier.sms'); - $this->setting = $config; - $this->sms_provider = $config['provider'] ?? 'callisto'; + $this->setting = $config ?? []; + $this->sms_provider = $config['provider'] ?? 'log'; } /** @@ -56,6 +56,14 @@ public function send(Model $context, Notifier $notifier): void return; } + if ($this->sms_provider === 'log') { + // Log the SMS content instead of sending + $data = $notifier->toSms($context); + $data['to'] = implode(', ', (array) ($data['to'] ?? [])); + logger()->info('SMS Log - To: ' . ($data['to'] ?? 'N/A') . ' Message: ' . ($data['message'] ?? 'N/A')); + return; + } + if ($this->sms_provider === 'twilio') { $this->sendWithTwilio($context, $notifier); return; @@ -64,7 +72,7 @@ public function send(Model $context, Notifier $notifier): void if ($this->sms_provider === 'callisto') { $this->sendWithCallisto($context, $notifier); return; - }; + } } /** diff --git a/src/Notifier/Adapters/TelegramChannelAdapter.php b/src/Notifier/Adapters/TelegramChannelAdapter.php index 186ab70a..5a6aa3d6 100644 --- a/src/Notifier/Adapters/TelegramChannelAdapter.php +++ b/src/Notifier/Adapters/TelegramChannelAdapter.php @@ -3,11 +3,10 @@ namespace Bow\Notifier\Adapters; use Bow\Database\Barry\Model; +use Bow\Http\Client\HttpClient; use Bow\Notifier\Contracts\ChannelAdapterInterface; use Bow\Notifier\Notifier; use Exception; -use GuzzleHttp\Client; -use GuzzleHttp\Exception\GuzzleException; use InvalidArgumentException; use RuntimeException; @@ -38,7 +37,7 @@ public function __construct() * @param Model $context * @param Notifier $notifier * @return void - * @throws GuzzleException + * @throws Exception */ public function send(Model $context, Notifier $notifier): void { @@ -52,16 +51,14 @@ public function send(Model $context, Notifier $notifier): void throw new InvalidArgumentException('The chat ID and message are required for Telegram'); } - $client = new Client(); + $client = new HttpClient(); $endpoint = "https://api.telegram.org/bot{$this->botToken}/sendMessage"; try { - $client->post($endpoint, [ - 'json' => [ - 'chat_id' => $data['chat_id'], - 'text' => $data['message'], - 'parse_mode' => $data['parse_mode'] ?? 'HTML' - ] + $client->acceptJson()->post($endpoint, [ + 'chat_id' => $data['chat_id'], + 'text' => $data['message'], + 'parse_mode' => $data['parse_mode'] ?? 'HTML' ]); } catch (Exception $e) { throw new RuntimeException('Error while sending Telegram message: ' . $e->getMessage()); diff --git a/src/Notifier/NotifierQueueJob.php b/src/Notifier/NotifierQueueTask.php similarity index 89% rename from src/Notifier/NotifierQueueJob.php rename to src/Notifier/NotifierQueueTask.php index 210f8a30..7cf629d4 100644 --- a/src/Notifier/NotifierQueueJob.php +++ b/src/Notifier/NotifierQueueTask.php @@ -3,10 +3,10 @@ namespace Bow\Notifier; use Bow\Database\Barry\Model; -use Bow\Queue\QueueJob; +use Bow\Queue\QueueTask; use Throwable; -class NotifierQueueJob extends QueueJob +class NotifierQueueTask extends QueueTask { /** * The message bag @@ -16,7 +16,7 @@ class NotifierQueueJob extends QueueJob private array $bags = []; /** - * NotifierQueueJob constructor + * NotifierQueueTask constructor * * @param Model $context * @param Notifier $notifier diff --git a/src/Notifier/NotifierShouldQueue.php b/src/Notifier/NotifierShouldQueue.php new file mode 100644 index 00000000..73788f3d --- /dev/null +++ b/src/Notifier/NotifierShouldQueue.php @@ -0,0 +1,7 @@ +setMessageQueue($notifier); + return; + } + $notifier->process($this); } @@ -23,7 +30,7 @@ public function sendMessage(Notifier $notifier): void */ public function setMessageQueue(Notifier $notifier): void { - $queue_job = new NotifierQueueJob($this, $notifier); + $queue_job = new NotifierQueueTask($this, $notifier); queue($queue_job); } @@ -37,7 +44,7 @@ public function setMessageQueue(Notifier $notifier): void */ public function sendMessageQueueOn(string $queue, Notifier $notifier): void { - $queue_job = new NotifierQueueJob($this, $notifier); + $queue_job = new NotifierQueueTask($this, $notifier); $queue_job->setQueue($queue); @@ -53,7 +60,7 @@ public function sendMessageQueueOn(string $queue, Notifier $notifier): void */ public function sendMessageLater(int $delay, Notifier $notifier): void { - $queue_job = new NotifierQueueJob($this, $notifier); + $queue_job = new NotifierQueueTask($this, $notifier); $queue_job->setDelay($delay); @@ -70,7 +77,7 @@ public function sendMessageLater(int $delay, Notifier $notifier): void */ public function sendMessageLaterOn(int $delay, string $queue, Notifier $notifier): void { - $queue_job = new NotifierQueueJob($this, $notifier); + $queue_job = new NotifierQueueTask($this, $notifier); $queue_job->setQueue($queue); $queue_job->setDelay($delay); diff --git a/src/Queue/Adapters/BeanstalkdAdapter.php b/src/Queue/Adapters/BeanstalkdAdapter.php index 6fb159e7..662be05b 100644 --- a/src/Queue/Adapters/BeanstalkdAdapter.php +++ b/src/Queue/Adapters/BeanstalkdAdapter.php @@ -4,7 +4,7 @@ namespace Bow\Queue\Adapters; -use Bow\Queue\QueueJob; +use Bow\Queue\QueueTask; use Pheanstalk\Contract\PheanstalkPublisherInterface; use Pheanstalk\Pheanstalk; use Pheanstalk\Values\Timeout; @@ -63,11 +63,11 @@ public function size(?string $queue = null): int /** * Queue a job * - * @param QueueJob $producer + * @param QueueTask $producer * @return bool * @throws ErrorException */ - public function push(QueueJob $producer): bool + public function push(QueueTask $producer): bool { $queues = (array) cache("beanstalkd:queues"); @@ -117,11 +117,10 @@ public function run(?string $queue = null): void // we want jobs from define queue only. $queue = $this->getQueue($queue); $this->pheanstalk->watch(new TubeName($queue)); - - // This hangs until a Job is produced. - $job = $this->pheanstalk->reserve(); - + $job = null; try { + // This hangs until a Job is produced. + $job = $this->pheanstalk->reserve(); $payload = $job->getData(); $producer = $this->unserializeProducer($payload); call_user_func([$producer, "process"]); @@ -138,6 +137,10 @@ public function run(?string $queue = null): void // Logger not available, already logged to error_log } + if (!$job) { + return; + } + cache("job:failed:" . $job->getId(), $job->getData()); // Check if producer has been loaded diff --git a/src/Queue/Adapters/DatabaseAdapter.php b/src/Queue/Adapters/DatabaseAdapter.php index 2b71c00f..ffc96cce 100644 --- a/src/Queue/Adapters/DatabaseAdapter.php +++ b/src/Queue/Adapters/DatabaseAdapter.php @@ -5,7 +5,7 @@ use Bow\Database\Database; use Bow\Database\Exception\QueryBuilderException; use Bow\Database\QueryBuilder; -use Bow\Queue\QueueJob; +use Bow\Queue\QueueTask; use ErrorException; use Exception; @@ -48,10 +48,10 @@ public function size(?string $queue = null): int /** * Queue a job * - * @param QueueJob $job + * @param QueueTask $job * @return void */ - public function push(QueueJob $job): bool + public function push(QueueTask $job): bool { $value = [ "id" => $this->generateId(), @@ -98,9 +98,7 @@ public function run(?string $queue = null): void if (!is_null($queue->reserved_at) && strtotime($queue->reserved_at) < time()) { continue; } - $this->table->where("id", $queue->id)->update([ - "status" => "processing", - ]); + $this->table->where("id", $queue->id)->update(["status" => "processing"]); $this->execute($producer, $queue); continue; } @@ -145,16 +143,14 @@ public function run(?string $queue = null): void /** * Process the next job on the queue. * - * @param QueueJob $job + * @param QueueTask $job * @param mixed $queue * @throws QueryBuilderException */ - private function execute(QueueJob $job, mixed $queue): void + private function execute(QueueTask $job, mixed $queue): void { call_user_func([$job, "process"]); - $this->table->where("id", $queue->id)->update([ - "status" => "done" - ]); + $this->table->where("id", $queue->id)->update(["status" => "done"]); $this->sleep($this->sleep ?? 5); } diff --git a/src/Queue/Adapters/QueueAdapter.php b/src/Queue/Adapters/QueueAdapter.php index 3e79c790..6d1447f5 100644 --- a/src/Queue/Adapters/QueueAdapter.php +++ b/src/Queue/Adapters/QueueAdapter.php @@ -4,7 +4,7 @@ namespace Bow\Queue\Adapters; -use Bow\Queue\QueueJob; +use Bow\Queue\QueueTask; abstract class QueueAdapter { @@ -58,18 +58,18 @@ abstract public function configure(array $config): QueueAdapter; /** * Push new job * - * @param QueueJob $job + * @param QueueTask $job * @return bool */ - abstract public function push(QueueJob $job): bool; + abstract public function push(QueueTask $job): bool; /** * Create job serialization * - * @param QueueJob $job + * @param QueueTask $job * @return string */ - public function serializeProducer(QueueJob $job): string + public function serializeProducer(QueueTask $job): string { return serialize($job); } @@ -78,9 +78,9 @@ public function serializeProducer(QueueJob $job): string * Create job unserialize * * @param string $job - * @return QueueJob + * @return QueueTask */ - public function unserializeProducer(string $job): QueueJob + public function unserializeProducer(string $job): QueueTask { return unserialize($job); } @@ -126,8 +126,13 @@ final public function work(int $timeout, int $memory): void } while (true) { - $this->run(); - $jobs_processed++; + try { + $this->updateProcessingTimeout(); + $this->run($this->queue); + } finally { + $this->sleep($this->sleep); + $jobs_processed++; + } if ($this->timeoutReached($timeout)) { $this->kill(static::EXIT_ERROR); diff --git a/src/Queue/Adapters/SQSAdapter.php b/src/Queue/Adapters/SQSAdapter.php index de72c652..d540d509 100644 --- a/src/Queue/Adapters/SQSAdapter.php +++ b/src/Queue/Adapters/SQSAdapter.php @@ -4,7 +4,7 @@ use Aws\Exception\AwsException; use Aws\Sqs\SqsClient; -use Bow\Queue\QueueJob; +use Bow\Queue\QueueTask; use ErrorException; use RuntimeException; @@ -48,10 +48,10 @@ public function configure(array $config): QueueAdapter /** * Push a job onto the queue. * - * @param QueueJob $job + * @param QueueTask $job * @return bool */ - public function push(QueueJob $job): bool + public function push(QueueTask $job): bool { $params = [ 'DelaySeconds' => $job->getDelay(), diff --git a/src/Queue/Adapters/SyncAdapter.php b/src/Queue/Adapters/SyncAdapter.php index 69e78e0f..e4a15a2c 100644 --- a/src/Queue/Adapters/SyncAdapter.php +++ b/src/Queue/Adapters/SyncAdapter.php @@ -4,7 +4,7 @@ namespace Bow\Queue\Adapters; -use Bow\Queue\QueueJob; +use Bow\Queue\QueueTask; class SyncAdapter extends QueueAdapter { @@ -31,13 +31,15 @@ public function configure(array $config): SyncAdapter /** * Queue a job * - * @param QueueJob $job + * @param QueueTask $job * @return bool */ - public function push(QueueJob $job): bool + public function push(QueueTask $job): bool { $job->process(); + $this->sleep($job->getDelay()); + return true; } } diff --git a/src/Queue/QueueJob.php b/src/Queue/QueueTask.php similarity index 90% rename from src/Queue/QueueJob.php rename to src/Queue/QueueTask.php index 68575a5a..19a51b1f 100644 --- a/src/Queue/QueueJob.php +++ b/src/Queue/QueueTask.php @@ -7,7 +7,7 @@ use Bow\Support\Serializes; use Throwable; -abstract class QueueJob +abstract class QueueTask { use Serializes; @@ -40,21 +40,21 @@ abstract class QueueJob protected int $priority = 1; /** - * Determine if the job can be deleted + * Determine if the task can be deleted * * @var bool */ protected bool $delete = false; /** - * Define the job id + * Define the task id * * @return integer */ protected ?string $id = null; /** - * Define the job attempts + * Define the task attempts * * @var int */ @@ -174,27 +174,27 @@ final public function setDelay(int $delay): void } /** - * Delete the job from queue. + * Delete the task from queue. * * @return void */ - public function deleteJob(): void + public function deleteTask(): void { $this->delete = true; } /** - * Delete the job from queue. + * Delete the task from queue. * * @return bool */ - public function jobShouldBeDelete(): bool + public function taskShouldBeDelete(): bool { return $this->delete; } /** - * Get the job error + * Get the task error * * @param Throwable $e * @return void @@ -205,7 +205,7 @@ public function onException(Throwable $e) } /** - * Process the producer + * Process the task * * @return void */ diff --git a/src/Queue/README.md b/src/Queue/README.md index 579b9ca9..eef7679c 100644 --- a/src/Queue/README.md +++ b/src/Queue/README.md @@ -4,9 +4,9 @@ Bow Framework's queue system help you to make a simple and powerful queue/job (c take a low of time. ```php -use App\Queues\EmailQueue; +use App\Queues\EmailQueueTask; -queue(new EmailQueue($email)); +queue(new EmailQueueTask($email)); ``` Launch the worker/consumer. diff --git a/src/Support/helpers.php b/src/Support/helpers.php index b63ad27a..84bbe309 100644 --- a/src/Support/helpers.php +++ b/src/Support/helpers.php @@ -18,7 +18,7 @@ use Bow\Http\Response; use Bow\Mail\Contracts\MailAdapterInterface; use Bow\Mail\Mail; -use Bow\Queue\QueueJob; +use Bow\Queue\QueueTask; use Bow\Security\Crypto; use Bow\Security\Hash; use Bow\Security\Sanitize; @@ -1039,7 +1039,7 @@ function app_storage(string $disk): DiskFilesystemService * @return mixed * @throws ErrorException */ - function cache(?string $key, mixed $value = null, ?int $ttl = null): mixed + function cache(?string $key = null, mixed $value = null, ?int $ttl = null): mixed { $instance = Cache::getInstance(); @@ -1696,9 +1696,9 @@ function is_blank(mixed $value): bool /** * Push the producer on queue * - * @param QueueJob $producer + * @param QueueTask $producer */ - function queue(QueueJob $producer): void + function queue(QueueTask $producer): void { app("queue")->push($producer); } diff --git a/tests/Console/GeneratorDeepTest.php b/tests/Console/GeneratorDeepTest.php index 7440775d..36b219a7 100644 --- a/tests/Console/GeneratorDeepTest.php +++ b/tests/Console/GeneratorDeepTest.php @@ -99,19 +99,19 @@ public function test_generate_middleware_stubs() $this->assertMatchesRegularExpression("@\nclass\sFakeMiddleware\simplements\sBaseMiddleware\n@", $content); } - public function test_generate_job_stubs() + public function test_generate_task_stubs() { - $generator = new Generator(TESTING_RESOURCE_BASE_DIRECTORY, 'FakeJob'); - $content = $generator->makeStubContent('job', [ + $generator = new Generator(TESTING_RESOURCE_BASE_DIRECTORY, 'FakeTask'); + $content = $generator->makeStubContent('task', [ "namespace" => "", - "className" => "FakeJob", - "baseNamespace" => "App\Jobs", + "className" => "FakeTask", + "baseNamespace" => "App\Tasks", ]); $this->assertNotNull($content); $this->assertMatchesSnapshot($content); - $this->assertMatchesRegularExpression("@\nnamespace\sApp\\\Jobs;\n@", $content); - $this->assertMatchesRegularExpression("@\nclass\sFakeJob\sextends\sQueueJob\n@", $content); + $this->assertMatchesRegularExpression("@\nnamespace\sApp\\\Tasks;\n@", $content); + $this->assertMatchesRegularExpression("@\nclass\sFakeTask\sextends\sQueueTask\n@", $content); } public function test_generate_seeder_stubs() diff --git a/tests/Console/SettingTest.php b/tests/Console/SettingTest.php index 2a4adca0..4b2e8990 100644 --- a/tests/Console/SettingTest.php +++ b/tests/Console/SettingTest.php @@ -56,7 +56,7 @@ public function get_the_directories() ["service", "/app/Services"], ["Event", "/app/Events"], ["EventListener", "/app/Listeners"], - ["job", "/app/Jobs"], + ["task", "/app/Tasks"], ["command", "/app/Commands"], ["seeder", "/seeders"], ["component", "/frontend"], diff --git a/tests/Console/__snapshots__/GeneratorDeepTest__test_generate_job_stubs__1.txt b/tests/Console/__snapshots__/GeneratorDeepTest__test_generate_job_stubs__1.txt index 28356765..c76a3282 100644 --- a/tests/Console/__snapshots__/GeneratorDeepTest__test_generate_job_stubs__1.txt +++ b/tests/Console/__snapshots__/GeneratorDeepTest__test_generate_job_stubs__1.txt @@ -1,10 +1,10 @@ setConnection("beanstalkd")->getAdapter(); - $producer = new EventQueueJob(new UserEventListenerStub(), new UserEventStub("bowphp")); + $producer = new EventQueueTask(new UserEventListenerStub(), new UserEventStub("bowphp")); $cache_filename = TESTING_RESOURCE_BASE_DIRECTORY . '/event.txt'; // Clean up any existing file before test @unlink($cache_filename); - $this->assertInstanceOf(EventQueueJob::class, $producer); + $this->assertInstanceOf(EventQueueTask::class, $producer); try { $result = $adapter->push($producer); @@ -69,15 +69,15 @@ public function test_should_create_event_queue_job_with_listener_and_payload(): $listener = new UserEventListenerStub(); $event = new UserEventStub("test-data"); - $producer = new EventQueueJob($listener, $event); + $producer = new EventQueueTask($listener, $event); - $this->assertInstanceOf(EventQueueJob::class, $producer); + $this->assertInstanceOf(EventQueueTask::class, $producer); } public function test_should_process_event_from_queue(): void { $adapter = static::$connection->setConnection("sync")->getAdapter(); - $producer = new EventQueueJob(new UserEventListenerStub(), new UserEventStub("sync-test")); + $producer = new EventQueueTask(new UserEventListenerStub(), new UserEventStub("sync-test")); $cache_filename = TESTING_RESOURCE_BASE_DIRECTORY . '/event.txt'; $adapter->push($producer); diff --git a/tests/Queue/MailQueueTest.php b/tests/Queue/MailQueueTest.php index 8d01af00..c9aaa8bd 100644 --- a/tests/Queue/MailQueueTest.php +++ b/tests/Queue/MailQueueTest.php @@ -8,7 +8,7 @@ use Bow\Database\DatabaseConfiguration; use Bow\Mail\Envelop; use Bow\Mail\MailConfiguration; -use Bow\Mail\MailQueueJob; +use Bow\Mail\MailQueueTask; use Bow\Queue\Connection as QueueConnection; use Bow\Queue\QueueConfiguration; use Bow\Tests\Config\TestingConfiguration; @@ -44,9 +44,9 @@ public function it_should_queue_mail_successfully(): void $envelop = new Envelop(); $envelop->to("bow@bow.org"); $envelop->subject("hello from bow"); - $producer = new MailQueueJob("email", [], $envelop); + $producer = new MailQueueTask("email", [], $envelop); - $this->assertInstanceOf(MailQueueJob::class, $producer); + $this->assertInstanceOf(MailQueueTask::class, $producer); $adapter = static::$connection->setConnection("beanstalkd")->getAdapter(); @@ -67,9 +67,9 @@ public function it_should_create_mail_producer_with_correct_parameters(): void $envelop->from("sender@example.com"); $envelop->subject("Test Subject"); - $producer = new MailQueueJob("test-template", ["name" => "John"], $envelop); + $producer = new MailQueueTask("test-template", ["name" => "John"], $envelop); - $this->assertInstanceOf(MailQueueJob::class, $producer); + $this->assertInstanceOf(MailQueueTask::class, $producer); } /** @@ -80,7 +80,7 @@ public function it_should_push_mail_to_specific_queue(): void $envelop = new Envelop(); $envelop->to("priority@example.com"); $envelop->subject("Priority Mail"); - $producer = new MailQueueJob("email", [], $envelop); + $producer = new MailQueueTask("email", [], $envelop); $adapter = static::$connection->setConnection("beanstalkd")->getAdapter(); $adapter->setQueue("priority-mail"); @@ -98,7 +98,7 @@ public function it_should_set_mail_retry_attempts(): void $envelop->to("retry@example.com"); $envelop->subject("Retry Test"); - $producer = new MailQueueJob("email", [], $envelop); + $producer = new MailQueueTask("email", [], $envelop); $producer->setRetry(3); $this->assertEquals(3, $producer->getRetry()); diff --git a/tests/Queue/NotifierQueueTest.php b/tests/Queue/NotifierQueueTest.php index 281fa7ab..d8b989ed 100644 --- a/tests/Queue/NotifierQueueTest.php +++ b/tests/Queue/NotifierQueueTest.php @@ -7,7 +7,7 @@ use Bow\Configuration\LoggerConfiguration; use Bow\Database\DatabaseConfiguration; use Bow\Mail\MailConfiguration; -use Bow\Notifier\NotifierQueueJob; +use Bow\Notifier\NotifierQueueTask; use Bow\Queue\Connection as QueueConnection; use Bow\Queue\QueueConfiguration; use Bow\Tests\Config\TestingConfiguration; @@ -58,10 +58,10 @@ public function test_can_send_message_to_queue(): void $context = new TestNotifiableModel(); $message = new TestNotifier(); - $producer = new NotifierQueueJob($context, $message); + $producer = new NotifierQueueTask($context, $message); // Verify that the producer is created with correct parameters - $this->assertInstanceOf(NotifierQueueJob::class, $producer); + $this->assertInstanceOf(NotifierQueueTask::class, $producer); // Push to queue and verify $result = static::$connection->setConnection("beanstalkd")->getAdapter()->push($producer); @@ -74,10 +74,10 @@ public function test_can_send_message_to_specific_queue(): void $context = new TestNotifiableModel(); $message = new TestNotifier(); - $producer = new NotifierQueueJob($context, $message); + $producer = new NotifierQueueTask($context, $message); // Verify that the producer is created with correct parameters - $this->assertInstanceOf(NotifierQueueJob::class, $producer); + $this->assertInstanceOf(NotifierQueueTask::class, $producer); // Push to specific queue and verify $adapter = static::$connection->setConnection("beanstalkd")->getAdapter(); @@ -93,10 +93,10 @@ public function test_can_send_message_with_delay(): void $context = new TestNotifiableModel(); $message = new TestNotifier(); - $producer = new NotifierQueueJob($context, $message); + $producer = new NotifierQueueTask($context, $message); // Verify that the producer is created with correct parameters - $this->assertInstanceOf(NotifierQueueJob::class, $producer); + $this->assertInstanceOf(NotifierQueueTask::class, $producer); // Push to queue with delay and verify $adapter = static::$connection->setConnection("beanstalkd")->getAdapter(); @@ -113,10 +113,10 @@ public function test_can_send_message_with_delay_on_specific_queue(): void $context = new TestNotifiableModel(); $message = new TestNotifier(); - $producer = new NotifierQueueJob($context, $message); + $producer = new NotifierQueueTask($context, $message); // Verify that the producer is created with correct parameters - $this->assertInstanceOf(NotifierQueueJob::class, $producer); + $this->assertInstanceOf(NotifierQueueTask::class, $producer); // Push to specific queue with delay and verify $adapter = static::$connection->setConnection("beanstalkd")->getAdapter(); diff --git a/tests/Queue/QueueTest.php b/tests/Queue/QueueTest.php index 8a2b938a..4912796f 100644 --- a/tests/Queue/QueueTest.php +++ b/tests/Queue/QueueTest.php @@ -15,8 +15,8 @@ use Bow\Queue\Adapters\SyncAdapter; use Bow\Queue\Connection as QueueConnection; use Bow\Tests\Config\TestingConfiguration; -use Bow\Tests\Queue\Stubs\BasicQueueJobStubs; -use Bow\Tests\Queue\Stubs\ModelJobStub; +use Bow\Tests\Queue\Stubs\BasicQueueTaskStub; +use Bow\Tests\Queue\Stubs\ModelQueueTaskStub; use Bow\Tests\Queue\Stubs\PetModelStub; use Bow\View\View; use PHPUnit\Framework\TestCase; @@ -78,18 +78,18 @@ private function getAdapter(string $connection) /** * Create and return a basic job producer */ - private function createBasicJob(string $connection): BasicQueueJobStubs + private function createBasicJob(string $connection): BasicQueueTaskStub { - return new BasicQueueJobStubs($connection); + return new BasicQueueTaskStub($connection); } /** * Create and return a model-based job producer */ - private function createModelJob(string $connection, string $petName = "Filou"): ModelJobStub + private function createModelJob(string $connection, string $petName = "Filou"): ModelQueueTaskStub { $pet = new PetModelStub(["name" => $petName]); - return new ModelJobStub($pet, $connection); + return new ModelQueueTaskStub($pet, $connection); } /** @@ -221,7 +221,7 @@ public function test_push_service_adapter(string $connection): void $this->cleanupFiles([$filename]); $producer = $this->createBasicJob($connection); - $this->assertInstanceOf(BasicQueueJobStubs::class, $producer); + $this->assertInstanceOf(BasicQueueTaskStub::class, $producer); try { $result = $adapter->push($producer); @@ -233,7 +233,7 @@ public function test_push_service_adapter(string $connection): void $adapter->run(); $this->assertFileExists($filename, "Producer file was not created for {$connection}"); - $this->assertEquals(BasicQueueJobStubs::class, file_get_contents($filename)); + $this->assertEquals(BasicQueueTaskStub::class, file_get_contents($filename)); } catch (\Exception $e) { if ($connection === 'beanstalkd') { $this->markTestSkipped('Beanstalkd service is not available: ' . $e->getMessage()); @@ -266,7 +266,7 @@ public function test_push_service_adapter_with_model(string $connection): void $this->cleanupFiles([$filename, $producerFile]); $producer = $this->createModelJob($connection, "Filou"); - $this->assertInstanceOf(ModelJobStub::class, $producer); + $this->assertInstanceOf(ModelQueueTaskStub::class, $producer); try { $result = $adapter->push($producer); @@ -308,13 +308,13 @@ public function test_push_service_adapter_with_model(string $connection): void public function test_job_can_be_created_with_connection_parameter(): void { $job = $this->createBasicJob("test-connection"); - $this->assertInstanceOf(BasicQueueJobStubs::class, $job); + $this->assertInstanceOf(BasicQueueTaskStub::class, $job); } public function test_model_job_can_be_created_with_pet_instance(): void { $job = $this->createModelJob("test", "TestPet"); - $this->assertInstanceOf(ModelJobStub::class, $job); + $this->assertInstanceOf(ModelQueueTaskStub::class, $job); } public function test_can_push_job_to_specific_queue(): void @@ -345,7 +345,7 @@ public function test_job_execution_creates_expected_output(): void $adapter->push($producer); $content = file_get_contents($filename); - $this->assertEquals(BasicQueueJobStubs::class, $content); + $this->assertEquals(BasicQueueTaskStub::class, $content); $this->cleanupFiles([$filename]); } @@ -495,7 +495,7 @@ public function test_beanstalkd_adapter_can_process_queued_jobs(): void $adapter->run(); $this->assertFileExists($filename); - $this->assertEquals(BasicQueueJobStubs::class, file_get_contents($filename)); + $this->assertEquals(BasicQueueTaskStub::class, file_get_contents($filename)); } catch (\Exception $e) { $this->markTestSkipped('Beanstalkd service is not available: ' . $e->getMessage()); } finally { @@ -608,7 +608,7 @@ public function test_sync_adapter_processes_immediately(): void $this->assertTrue($result); $this->assertFileExists($filename); - $this->assertEquals(BasicQueueJobStubs::class, file_get_contents($filename)); + $this->assertEquals(BasicQueueTaskStub::class, file_get_contents($filename)); $this->cleanupFiles([$filename]); } diff --git a/tests/Queue/Stubs/BasicQueueJobStubs.php b/tests/Queue/Stubs/BasicQueueTaskStub.php similarity index 65% rename from tests/Queue/Stubs/BasicQueueJobStubs.php rename to tests/Queue/Stubs/BasicQueueTaskStub.php index d7c09e06..0d8f5356 100644 --- a/tests/Queue/Stubs/BasicQueueJobStubs.php +++ b/tests/Queue/Stubs/BasicQueueTaskStub.php @@ -2,9 +2,9 @@ namespace Bow\Tests\Queue\Stubs; -use Bow\Queue\QueueJob; +use Bow\Queue\QueueTask; -class BasicQueueJobStubs extends QueueJob +class BasicQueueTaskStub extends QueueTask { public function __construct( private string $connection @@ -13,6 +13,6 @@ public function __construct( public function process(): void { - file_put_contents(TESTING_RESOURCE_BASE_DIRECTORY . "/{$this->connection}_producer.txt", BasicQueueJobStubs::class); + file_put_contents(TESTING_RESOURCE_BASE_DIRECTORY . "/{$this->connection}_producer.txt", BasicQueueTaskStub::class); } } diff --git a/tests/Queue/Stubs/MixedQueueJobStub.php b/tests/Queue/Stubs/MixedQueueTaskStub.php similarity index 79% rename from tests/Queue/Stubs/MixedQueueJobStub.php rename to tests/Queue/Stubs/MixedQueueTaskStub.php index fbf31945..14df8441 100644 --- a/tests/Queue/Stubs/MixedQueueJobStub.php +++ b/tests/Queue/Stubs/MixedQueueTaskStub.php @@ -2,9 +2,9 @@ namespace Bow\Tests\Queue\Stubs; -use Bow\Queue\QueueJob; +use Bow\Queue\QueueTask; -class MixedQueueJobStub extends QueueJob +class MixedQueueTaskStub extends QueueTask { public function __construct( private ServiceStub $service, diff --git a/tests/Queue/Stubs/ModelJobStub.php b/tests/Queue/Stubs/ModelJobStub.php index 1452655f..0ad9e9d3 100644 --- a/tests/Queue/Stubs/ModelJobStub.php +++ b/tests/Queue/Stubs/ModelJobStub.php @@ -2,9 +2,9 @@ namespace Bow\Tests\Queue\Stubs; -use Bow\Queue\QueueJob; +use Bow\Queue\QueueTask; -class ModelJobStub extends QueueJob +class ModelQueueTaskStub extends QueueTask { public function __construct( private PetModelStub $pet,