Execute actions both sync and async
In laravel 5.7 I want to be able to have multiple actions,
for example inserting user in database, sending registration Email,
sending notification, ...
I want to be able to execute these actions both sync and async.
the problem is I don't want to create Job class for every action.
each action is a php callable class.
The thing I don't understand in Laravel Job class is It receives dependencies as handle method arguments and receive It's Input which should process on,in the constructor, I think It's kind of odd.
for example when I want to call send register email action, I want to be able to do sth like :
$registerEmailAction->__invoke($user, true);
second parameter indicates whether to do this action sync or async.
php laravel asynchronous
add a comment |
In laravel 5.7 I want to be able to have multiple actions,
for example inserting user in database, sending registration Email,
sending notification, ...
I want to be able to execute these actions both sync and async.
the problem is I don't want to create Job class for every action.
each action is a php callable class.
The thing I don't understand in Laravel Job class is It receives dependencies as handle method arguments and receive It's Input which should process on,in the constructor, I think It's kind of odd.
for example when I want to call send register email action, I want to be able to do sth like :
$registerEmailAction->__invoke($user, true);
second parameter indicates whether to do this action sync or async.
php laravel asynchronous
I don't understand the question very well. How are you handling async jobs? Why not use Queues for this?
– Mozammil
Dec 30 '18 at 17:50
I don't mind using queues, but as I said I have many (40) php callable class which I call them my actions, and what I need, is to be able to call these actions synchronously or asynchronous, and I don't want create 40 new classes just to be able to handle async in my code base.
– mhndev
Dec 30 '18 at 20:41
add a comment |
In laravel 5.7 I want to be able to have multiple actions,
for example inserting user in database, sending registration Email,
sending notification, ...
I want to be able to execute these actions both sync and async.
the problem is I don't want to create Job class for every action.
each action is a php callable class.
The thing I don't understand in Laravel Job class is It receives dependencies as handle method arguments and receive It's Input which should process on,in the constructor, I think It's kind of odd.
for example when I want to call send register email action, I want to be able to do sth like :
$registerEmailAction->__invoke($user, true);
second parameter indicates whether to do this action sync or async.
php laravel asynchronous
In laravel 5.7 I want to be able to have multiple actions,
for example inserting user in database, sending registration Email,
sending notification, ...
I want to be able to execute these actions both sync and async.
the problem is I don't want to create Job class for every action.
each action is a php callable class.
The thing I don't understand in Laravel Job class is It receives dependencies as handle method arguments and receive It's Input which should process on,in the constructor, I think It's kind of odd.
for example when I want to call send register email action, I want to be able to do sth like :
$registerEmailAction->__invoke($user, true);
second parameter indicates whether to do this action sync or async.
php laravel asynchronous
php laravel asynchronous
asked Dec 30 '18 at 17:05
mhndevmhndev
6451024
6451024
I don't understand the question very well. How are you handling async jobs? Why not use Queues for this?
– Mozammil
Dec 30 '18 at 17:50
I don't mind using queues, but as I said I have many (40) php callable class which I call them my actions, and what I need, is to be able to call these actions synchronously or asynchronous, and I don't want create 40 new classes just to be able to handle async in my code base.
– mhndev
Dec 30 '18 at 20:41
add a comment |
I don't understand the question very well. How are you handling async jobs? Why not use Queues for this?
– Mozammil
Dec 30 '18 at 17:50
I don't mind using queues, but as I said I have many (40) php callable class which I call them my actions, and what I need, is to be able to call these actions synchronously or asynchronous, and I don't want create 40 new classes just to be able to handle async in my code base.
– mhndev
Dec 30 '18 at 20:41
I don't understand the question very well. How are you handling async jobs? Why not use Queues for this?
– Mozammil
Dec 30 '18 at 17:50
I don't understand the question very well. How are you handling async jobs? Why not use Queues for this?
– Mozammil
Dec 30 '18 at 17:50
I don't mind using queues, but as I said I have many (40) php callable class which I call them my actions, and what I need, is to be able to call these actions synchronously or asynchronous, and I don't want create 40 new classes just to be able to handle async in my code base.
– mhndev
Dec 30 '18 at 20:41
I don't mind using queues, but as I said I have many (40) php callable class which I call them my actions, and what I need, is to be able to call these actions synchronously or asynchronous, and I don't want create 40 new classes just to be able to handle async in my code base.
– mhndev
Dec 30 '18 at 20:41
add a comment |
1 Answer
1
active
oldest
votes
Laravel Job classes are simple objects that only need to implement the ShouldQueue
interface and an handle()
method. You can dispatch them, or run them immediately explicitly calling the handle method. If you want to take the __invoke
route you could so something like this:
class RegisterEmailAction implements ShouldQueue
{
//... more code ...
public function __invoke(User $user, bool $async)
{
$this->setUser($user);
if ($async) {
dispatch($this);
}
else {
$this->handle(); // or dispatch_now($this);
}
}
public function handle()
{
if (!$this->user) {
throw new UserNotFoundException();
}
// ... some other code ...
}
}
Since you don't want to pass the $user as a dependency in the constructor, I would suggest to check for it in the handle method, so that you get an error if some client code tries to call the handle method, without taking the __invoke
route. You also may needs to use some traits, like SerializeeModels
or Dispatchable
(check the docs for more info).
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53979687%2fexecute-actions-both-sync-and-async%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Laravel Job classes are simple objects that only need to implement the ShouldQueue
interface and an handle()
method. You can dispatch them, or run them immediately explicitly calling the handle method. If you want to take the __invoke
route you could so something like this:
class RegisterEmailAction implements ShouldQueue
{
//... more code ...
public function __invoke(User $user, bool $async)
{
$this->setUser($user);
if ($async) {
dispatch($this);
}
else {
$this->handle(); // or dispatch_now($this);
}
}
public function handle()
{
if (!$this->user) {
throw new UserNotFoundException();
}
// ... some other code ...
}
}
Since you don't want to pass the $user as a dependency in the constructor, I would suggest to check for it in the handle method, so that you get an error if some client code tries to call the handle method, without taking the __invoke
route. You also may needs to use some traits, like SerializeeModels
or Dispatchable
(check the docs for more info).
add a comment |
Laravel Job classes are simple objects that only need to implement the ShouldQueue
interface and an handle()
method. You can dispatch them, or run them immediately explicitly calling the handle method. If you want to take the __invoke
route you could so something like this:
class RegisterEmailAction implements ShouldQueue
{
//... more code ...
public function __invoke(User $user, bool $async)
{
$this->setUser($user);
if ($async) {
dispatch($this);
}
else {
$this->handle(); // or dispatch_now($this);
}
}
public function handle()
{
if (!$this->user) {
throw new UserNotFoundException();
}
// ... some other code ...
}
}
Since you don't want to pass the $user as a dependency in the constructor, I would suggest to check for it in the handle method, so that you get an error if some client code tries to call the handle method, without taking the __invoke
route. You also may needs to use some traits, like SerializeeModels
or Dispatchable
(check the docs for more info).
add a comment |
Laravel Job classes are simple objects that only need to implement the ShouldQueue
interface and an handle()
method. You can dispatch them, or run them immediately explicitly calling the handle method. If you want to take the __invoke
route you could so something like this:
class RegisterEmailAction implements ShouldQueue
{
//... more code ...
public function __invoke(User $user, bool $async)
{
$this->setUser($user);
if ($async) {
dispatch($this);
}
else {
$this->handle(); // or dispatch_now($this);
}
}
public function handle()
{
if (!$this->user) {
throw new UserNotFoundException();
}
// ... some other code ...
}
}
Since you don't want to pass the $user as a dependency in the constructor, I would suggest to check for it in the handle method, so that you get an error if some client code tries to call the handle method, without taking the __invoke
route. You also may needs to use some traits, like SerializeeModels
or Dispatchable
(check the docs for more info).
Laravel Job classes are simple objects that only need to implement the ShouldQueue
interface and an handle()
method. You can dispatch them, or run them immediately explicitly calling the handle method. If you want to take the __invoke
route you could so something like this:
class RegisterEmailAction implements ShouldQueue
{
//... more code ...
public function __invoke(User $user, bool $async)
{
$this->setUser($user);
if ($async) {
dispatch($this);
}
else {
$this->handle(); // or dispatch_now($this);
}
}
public function handle()
{
if (!$this->user) {
throw new UserNotFoundException();
}
// ... some other code ...
}
}
Since you don't want to pass the $user as a dependency in the constructor, I would suggest to check for it in the handle method, so that you get an error if some client code tries to call the handle method, without taking the __invoke
route. You also may needs to use some traits, like SerializeeModels
or Dispatchable
(check the docs for more info).
answered Dec 31 '18 at 9:44
geregere
44347
44347
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53979687%2fexecute-actions-both-sync-and-async%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I don't understand the question very well. How are you handling async jobs? Why not use Queues for this?
– Mozammil
Dec 30 '18 at 17:50
I don't mind using queues, but as I said I have many (40) php callable class which I call them my actions, and what I need, is to be able to call these actions synchronously or asynchronous, and I don't want create 40 new classes just to be able to handle async in my code base.
– mhndev
Dec 30 '18 at 20:41