我喜欢在服务类app(MailService::class);
中编写繁重的逻辑,但是从Service类 - 如何回复Job Class来执行$this->release()
或尝试检查$this->attempts()
?
类似地,您如何回复命令(SendReminderCommand
)传递给$this->error()
或$this->info()
- 这对于Controller也应该是灵活的。
我想使用Service Class是灵活的,因此它可以与Job Class,Command甚至Controller一起使用。
例如:
职业分类
class SendReminderEmail extends Job implements SelfHandling, ShouldQueue
{
use InteractsWithQueue, SerializesModels;
public function handle()
{
$mail = app(MailService::class); //service class
$this->release(10)
$this->attempts()
}
}
命令类
class SendReminderCommand extends Command
{
protected $signature = 'email:reminder';
protected $description = 'Send Reminder';
public function handle()
{
$mail = app(MailService::class); //service class
$this->info("Emails Sent");
$this->error('Something went wrong!');
}
}
答案 0 :(得分:1)
您可以构建服务方法,以便在没有抛出异常的情况下,可以假定非查询方法已成功:
public function handle(MailService $mail)
{
try {
$mail->performAction();
$this->info('Action Complete');
} catch (RecoverableProblem $e) {
/* Recover and perhaps retry at a later time? */
} catch (UnrecoverableProblem $e) {
$this->error('Something went wrong!');
/* Abort? */
}
}
如果您需要将一些其他信息传递给日志记录方法,只需从服务方法返回该信息并使用返回值来构建消息。