summaryrefslogtreecommitdiff
path: root/lib/presque/worker/Role/Dispatcher.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/presque/worker/Role/Dispatcher.pm')
-rw-r--r--lib/presque/worker/Role/Dispatcher.pm44
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/presque/worker/Role/Dispatcher.pm b/lib/presque/worker/Role/Dispatcher.pm
new file mode 100644
index 0000000..04ac8c3
--- /dev/null
+++ b/lib/presque/worker/Role/Dispatcher.pm
@@ -0,0 +1,44 @@
+package presque::worker::Role::Dispatcher;
+
+use Moose::Role;
+use Try::Tiny;
+
+has fork_dispatcher => (
+ is => 'ro',
+ isa => 'Bool',
+ default => 0,
+);
+
+around work => sub {
+ my ($orig, $self, $job) = @_;
+
+ try {
+ if ($self->fork_dispatcher) {
+ $self->_fork_and_work($orig, $job);
+ }
+ else {
+ $self->$orig($job);
+ }
+ }catch{
+ $self->_job_failure($job, $_);
+ };
+};
+
+
+sub _fork_and_work {
+ my ($self, $orig, $job) = @_;
+
+ my $pid = fork();
+ if ($pid == 0) {
+ $self->$orig($job);
+ exit;
+ }
+ elsif ($pid > 0) {
+ return;
+ }
+ else {
+ # failure
+ }
+}
+
+1;