ePages 6.15.1 - DE_EPAGES/Trigger/API/BatchProcessor.pm

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:

  1. Think of unique batch task name (for example $BatchTaskName = 'SolrBatchProductExport')
  2. Create your own BatchProcessHandler by inherit from DE_EPAGES::Trigger::API::Object::BatchProcessHandler (e.g. ProductBatchExporter)
  3. Register your own BatchProcessHanlder with RegisterHandler( $BatchTaskName, ...::ProductBatchExporter->new() )
  4. Feed RunBatchProcess(...) with your Business logic which trigger hook function, which use HandleItem($BatchTaskName, $CurrentProduct)
The batch task name must be choosen with care, because it have implications: First the $BatchTaskName is used determine where your registered BatchProcessHandler(s) are stored. Second every time RunBatchProcess($BatchTaskName, sub{...}) is called, TriggerHook('API_Register' . $BatchTaskName . Handler). In the example above the event 'API_RegisterSolrBatchProductExportHandler' is triggered. Finally there are no namespaces or other checks which prevent overwriting or breaking other batch handler.
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
GetHandler
RegisterHandler
RunBatchProcess
HandleItem
IsCalledInBatchMode
IsHandlerRegistered

Functions

GetHandler
HandleItem
IsCalledInBatchMode
IsHandlerRegistered
RegisterHandler
RunBatchProcess

GetHandler

return object which implements BatchProcessHandler

Syntax
my $Handler = GetHandler( $BatchTaskName, $HandlerPackageName );
Example
my $Handler = GetHandler( 'MyProductExport', 'DE_EPAGES::My::Hooks::ProductBatchExporter' );
Input
$BatchTaskName (String)
unique name (see package discription)
$HandlerPackageName (String)
package name of a BatchProcessHandler
Return
$YourBatchProcessHandler (BatchProcessHandler)
BatchProcessHandler $HandlerPackageName registered
$BatchTaskName

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
$BatchTaskName (String)
unique task name
$HandlerPackageName (String)
package name of BatchProcesHandler
$Item (any)
payload for later processing

IsCalledInBatchMode

Returns true only if $BatchTaskName called in BatchMode - inside RunBatchProcess.

Syntax
IsCalledInBatchMode( $BatchTaskName );
Input
$BatchTaskName (String)
Name of batch task
Return
$InBatchMode (boolean)
is called in batch context

IsHandlerRegistered

Retruns true only if a BatchHandler is defined for given BatchTask

Syntax
my $IsRegistered = IsHandlerRegistered( $BatchTaskName, $BatchHandler );
Input
$BatchTaskName (String)
unique name
$BatchHandler (String)
package name of BatchHandler class
Return
$IsRegistered (Boolean)
True if $BatchHandler is registered for $BatchTaskName

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
$BatchTaskName (String)
unique name
$ImplementedBatchProcessHandler (object)
implementation of BatchProcessHandler

RunBatchProcess

This method first fire event TriggerHook('API_Register' . $BatchTaskName . 'Handler' ) than run $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
$BatchTaskName (String)
unique name for batch task
$cSub (code.ref)
your code block