How to log php artisan calls? - php

How do can I log every artisan calls?
Ex:
Logging all the php artisan serve, migration and seeds
and save it to a database?

I have no idea why you need it but here is it. The easiest way (but not the best) - is to modify "artisan.php" with simple code like this:
ArtisanModel::create(['command' => $_SERVER['argv'][1]];
just after $kernel var
It mean you have to create new Model and store $_SERVER['argv'] result in it.
Another way (the better one) is determine is your application running in CLI mode somewhere in ServiceProvider

Related

Multi threading in Laravel 9

I've a code base written in Laravel 9 which targets to execute multiple requests to one API with 1000 different API keys + API secrets. Is there a way to actually do this simmultaniously and not one after another ?
I've come up to this thread Laravel Commands, Pthreads and Closure but the answer there doesn't look like it is wokring, in my opinion it shows that everything is kinda executing at the same time because there is sleep(rand(1,3)) inside the OperatorThreaded class.
Any idea ?
A Laravelly way is to do it like so:
Create a RequestYourApi Job (php artisan make:job RequestYourApi or make a class yourself inside App\Jobs).
Dispatch any amount of jobs using RequestYourApi::dispatch([/* args */]) (the arguments will be passed to the constructor of this class, and the job will be put into the queue, see https://laravel.com/docs/9.x/queues)
Run multiple workers using for instance Supervisor (or Laravel Horizon): php artisan queue:work (use --help to see additional options)
Scale up/down your workers depending on how fast you need to process things.

run specific migration using artisan command from class

I tried to run a specific migration table from the controller using the following code
Artisan::call ("migrate:refresh --step=14");
but it does not refresh table 14, on the other hand it refreshes all the tables
.. ,
any suggestion !!
That is the purpose of migrate:refresh. It rolls back the migrations and then runs them:
The migrate:refresh command will roll back all of your migrations and
then execute the migrate command. This command effectively re-creates
your entire database
You may roll back and re-migrate a limited number of migrations by
providing the step option to the refresh command. For example, the
following command will roll back and re-migrate the last five
migrations
If you want to run run a specific migration the command would be:
migrate --path=/database/migrations/my_migration.php
This does however not sound like the best way to achieve whatever you are trying to achieve. Without further information it's hard to suggest an alternative plan for how to achieve this.
Edit: If you are trying to insert pre-defined data into a table you should look into using Database Seeders which sound much more to be what you are looking for.

Laravel Dusk - Run tests & get results via code

I need to make a tool to Browser test my production system. I have read up all about Laravel Dusk and it seems like a perfect tool. However, I need to run tests automatically via schedule and have a dashboard with the results.
I can easily run the command php artisan dusk from the code using the Scheduler, however, how can I get the results? Is there a better option than simply parsing the Console Output from that command? Ideally I would have a way of getting the status of each test (whether it passed or failed) to be able to log, process and display all that information.
The Dusk documentation hasn't got any more information on running the tests programatically, it only has instructions to run via php artisan dusk.
Has anyone encountered this?
Thank you!
The way I have achieved what I needed is to use the command options for dusk/phpunit.
I used --filter=MyTestClass to single out which test I wanted to run, and --log-junit log.log to log the results for that test, which I then parsed via code as well to fetch the results. This allowed me to build a fully custom dashboard that was able to run each test individually, report the results, send notifications etc.
Not the prettiest solution, but it worked well for what I needed. If anyone encounters better way to achieve that (or just use Dusk in general as a browser/scraper outside phpunit) please do post a comment/answer!

Accessing symfony 1.4 URL using CLI Terminal in Ubuntu

Can anyone tell me, how to access an action URL of symfony using terminal in ubuntu?
For Example:
http://mysymfonyproject.com/index.php/users/sendnewsletter
Same way, I want to set up an action class URL as cron job. Is it possible to set up cron jobs in symfony?
For Example:
http://mysymfonyproject.com/index.php/crons/sendnewsletter
I think a Symfony Task is what you should look into. There's a lot of built-in tasks, but you probably should write your own task and then call it from cli using a command:
php symfony yourTaskNamespace:yourTaskName
Then you can add such command as a regular cronjob task.
You can check detailed instruction how to create custom Symfony tasks at Documentation Page
Using tasks is the best solution (as Tomasz Madeyski suggested). Just be aware that the tasks work a bit differently than the web app (e.g. read about using sfContext in tasks)
You can also use curl to access your sf pages directly from CLI:
curl -sS http://mysymfonyproject.com/index.php/crons/sendnewsletter >> mylog.log
The -sS option will make curl silent unless there's an error. You can skip that if you don't need it.
The last part (>> mylog.log) will append the result of the request to the log file (it's best if you specify whole path to the log file)
As for setting the cron job it's fairly easy no matter if you use curl or sf tasks. Have a look around the web for a tutorial on setting the crontab, there's plenty of them out there.

How do I run a php function in a subprocess in Symfony2?

I want to run some functions parallel in the background.
Is there a nice way to do this in Symfony2 or even with php?
Or is there only the Symfony\Component\Process\Process?
Can I use this Process with a function? I need the actual Context (logged in user and some session data), so it is not possible to source the function out to an external php-file...
Symfony2 Process component allows you to run some shell command or execute php-script in a different process.
To run exact function in a thread try look at PHP Thread class

Categories