I'm trying to setup a cron job to run a console command every minute.
class RescanCommand extends Command {
public function sendMail() {
$email = new Email();
// Sample SMTP configuration.
Email::setConfigTransport('mailtrap', [
'host' => 'smtp.mailtrap.io',
'port' => 25,
'username' => 'username',
'password' => 'pass',
'className' => 'Smtp'
]);
$email->setFrom(['test#test.com' => 'CSV file'])
->setTo('test#test.com')
->setSubject('CSV Link File')
->send('Please find attached a copy of the links');
}
public function execute(Arguments $args, ConsoleIo $io) {
$this->sendMail();
}
}
I can run the above code from command line by
bin/cake rescan execute
and I receive the test email, but after creating a cron job by editing cron tabs like
crontab -e
and inside the file by writing
*/1 * * * * cd /Applications/MAMP/htdocs/music && bin/cake Rescan execute
nothing happens, I was expecting to receive emails every minute,
can anyone please help find out what I'm doing wrong.
Thanks
Related
i'm using laravel6, and voyager, i'm creating artisan command for installation use cmd following php artisan make: command EcommerceInstall
, I recopy DataTypesTableSeeder from database/seeder and I rename it by DataTypesTableSeederCustom, I modify my file which I wrote EcommerceInstall.php but it gives me error class [DataTypesTableSeederCustom] does not exist.
i think i forget import the class but i don't know where .
EcommerceInstall.php
public function handle()
{
if ($this->confirm('this well delete all you current data and install the dummy default data, Are you sure ?')) {
File::deleteDirectory(public_path('storage/products/dummy'));
$this->callSilent('storage:link');
$copySuccess = File::copyDirectory(public_path('img/products'),public_path('storage/products/dummy'));
if($copySuccess){
$this->info('images succefully copied to storage folder');
}
$this->call('migrate:fresh', [
'--seed' => true,
]);
$this->call('db:seed', [
'--class' => 'DataTypesTableSeederCustom'
]);
$this->info('Dummy data installed');
}
Im using Queue::before in AppServiceProvider.php and set logging.channels.single.path value every time when job started:
config(['logging.channels.single.path' => storage_path('logs/accounts/'.$command->acc->login.'.log')]);
When I running 1 job all ok - logs in the right place.
When running 2 or more it writing logs to different files - one account can write to another accounts logfile. Why is it happening? It looks like it is caching the config variable.
Queue on horizon redis. One job after done dispatching another same job with the same $acc instance.
Queue::before(function (JobProcessing $event) {
$job = $event->job->payload();
$command = unserialize($job['data']['command']);
Added ^^^ from where $command going.
Customization is now done through invoking a custom formatter for Monolog.
This can be setup in config/logging.php, note the non-default tap parameter:
'channels' => [
'daily' => [
'driver' => 'daily',
'tap' => [App\Logging\CustomFilenames::class],
'path' => storage_path('logs/accounts/laravel.log'),
'level' => 'debug',
],
]
In your custom formatter, you can manipulate the Monolog logger however you wish:
<?php
namespace App\Logging;
use Monolog\Handler\RotatingFileHandler;
class CustomFilenames
{
/**
* Customize the given logger instance.
*
* #param \Illuminate\Log\Logger $logger
* #return void
*/
public function __invoke($logger) {
foreach ($logger->getHandlers() as $handler) {
if ($handler instanceof RotatingFileHandler) {
$login = $command->acc->login;
$handler->setFilenameFormat("{filename}-$login-{date}", 'Y-m-d');
}
}
}
}
See: https://laravel.com/docs/5.6/logging#advanced-monolog-channel-customization
https://github.com/Seldaek/monolog/blob/master/src/Monolog/Handler/RotatingFileHandler.php
The configuration values work globaly for all sessions, like global variables (
See the exampe here https://laravel.io/forum/how-can-i-set-global-dynamic-variables-in-laravel)
You set the value in the config file always to the last login. Therefore all new logs go in the new named config file.
I've hosted my app in Digital Ocean with Laravel Forge and have created a artisan command to run through schedule [Nightly].
The artisan command runs smoothly via terminal but returns No such file or directory while running it through Scheduled Jobsmanually from Laravel Forge.
The command for the job is:
php /home/forge/sitename.com/artisan Instagram:fetch
The artisan command php /home/forge/sitename.com/artisan Inspire i.e php artisan inspire returns the correct output.
While the above mentioned custom artisan returns No such file or directory
app\Console\Commands\FetchInstagram.php's handel method:
public function handle()
{
$instagram = new Instagram('api-key');
$posts = $instagram->media(); //gets 20 latest insta posts of a user
// dd($posts);
$fromDBs = Insta::orderBy('id', 'desc')->take(20)->get(); //get last 20 rows from table
foreach( $posts as $post)
{
Insta::firstOrCreate([
'thumb_link' => $post->images->thumbnail->url ,
'standard_link' => $post->images->standard_resolution->url ,
'caption' => $post->caption->text
]);
}
}
Code from Kernel.php:
protected $commands = [
'App\Console\Commands\FetchInstagram'
];
protected function schedule(Schedule $schedule)
{
$schedule->command('Instagram:fetch')
->daily();
}
I'm trying to create a customised command for my first time with Symfony.
This command should execute 3 commands in one go.
This is my command class
class DoctrineFullFixturesLoadCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('doctrine:full-fixtures:load')
->setDescription('...')
->addArgument('argument', InputArgument::OPTIONAL, 'Argument description')
->addOption('option', null, InputOption::VALUE_NONE, 'Option description')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$argument = $input->getArgument('argument');
if ($input->getOption('option')) {
// ...
}
$arrCommands = [
[
'command' => 'doctrine:database:drop',
'--force' => true
], [
'command' => 'doctrine:database:create'
], [
'command' => 'doctrine:schema:update',
'--force' => true,
'--complete' => true
]
];
foreach ($arrCommands as $arrInput) {
$this->getApplication()->run(
new ArrayInput($arrInput),
$output
);
}
$output->writeln('Command result.');
}
}
The issue occurs when the iteration starts.
It is actually executed only the first command in the line instead of all three.
It seems the iteration stops after executing the first command for some reason, which I don't understand, in fact the method at the end $output->writeln, isn't called at all.
You should enable the debug for the console to understand the problem.
Console commands run in the environment defined in the APP_ENV variable of the .env file, which is dev by default. It also reads the APP_DEBUG value to turn "debug" mode on or off (it defaults to 1, which is on).
To run the command in another environment or debug mode, edit the value of APP_ENV and APP_DEBUG.
I have controller like this
public function store(Request $request)
{
Artisan::call("php artisan infyom:scaffold {$request['name']} --fieldsFile=public/Product.json");
}
Show me error
There are no commands defined in the "php artisan infyom" namespace.
When I run this command in CMD it work correctly
You need to remove php artisan part and put parameters into an array to make it work:
public function store(Request $request)
{
Artisan::call("infyom:scaffold", ['name' => $request['name'], '--fieldsFile' => 'public/Product.json']);
}
https://laravel.com/docs/5.2/artisan#calling-commands-via-code
If you have simple job to do you can do it from route file. For example you want to clear cache. In terminal it would be php artisan cache:clear In route file that would be:
Route::get('clear_cache', function () {
\Artisan::call('cache:clear');
dd("Cache is cleared");
});
To run this command from browser just go to your's project route and to clear_cache. Example:
http://project_route/clear_cache
Apart from within another command, I am not really sure I can think of a good reason to do this. But if you really want to call a Laravel command from a controller (or model, etc.) then you can use Artisan::call()
Artisan::call('email:send', [
'user' => 1, '--queue' => 'default'
]);
One interesting feature that I wasn't aware of until I just Googled this to get the right syntax is Artisan::queue(), which will process the command in the background (by your queue workers):
Route::get('/foo', function () {
Artisan::queue('email:send', [
'user' => 1, '--queue' => 'default'
]);
//
});
If you are calling a command from within another command you don't have to use the Artisan::call method - you can just do something like this:
public function handle()
{
$this->call('email:send', [
'user' => 1, '--queue' => 'default'
]);
//
}
Source: https://webdevetc.com/programming-tricks/laravel/general-laravel/how-to-run-an-artisan-command-from-a-controller/
Remove php artisan part and try:
Route::get('/run', function () {
Artisan::call("migrate");
});
Command Job,
Path : {project-path}/app/Console/Commands/RangeDatePaymentsConsoleCommand.php
This is the Job that runs with the artisan command.
class RangeDatePaymentsConsoleCommand extends Command {
protected $signature = 'batch:abc {startDate} {endDate}';
...
}
web.php,
Path : {project-path}/routes/web.php
web.php manage all the requests and route to relevant Controller and can have multiple routes for multiple controllers and multiple functions within the same controller.
$router->group(['prefix' => 'command'], function () use ($router) {
Route::get('abc/start/{startDate}/end/{endDate}', 'CommandController#abc');
});
CommandController.php,
Path : {project-path}/app/Http/Controllers/CommandController.php
This Controller is created for handle artisan commands and name can be vary but should be same to web.php Controller name and the function name.
class CommandController extends Controller {
public function abc(string $startDate, string $endDate) {
$startDate = urldecode($startDate);
$endDate = urldecode($endDate);
$exitCode = Artisan::call('batch:abc',
[
'startDate' => $startDate,
'endDate' => $endDate
]
);
return 'Command Completed Successfully. ';
}
Request : http://127.0.0.1:8000/command/abc/start/2020-01-01 00:00:00/end/2020-06-30 23:59:59
It's can be access through the web browser or Postman after startup the server. Run this command to start the php server at {project-path}
php -S 127.0.0.1:8080 public/index.php
Method #1: Using route
Route::get('run-it', function () {
(new \App\Console\Commands\ThisIsMyCommand())->handle();
});
Method #2: Using command line
php artisan command:this_is_my_command