Background backend task

I use:

Basically, I have a function which sends ~1000 emails and I would like to make it as background process.

My yii2 set-up is the following:

Front-end is on "/" and back-end is under "/admin".

The questions is the following: I do not understand how to say which controller I would like to use? Does it determine by default which controller it is going to use?




$output = '';

Yii::$app->consoleRunner->run('controller/action', $output);

echo $output; //prints the command output



When I try to execute code above and var_dump() the result of


$output

then it returns the following:


Could not open input file: /home/admin/web/domain.com/public_html/backend/yii

and emails do not get sent (tried to execute controller/action link by opening it it worked, meaning that action itself works correctly).

Why don’t you use a console controller and invoke commands from console (so you can use it in crontab).

To make functions reusable, you can create a component to call from web controller or console controller (if you need to use functionalities from web).

Why would I build a new one, If I can use an existing one and change it to my needs?

Any suggestions on the problem that I have?

Because more in general you can’t solve every needs using consoleRunner.

For example, if the action processes $_POST data, how would you pass $_POST parameters applying consoleRunner?

So I think that if doesn’t require too much work, It should be better put action content in a component that could be called from a console controller and web controller.

Anyway, for your request how have you built consoleRunner object? How have you defined ‘file’ parameter?

It should be similar at:




components [

    'consoleRunner' => [

        'class' => 'vova07\console\ConsoleRunner',

        'file' => '@my/path/to/yii' // or an absolute path to console file

    ]

]



Taken from https://github.com/vova07/yii2-console-runner-extension

I want the following:

If POST request is coming from the form on controller "add/signal", I would like to send POST request to "signal\email" which is sending emails ~1000 emails to people, however I would like to make "signal\email" as a background process, otherwise, it will take forever for a person to wait for the reply.

Btw, console runner does not for some reason. I have tried vova’s class as well. Also, I would like to execute controller from backend, not frontend.