Skip to content

Commit

Permalink
ExportProcessor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbjr committed Oct 11, 2024
1 parent 7547e5d commit 1a61adb
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 12 deletions.
65 changes: 63 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,69 @@ class UserExportProcessor extends ExportProcessor
}
```

Next, create an `ExportNovaAction` class and create a `processor()` function that returns the processor class you just
created.
You can also override methods in the `ExportProcessor` class to customize the export process. The following methods can
be overridden:

```php
<?php

namespace App\Nova\Exports\Products;

use App\Models\Product;
use Coreproc\NovaDataSync\Export\Jobs\ExportProcessor;
use Illuminate\Contracts\Database\Query\Builder;

class ProductExportProcessor extends ExportProcessor
{
public function __construct(public string $startDate, public string $endDate)
{
// Always remember to call the parent constructor when overriding the constructor
parent::__construct();
}

public function query(): Builder
{
$startDate = Carbon::make($this->startDate)->startOfDay();
$endDate = Carbon::make($this->endDate)->endOfDay();

return Product::query()
->whereBetween('created_at', [$startDate, $endDate])
->with('productCategory');
}

public function formatRow($row): array
{
return [
'name' => $row->name,
'product cat' => $row->productCategory->name ?? null,
'price' => $row->price,
];
}

public static function queueName(): string
{
return 'custom-queue-name'; // Default is whatever is set in the config
}

public function allowFailures(): bool
{
return true; // Default is whatever is set in the config
}

public function disk(): string
{
return 'custom-disk-name'; // Default is whatever is set in the config
}

public static function chunkSize(): int
{
return 100; // Default is whatever is set in the config
}
}
```

Next, in order to use it as a Nova Action, create an `ExportNovaAction` class and create a `processor()` function that
returns the processor class you just created.

```php
namespace App\Nova\Exports;
Expand Down
1 change: 1 addition & 0 deletions config/nova-data-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
'chunk_size' => 1000,
'queue' => 'default',
'table_name' => 'exports',
'allow_failures' => true,
],

'nova_resources' => [
Expand Down
17 changes: 7 additions & 10 deletions src/Export/Jobs/ExportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,11 @@ abstract class ExportProcessor implements ShouldQueue

protected string $directory = '';

/**
* Pass your query here as a Builder instance
*/
abstract public function query(): Builder;

public function __construct()
{
$this->onQueue($this->queue());
$this->onQueue(self::queueName());
}

/**
Expand Down Expand Up @@ -104,7 +101,7 @@ public function handle(): void
})
->allowFailures($this->allowFailures())
->name($this->name())
->onQueue(config('nova-data-sync.exports.queue', 'default'))
->onQueue(self::queueName())
->dispatch();
}

Expand All @@ -124,19 +121,19 @@ protected function name(): string
}

/**
* Override this in your ExportProcessor class to set a different queue.
* The queue to use for the export
*/
protected function queue(): string
public static function queueName(): string
{
return config('nova-data-sync.exports.queue', 'default');
}

/**
* Override this in your ExportProcessor class to set allow failures in the batch job.
* Whether to allow failures in the batch job
*/
protected function allowFailures(): bool
public function allowFailures(): bool
{
return true;
return config('nova-data-sync.exports.allow_failures', true);
}

protected function directory(): string
Expand Down

0 comments on commit 1a61adb

Please sign in to comment.