laravel-console-dusk icon indicating copy to clipboard operation
laravel-console-dusk copied to clipboard

[1.1] Usage outside of Artisan commands

Open necenzurat opened this issue 6 years ago • 12 comments

Any chance for this to be implemented with queues, that would be awesome.

necenzurat avatar Jun 08 '18 00:06 necenzurat

@necenzurat Can you give me an example? Queuing a command?

nunomaduro avatar Jun 08 '18 06:06 nunomaduro

nope, when you dispatch a job, the job fires up a dusk instance, just like the command but with queues. Could that be posible?

necenzurat avatar Jun 09 '18 11:06 necenzurat

I think so. I will do a test locally tho.

nunomaduro avatar Jun 09 '18 22:06 nunomaduro

@necenzurat Sure is possible. You have two options:

  1. You can call your artisan command from your job using Artisan::call($commandName);

  2. You can place the code as the following example in your job:

resolve(ManagerContract::class)->browse($this, function ($browser) {
    $browser->visit('http://laravel-zero.com')
        ->assertSee('Collision');
});

nunomaduro avatar Jun 13 '18 16:06 nunomaduro

I need to import the ManagerContract, where is that?

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class Q implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        resolve(\ManagerContract::class)->browse($this, function ($browser) {
            $browser->visit('https://necesrequests.herokuapp.com/1eyyti51');
        });
    }
}

necenzurat avatar Jun 13 '18 16:06 necenzurat

Found it, but: image

necenzurat avatar Jun 13 '18 16:06 necenzurat

@necenzurat Hummmmmm. I need to take a deep look into this.

The first option is not good?

nunomaduro avatar Jun 13 '18 16:06 nunomaduro

Nope, i would like to have the possibility to queue some jobs then the workers would start the chrome browsers in parallel for scraping/other purposes. The command method is slow if you wan't to check many links at the same time. 🐌

necenzurat avatar Jun 13 '18 16:06 necenzurat

@necenzurat I understand. We should definitely find a way for making things easy while using Laravel Jobs.

nunomaduro avatar Jun 19 '18 20:06 nunomaduro

I mean, it would be super cool, but no worries, no one is in a hurry. Let me know if i can help. Ps: would be nice in a separate package like the awesome laravel-console-dusk.

On Tue, 19 Jun 2018 at 23:21, Nuno Maduro [email protected] wrote:

@necenzurat https://github.com/necenzurat I understand. We should definitely find a way for making things easy while using Laravel Jobs.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/nunomaduro/laravel-console-dusk/issues/4#issuecomment-398532288, or mute the thread https://github.com/notifications/unsubscribe-auth/AAI4KfQQ7PKhjvbzyu_B-kXc48y2nqWKks5t-V1CgaJpZM4UfTou .

--

-- Costin Moise aka necenzurat

Tel: +4 0726.459.188 | Github https://github.com/necenzurat | Linkedin http://www.linkedin.com/in/necenzurat

necenzurat avatar Jun 19 '18 20:06 necenzurat

Would be good to have: Provide an easy way of using this package outside artisan commands.

nunomaduro avatar Jun 19 '18 20:06 nunomaduro

Hey,

I'm taking a look at it, and maybe something like this helps:

  • Extend your job with Illuminate\Console\Command:
...
use Illuminate\Console\Command;

class bot extends Command implements ShouldQueue {
    ...
}
  • Then you will have a problem of $this->output being null on your LaravelConsoleTaskServiceProvider. For a temporary fix, you can add something like this in the beginning of the task macro function, line 43, by adding:
...
Command::macro(
    'task',
    function (string $title, $task = null, $loadingText = 'loading...') {
        $this->output = optional($this->output); // Add this line

        $this->output->write("$title: <comment>{$loadingText}</comment>");
...

@nunomaduro, @owenvoke it would be nice if you can take a look at this, so we can find a solution, as it is the same issue as #17.

manelgavalda avatar Feb 24 '20 12:02 manelgavalda