1

Good morning!

I have the following code in my Controller:

$num_rows = $this->drupalRequestRowCount();
$uris = $this->drupalRequestPrepareUri($num_rows, config('constants.IMPORT_DATA_URI'));
$requestHandler = new RequestHandler($uris[0]);
$content = $requestHandler->httpRequest();

// Dispatch each URI for the job that will handle
// the insertion of our Drupal data.
foreach($uris as $uri) {
  $job = (new DataJob($uri))->onQueue('data_export_insert');
  $this->dispatch($job);
}

return 'Jobs dispatched';

I have a Drupal database with data that I want to synchronize with my Laravel database every night. I make use of an API i developed myself in Drupal that returns a lot of data that I need for my Laravel database.

The API returns a maximum of 10 items (which contain a lot of data). The Drupal database has +- 17000 items that I need to import every night. I thought that the Queue API might be a good solution for this to import the data in batches instead of importing it all at once. I loop through the $uris array in the foreach loop that appends an offset to the API based on the $num_rows I get.

So if $num_rows returns 17000 items, the $uris array will contain 1700 items like so:

[0] => 'http://mywebsite.com/api/request_data.json?offset=0'

[1] => 'http://mywebsite.com/api/request_data.json?offset=10'

[2] => 'http://mywebsite.com/api/request_data.json?offset=20' etc.

This means that I want to have 1700 jobs dispatched that Laravel will execute for me once I run the php artisan queue:listen database command.

The following code is the Job I want to execute 1700 times.

public function __construct($uri)
{
  $this->uri = $uri;
}
public function handle()
{
  try {
    Log::info('Inserting data into database for uri: ' . $this->uri);

    $requestHandler = new RequestHandler($this->uri);
    $content = $requestHandler->httpRequest();

    foreach($content as $key => $value) {
      $id = $value->id;
      $name = $value->name;
      DB::insert('INSERT INTO table_1 (id, name) VALUES (?, ?)', [$id, $name]);
    }
  } catch(\Exception $e) {
    Log::error('Error in job: '. $e->getMessage());
  }
}

My problem is that the Jobs don't get executed at all. The jobs are being correctly dispatched to the database because I can see 1700 rows in Jobs the table when I check the Jobs table in the database. I tried logging the handle function but when i run php artisan queue:listen or php artisan queue:work nothing happens. The failed_jobs table also don't have any records.

The handle function is not being executed so there is no way I can log anything that's happening inside. I tried finding information on the internet with examples, but all I can find is some mailing examples which I don't need.

Any help would be greatly appreciated!

nicolinho66
  • 107
  • 1
  • 7

1 Answers1

2

Fixed the issue. Make sure you place in your Job code protected $payload! With payload being the data you want to pass to the Queue Handler!

nicolinho66
  • 107
  • 1
  • 7