__construct() method call but handle() function not call in laravel job - laravel-5.4

I am trying to execute job through dispatch() method using
laravel 5.4
redis-server
supervisor
I have done queue config like 'default' => env('QUEUE_DRIVER', 'redis').
I am call dispatch() method in my app/Services file
dispatch(new SavePropertyImages($pid_list));
the following is my job file in app/Jobs:
namespace App\Jobs;
use App\Property;
use App\Services\CreaBase;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class SavePropertyImages implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $pid;
protected $creaBase;
public $timeout = 300;
public $tries = 1;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($pid)
{
$this->pid = $pid;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$this->creaBase = new CreaBase();
if (!$this->creaBase->isLogin){
$this->creaBase->init();
}
$this->creaBase->saveAllImages("Property", $this->pid);
}
}
When I call a dispatch(new SavePropertyImages($pid_list)) method, the job's __construct() method calls, but it does not call handle() function.
Do you have any ideas?

bingo, I got the answer
my handle() function does not call. because I set a supervisor but not start the supervisor process that's why my queue:work redis process not start and my job does not assign to redis server and my job was not to execute.
first off all I start my supervisor service and my queue works fine.

Related

PhpStorm no autocomplete when using getInstance method of a Class

Can somebody tell me why the autocompletion doesn't work when I'm using a getInstance() method instead of new ClassName?
Following is the getInstance() method of the class:
// Define The Namespace For Our Library
namespace JUnstoppable;
class JUnstoppable
{
// Instance Of The Class
protected static $instance = array ();
public static function getInstance ($forPlatformName = 'joomla')
{
$forPlatformName = strtolower($forPlatformName);
if (!isset(static::$instance[$forPlatformName]))
{
static::$instance[$forPlatformName] = new \JUnstoppable\JUnstoppable($forPlatformName);
}
return static::$instance[$forPlatformName];
}
public function __construct ($platformName = 'joomla')
{
}
public function doExecute ($test = 'lalala')
{
return $test;
}
}
Can somebody tell me why the autocompletion doesn't work when I'm using a getInstance() method instead of new ClassName?
That's because IDE does not know what can be inside your $instance static property and therefore it cannot figure out what getInstance() returns. From IDE point of view it's just plain array (elements of any types) and not an array of JUnstoppable instances.
You can place caret on $test and invoke View | Quick Documentation to see what IDE knows about that variable. If it does not say JUnstoppable there then no wonders.
Just add proper type hint for return value of getInstance() method via PHPDoc's #return tag:
/**
* My super method.
*
* #param string $forPlatformName Optional parameter description
* #return JUnstoppable
*/
public static function getInstance ($forPlatformName = 'joomla')
You can specify concrete class (JUnstoppable in this case) .. or static if this method will be used by child classes as well and they will return different instances.
Alternatively (or better say: in addition) you can typehint $instance property which IDE will use to figure out what getInstance() method returns:
/** #var JUnstoppable[] Instance Of The Class */
protected static $instance = array ();

Laravel ajax response issue returning with html content

I am using ajax for ratings. Rating successfully but return response with html code above the json. And it is because of send mail function.
The this code alwasy attached when return response and may be it was from send mail function.
When I am removing Mail function it will return proper result and its working well.
If i were in your place i will work with jobs.
So Here is how it goes
php artisan make:job SendingEmail
App\Jobs\SendingEmail.php
<?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;
use Illuminate\Support\Facades\Mail;
class SendingEmail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $admin_email;
protected $email_data;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($admin_email,$email_data)
{
$this->email_data = $email_data;
$this->admin_email = $admin_email;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
Mail::to($this->admins_email)->send(new RateNotification($this->email_data));
}
}
App\Jobs\SendingEmail::dispatch($podcast);
In Your Controller add this
App\Jobs\SendingEmail::dispatch($admin_email,$email_data);

Getting a Parse error: syntax error, unexpected '$faker' error when seeding

I am getting the following error when trying to seed in Laravel 5.4
[Symfony\Component\Debug\Exception\FatalThrowableError]
Parse error: syntax error, unexpected '$faker' (T_VARIABLE), expecting function (T_FUNCTION) or c
onst (T_CONST)
Here is the code for the seed file.
<?php
use Illuminate\Database\Seeder;
use App\Book;
use Faker\Factory as Faker;
class BookSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
$faker = Faker::create();
public function run()
{
foreach (range(1, 30) as $index) {
Book::create([
'title'=> $faker->sentence(5),
'author'=> $faker->sentence(7),
'description'=>$faker->paragraph(4)
]);
}
}
}
I have created the model and done the migration. I can't seem to find any good tutorials on how to do this with Laravel 5.4 . Any help would be appreciated.
Declare $faker variable in run method will solve the problem
`<?php
use Illuminate\Database\Seeder;
use App\Book; use Faker\Factory as Faker;
class BookSeeder extends Seeder
{
/**
* Run the database seeds. *
* #return void */
public function run() {
$faker = Faker::create();
foreach (range(1, 30) as $index) {
Book::create([
'title'=> $faker->sentence(5),
'author'=> $faker->sentence(7),
'description'=>$faker->paragraph(4)
]);
}
}
}`

Yii2 console command pass arguments with name

I want to use commands as
php yii sync anyvar2=anValue anyVar1=anyValue
In controller
public function actionIndex(){
echo $anyVar1;
echo $anyVar2;
}
I tried with php yii sync [--anyvar2=anValue ,--anyVar1=anyValue]
1) If you want to set controller parameters:
class SyncController extends \yii\console\Controller
{
public $anyVar1;
public $anyVar2;
public function options($actionID)
{
return array_merge(parent::options($actionID), [
'anyVar1', 'anyVar2'
]);
}
}
Now you can set them like that:
php yii sync --anyVar1=aaa --anyVar2=bbb
2) If you want to just pass variables as arguments:
public function actionIndex($anyVar1, $anyVar2)
{
// ...
}
Now you can set them like that:
php yii sync aaa bbb
Got solution
when need to pass variable in console
Variable should declared in public scope.
Variable should returned in options function
Ex:
class SyncController extends \yii\console\Controller
{
public $anyVar1;
public $anyVar2;
public function options()
{
return ['anyVar1','anyVar2'];
}
public function actionIndex(){
echo $this->anyVar1."\n";
echo $this->anyVar2."\n";
}
}
In console
php yii sync --anyVar2=1111 --anyVar1=999

Laravel seeding tables in second database

connection('mysql2') is my (working) second database connection.
When I migrate first, connection('mysql2') is working like expected, the table is created.
Schema::connection('mysql2')->create('brands', function(Blueprint $table)
{
//...
});
But when I try to seed tables in my second database:
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use App\Brands;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Model::unguard();
$this->call('BrandsTableSeeder');
$this->command->info("Brands table seeded.");
}
}
class BrandsTableSeeder extends Seeder
{
public function run()
{
DB::connection('mysql2')->table('brands')->delete();
Brands::connection('mysql2')->create(['brand' => 'test']);
}
}
I got:
[BadMethodCallException]
Call to undefined method Illuminate\Database\Query\Builder::connection()
Problem with your code is you have used Connection() method with Eloquent(not DB), Eloquent doesn't have connection() method.
You can use on() method with model(Eloquent) to specify connection
$user = User::on('mysql2')->create(['brand' => 'test']);
reference http://laravel.com/docs/4.2/eloquent#basic-usage
or
instead of writing on('mysql2') everywhere
you can write following code in model
protected $connection = 'mysql2';
and now seed is written as
class BrandsTableSeeder extends Seeder
{
public function run()
{
Brands::truncate();
Brands::create(['brand' => 'test']);
}
}