Package DE_EPAGES::Trigger::API::BatchProcessor
This package provides mechanism to collect Hook events and batch
them for later processing with a BatchProcessHandler.
Standard workflow:
- Think of unique batch task name (for example $BatchTaskName = 'SolrBatchProductExport')
- Create your own BatchProcessHandler by inherit from DE_EPAGES::Trigger::API::Object::BatchProcessHandler (e.g. ProductBatchExporter)
- you must implement processItem()
- for full control over processing override processItemList()
- Register your own BatchProcessHanlder with RegisterHandler( $BatchTaskName, ...::ProductBatchExporter->new() )
- Feed RunBatchProcess(...) with your Business logic which trigger hook function, which use HandleItem($BatchTaskName, $CurrentProduct)
see DE_EPAGES::Trigger::API::Object::BatchProcessHandler and small example /t/BatchProcessor2.t for implementation
Example |
# # BusinessLogic # package My::UI::BusinessLogic; use DE_EPAGES::Trigger::API::BatchProcessor qw( RunBatchProcess ); use DE_EPAGES::Trigger::API::Trigger qw( TriggerHook); sub ImportProducts { ... RunBatchProcess('DummyImportProducts', sub { foreach my $hProduct ( @productList ){ TriggerHook('MyEvent', $hProduct) } } # after code is done ProductBatchHandler is called automatically ); ... } # # Hooks # package My::Hooks::Product; use DE_EPAGES::Trigger::API::BatchProcessor qw( RegisterHandler HandleItem ); sub OnRegisterMyProductsHandler{ # Handler must be registered before it can be used RegisterHandler('ImportProducts', MyCartridge::Hooks::ProductBatchHandler->new() ); } sub MyEvent{ my ($hProduct) = @_; # collect Product - addItems($item) of ProductBatchHandler is called HandleItem('ImportProducts','My::Hooks::ProductBatchHandler', $hProduct); } |
@EXPORT_OK |
Functions
- ExistsBatchHook
- GetHandler
- HandleItem
- HandleItemInBatchModeOnly
- IsCalledInBatchMode
- IsHandlerRegistered
- RegisterHandler
- RunBatchProcess
ExistsBatchHook
Returns true if a batch hook registration exists for the given batch task name.
Syntax |
ExistsBatchHook( $BatchTaskName ); |
Input |
|
Return |
|
GetHandler
return object which implements BatchProcessHandler
Syntax |
my $Handler = GetHandler( $BatchTaskName, $HandlerPackageName ); |
Example |
my $Handler = GetHandler( 'MyProductExport', 'DE_EPAGES::My::Hooks::ProductBatchExporter' ); |
Input |
|
Return |
|
HandleItem
Queue items for later processing in specified BatchHandler, which is registered under $BatchTaskName. If invokecation is not triggered by RunBatchProcess(...), then the item will processed instantly. To ensure the BatchHandler is only invoked by RunBatchProcess use function IsCalledInBatchMode('MyBatchTask') as guard.
Syntax |
HandleItem( $BatchTaskName, $HandlerPackageName, $Item ); |
Example |
HandleItem( 'MyBatchTask', 'MyCompany::MyCartridge::API::Object::MyProductImportHandler', $Item ); HandleItem( 'MyBatchTask', 'MyCompany::MyCartridge::API::Object::MyProductImportHandler', $Item ) if IsCalledInBatchMode('MyBatchTask'); |
Input |
|
HandleItemInBatchModeOnly
Queue items for later processing in specified BatchHandler, which is registered under $BatchTaskName.
Syntax |
HandleItemInBatchModeOnly( $BatchTaskName, $HandlerPackageName, $Item ); |
Example |
HandleItemInBatchModeOnly( 'MyBatchTask', 'MyCompany::MyCartridge::API::Object::MyProductImportHandler', $Item ); HandleItemInBatchModeOnly( 'MyBatchTask', 'MyCompany::MyCartridge::API::Object::MyProductImportHandler', $Item ) if IsCalledInBatchMode('MyBatchTask'); |
Input |
|
IsCalledInBatchMode
Returns true only if $BatchTaskName called in BatchMode - inside RunBatchProcess.
Syntax |
IsCalledInBatchMode( $BatchTaskName ); |
Input |
|
Return |
|
IsHandlerRegistered
Retruns true only if a BatchHandler is defined for given BatchTask
Syntax |
my $IsRegistered = IsHandlerRegistered( $BatchTaskName, $BatchHandler ); |
Input |
|
Return |
|
RegisterHandler
Register a BatchProcessHandler under task name, which must be unique. You may register more than one Handler under the same task name.
Syntax |
RegisterHandler( $BatchTaskName, $ImplementedBatchProcessHandler ); |
Example |
RegisterHandler( 'SolrProductExport', $ProductBatchExporter ) |
Input |
|
RunBatchProcess
This method first fires event TriggerHook('API_Register' . $BatchTaskName . 'Handler' )
then runs $cSub. Eventually all registerd BatchProcessHandler for $BatchTaskName
will be executed. In $cSub HandleItem must be used to queue
items for specific BatchProcessHandler.
By wrapping RunBatchProcess whith second RunBatchProcess (with same $BatchTaskName)
you are able to deactivate batch processing. This may be benifitial if default batch
processing like CSV/XML import don't suit your import style e.g. large number of short imports.
Syntax |
RunBatchProcess( $BatchTaskName, $cSub ); |
Input |
|