| 1 | package Daemonise::Plugin::JobQueue; | ||
| 2 | |||
| 3 | 1 | ▶ | use Mouse::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | # ABSTRACT: Daemonise JobQueue plugin | ||
| 6 | |||
| 7 | 1 | ▶ | use Data::Dumper; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Digest::MD5 'md5_hex'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use DateTime; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use Basket::Calc; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use Scalar::Util qw(looks_like_number); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | |||
| 14 | 1 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 15 | 1 | with("Daemonise::Plugin::CouchDB"); | |
| 16 | 0 | with("Daemonise::Plugin::RabbitMQ"); | |
| 17 | 0 | with("Daemonise::Plugin::KyotoTycoon"); | |
| 18 | } | ||
| 19 | |||
| 20 | |||
| 21 | has 'jobqueue_db' => ( | ||
| 22 | is => 'rw', | ||
| 23 | isa => 'Str', | ||
| 24 | lazy => 1, | ||
| 25 | default => sub { 'jobqueue' }, | ||
| 26 | ); | ||
| 27 | |||
| 28 | |||
| 29 | has 'job' => ( | ||
| 30 | is => 'rw', | ||
| 31 | isa => 'HashRef', | ||
| 32 | lazy => 1, | ||
| 33 | default => sub { {} }, | ||
| 34 | ); | ||
| 35 | |||
| 36 | |||
| 37 | has 'items_key' => ( | ||
| 38 | is => 'rw', | ||
| 39 | isa => 'Str', | ||
| 40 | lazy => 1, | ||
| 41 | default => sub { 'domains' }, | ||
| 42 | ); | ||
| 43 | |||
| 44 | |||
| 45 | has 'item_key' => ( | ||
| 46 | is => 'rw', | ||
| 47 | isa => 'Str', | ||
| 48 | lazy => 1, | ||
| 49 | default => sub { 'domain' }, | ||
| 50 | ); | ||
| 51 | |||
| 52 | |||
| 53 | has 'log_worker_enabled' => ( | ||
| 54 | is => 'rw', | ||
| 55 | isa => 'Bool', | ||
| 56 | lazy => 1, | ||
| 57 | default => sub { 1 }, | ||
| 58 | ); | ||
| 59 | |||
| 60 | |||
| 61 | has 'job_locked' => ( | ||
| 62 | is => 'rw', | ||
| 63 | isa => 'Bool', | ||
| 64 | default => sub { 0 }, | ||
| 65 | ); | ||
| 66 | |||
| 67 | |||
| 68 | has 'jobqueue_sync_delay' => ( | ||
| 69 | is => 'rw', | ||
| 70 | isa => 'Int', | ||
| 71 | default => sub { 1 }, | ||
| 72 | ); | ||
| 73 | |||
| 74 | # internal attribute to store all command hooks | ||
| 75 | has '_hooks' => ( | ||
| 76 | is => 'rw', | ||
| 77 | isa => 'HashRef[CodeRef]', | ||
| 78 | ); | ||
| 79 | |||
| 80 | |||
| 81 | after 'configure' => sub { | ||
| 82 | my ($self, $reconfig) = @_; | ||
| 83 | |||
| 84 | $self->log("configuring JobQueue plugin") if $self->debug; | ||
| 85 | |||
| 86 | if (ref($self->config->{jobqueue}) eq 'HASH') { | ||
| 87 | foreach my $conf_key ('db', 'sync_delay') { | ||
| 88 | my $attr = "jobqueue_" . $conf_key; | ||
| 89 | $self->$attr($self->config->{jobqueue}->{$conf_key}) | ||
| 90 | if defined $self->config->{jobqueue}->{$conf_key}; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | return; | ||
| 95 | }; | ||
| 96 | |||
| 97 | |||
| 98 | around 'log' => sub { | ||
| 99 | my ($orig, $self, $msg) = @_; | ||
| 100 | |||
| 101 | if (ref $self->job->{message} eq 'HASH' | ||
| 102 | and exists $self->job->{message}->{meta}) | ||
| 103 | { | ||
| 104 | foreach my $meta (qw/account user session id/) { | ||
| 105 | my $log_meta = $meta eq 'id' ? 'job' : $meta; | ||
| 106 | $msg = | ||
| 107 | "$log_meta=" | ||
| 108 | . $self->job->{message}->{meta}->{$meta} . ' ' | ||
| 109 | . $msg | ||
| 110 | if (exists $self->job->{message}->{meta}->{$meta} | ||
| 111 | and defined $self->job->{message}->{meta}->{$meta}); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | $self->$orig($msg); | ||
| 116 | |||
| 117 | return; | ||
| 118 | }; | ||
| 119 | |||
| 120 | |||
| 121 | around 'start' => sub { | ||
| 122 | my ($orig, $self, $code) = @_; | ||
| 123 | |||
| 124 | # check whether we have an optional custom CODEREF | ||
| 125 | if (defined $code) { | ||
| 126 | unless (ref $code eq 'CODE') { | ||
| 127 | $self->log( | ||
| 128 | "first argument of start() must be a CODEREF! existing..."); | ||
| 129 | $self->stop; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | my $wrapper = sub { | ||
| 134 | |||
| 135 | # reset job_locked attribute to unlocked to start with | ||
| 136 | $self->job_locked(0); | ||
| 137 | |||
| 138 | # get the next message from the queue (includes locking) | ||
| 139 | my $msg = $self->dequeue; | ||
| 140 | |||
| 141 | # error if message was empty or not a hashref | ||
| 142 | $msg = { error => "message must not be empty!" } | ||
| 143 | unless ref $msg eq 'HASH'; | ||
| 144 | |||
| 145 | # don't process anything if we already have an error | ||
| 146 | # e.g. locking failed, empty message, ... | ||
| 147 | if (exists $msg->{error}) { | ||
| 148 | $self->_finish_processing($msg); | ||
| 149 | return; | ||
| 150 | } | ||
| 151 | |||
| 152 | unless (exists $msg->{data} | ||
| 153 | and ref $msg->{data} eq 'HASH' | ||
| 154 | and exists $msg->{data}->{command} | ||
| 155 | and $msg->{data}->{command}) | ||
| 156 | { | ||
| 157 | $msg->{error} = "msg->data->command missing or empty!"; | ||
| 158 | $self->_finish_processing($msg); | ||
| 159 | return; | ||
| 160 | } | ||
| 161 | |||
| 162 | my $command = $msg->{data}->{command}; | ||
| 163 | |||
| 164 | $self->log("[$command]") unless $command eq 'graph'; | ||
| 165 | |||
| 166 | # give custom code ref preference over hooks | ||
| 167 | if ($code) { | ||
| 168 | $msg = $code->($msg); | ||
| 169 | } | ||
| 170 | else { | ||
| 171 | # fallback to 'default' command hook | ||
| 172 | $command = 'default' unless (exists $self->hooks->{$command}); | ||
| 173 | |||
| 174 | if (exists $self->hooks->{$command}) { | ||
| 175 | $msg = $self->hooks->{$command}->($msg); | ||
| 176 | } | ||
| 177 | else { | ||
| 178 | $msg->{error} = | ||
| 179 | 'Command "' | ||
| 180 | . $msg->{data}->{command} | ||
| 181 | . '" not defined and no default/fallback found!'; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | $self->_finish_processing($msg); | ||
| 186 | return; | ||
| 187 | }; | ||
| 188 | |||
| 189 | return $self->$orig($wrapper); | ||
| 190 | }; | ||
| 191 | |||
| 192 | |||
| 193 | before 'stop' => sub { | ||
| 194 | my ($self) = @_; | ||
| 195 | |||
| 196 | if (exists $self->job->{message} and $self->job_locked) { | ||
| 197 | $self->unlock_job($self->job->{message}); | ||
| 198 | } | ||
| 199 | |||
| 200 | return; | ||
| 201 | }; | ||
| 202 | |||
| 203 | |||
| 204 | around 'queue' => sub { | ||
| 205 | my ($orig, $self, $queue, $msg, $reply_queue, $exchange) = @_; | ||
| 206 | |||
| 207 | if (ref $self->job->{message} eq 'HASH' | ||
| 208 | and exists $self->job->{message}->{meta}) | ||
| 209 | { | ||
| 210 | for my $key ('user', 'account', 'session') { | ||
| 211 | next if exists $msg->{meta}->{$key}; | ||
| 212 | |||
| 213 | $msg->{meta}->{$key} = $self->job->{message}->{meta}->{$key} | ||
| 214 | if exists $self->job->{message}->{meta}->{$key}; | ||
| 215 | } | ||
| 216 | } | ||
| 217 | |||
| 218 | $self->unlock_job($msg) if $self->job_locked; | ||
| 219 | |||
| 220 | return $self->$orig($queue, $msg, $reply_queue, $exchange); | ||
| 221 | }; | ||
| 222 | |||
| 223 | |||
| 224 | around 'dequeue' => sub { | ||
| 225 | my ($orig, $self, $tag) = @_; | ||
| 226 | |||
| 227 | my $msg = $self->$orig($tag); | ||
| 228 | |||
| 229 | # don't lock and store job if it's an RPC response | ||
| 230 | unless ($tag) { | ||
| 231 | $self->job({ message => $msg }); | ||
| 232 | $self->lock_job($msg); | ||
| 233 | } | ||
| 234 | |||
| 235 | return $msg; | ||
| 236 | }; | ||
| 237 | |||
| 238 | |||
| 239 | before 'ack' => sub { | ||
| 240 | my ($self) = @_; | ||
| 241 | |||
| 242 | $self->job({}); | ||
| 243 | |||
| 244 | return; | ||
| 245 | }; | ||
| 246 | |||
| 247 | |||
| 248 | sub hooks { | ||
| 249 | my ($self, %hooks) = @_; | ||
| 250 | |||
| 251 | $self->_hooks(\%hooks) if %hooks; | ||
| 252 | |||
| 253 | return $self->_hooks; | ||
| 254 | } | ||
| 255 | |||
| 256 | |||
| 257 | sub _finish_processing { | ||
| 258 | my ($self, $msg) = @_; | ||
| 259 | |||
| 260 | # methods may return void to prevent further processing | ||
| 261 | if (ref $msg eq 'HASH') { | ||
| 262 | $self->log("ERROR: " . $msg->{error}) if exists $msg->{error}; | ||
| 263 | |||
| 264 | # log if workflow stops and will be started by event or cron | ||
| 265 | $self->log("waiting for " . $msg->{meta}->{wait_for} . " event/cron") | ||
| 266 | if (exists $msg->{meta} | ||
| 267 | and exists $msg->{meta}->{wait_for} | ||
| 268 | and not $self->wants_reply); | ||
| 269 | |||
| 270 | # log worker and update job if we have to | ||
| 271 | my $status = delete $msg->{status}; | ||
| 272 | if ($self->log_worker_enabled and $self->job_locked) { | ||
| 273 | $self->log_worker($msg); | ||
| 274 | $self->update_job($msg, $status); | ||
| 275 | } | ||
| 276 | |||
| 277 | $self->unlock_job($msg) if $self->job_locked; | ||
| 278 | |||
| 279 | # reply if needed and wanted | ||
| 280 | $self->queue($self->reply_queue, $msg) if $self->wants_reply; | ||
| 281 | } | ||
| 282 | |||
| 283 | # at last, ack message | ||
| 284 | $self->ack; | ||
| 285 | |||
| 286 | return; | ||
| 287 | } | ||
| 288 | |||
| 289 | |||
| 290 | sub lock_job { | ||
| 291 | my ($self, $msg, $mode) = @_; | ||
| 292 | |||
| 293 | # default to locking | ||
| 294 | $mode = (defined $mode and $mode eq 'unlock') ? 'unlock' : 'lock'; | ||
| 295 | |||
| 296 | if ( ref $msg eq 'HASH' | ||
| 297 | and exists $msg->{meta} | ||
| 298 | and ref $msg->{meta} eq 'HASH' | ||
| 299 | and exists $msg->{meta}->{id}) | ||
| 300 | { | ||
| 301 | my $key = 'activejob:' . $msg->{meta}->{id}; | ||
| 302 | my $value = $self->name . ':' . $$; | ||
| 303 | my $success = $self->$mode($key, $value); | ||
| 304 | |||
| 305 | # return error to prevent any further processing in around 'start' wrapper | ||
| 306 | if ($mode eq 'lock') { | ||
| 307 | if ($success) { | ||
| 308 | $self->job_locked(1); | ||
| 309 | return 1; | ||
| 310 | } | ||
| 311 | else { | ||
| 312 | # we need to take into account that KT may be slow | ||
| 313 | # replicating so we need a way to re-try here once after | ||
| 314 | # a set timeout. | ||
| 315 | sleep($self->jobqueue_sync_delay); | ||
| 316 | if ($self->$mode($key, $value)) { | ||
| 317 | $self->job_locked(1); | ||
| 318 | return 1; | ||
| 319 | } | ||
| 320 | $msg->{error} = "job locked by different worker process"; | ||
| 321 | $self->job_locked(0); | ||
| 322 | return; | ||
| 323 | } | ||
| 324 | } | ||
| 325 | else { | ||
| 326 | $self->job_locked(0); | ||
| 327 | return 1; | ||
| 328 | } | ||
| 329 | } | ||
| 330 | |||
| 331 | return 1; | ||
| 332 | } | ||
| 333 | |||
| 334 | |||
| 335 | sub unlock_job { return $_[0]->lock_job($_[1], 'unlock'); } ## no critic | ||
| 336 | |||
| 337 | |||
| 338 | sub dont_log_worker { $_[0]->log_worker_enabled(0); return; } ## no critic | ||
| 339 | |||
| 340 | |||
| 341 | sub get_job { | ||
| 342 | my ($self, $id) = @_; | ||
| 343 | |||
| 344 | unless ($id) { | ||
| 345 | carp "Job ID missing"; | ||
| 346 | return; | ||
| 347 | } | ||
| 348 | |||
| 349 | my $old_db = $self->couchdb->db; | ||
| 350 | $self->couchdb->db($self->jobqueue_db); | ||
| 351 | my $job = $self->couchdb->get_doc({ id => $id }); | ||
| 352 | $self->couchdb->db($old_db); | ||
| 353 | |||
| 354 | unless ($job) { | ||
| 355 | carp "job ID not existing: $id"; | ||
| 356 | return; | ||
| 357 | } | ||
| 358 | |||
| 359 | # store job in attribute | ||
| 360 | $self->job($job); | ||
| 361 | |||
| 362 | return $job; | ||
| 363 | } | ||
| 364 | |||
| 365 | |||
| 366 | sub create_job { | ||
| 367 | my ($self, $msg) = @_; | ||
| 368 | |||
| 369 | unless (ref $msg eq 'HASH') { | ||
| 370 | carp "msg must be a HASH"; | ||
| 371 | return; | ||
| 372 | } | ||
| 373 | |||
| 374 | # set testmode when running in debug mode | ||
| 375 | $msg->{data}->{options}->{testmode} = 1 if $self->debug; | ||
| 376 | |||
| 377 | # kill duplicate identical jobs with a hashsum over the input data | ||
| 378 | # and a 2 min caching time | ||
| 379 | my $created = time; | ||
| 380 | my $cached = DateTime->from_epoch(epoch => $created); | ||
| 381 | $cached->truncate(to => 'minute'); | ||
| 382 | $cached->set_minute($cached->minute - ($cached->minute % 2)); | ||
| 383 | my $dumper = Data::Dumper->new([ $msg->{data} ]); | ||
| 384 | $dumper->Terse(1); | ||
| 385 | $dumper->Sortkeys(1); | ||
| 386 | $dumper->Indent(0); | ||
| 387 | $dumper->Deepcopy(1); | ||
| 388 | my $id = md5_hex($dumper->Dump . $cached); | ||
| 389 | |||
| 390 | # return if job ID exists | ||
| 391 | my $old_db = $self->couchdb->db; | ||
| 392 | $self->couchdb->db($self->jobqueue_db); | ||
| 393 | my $job = $self->couchdb->get_doc({ id => $id }); | ||
| 394 | if ($job) { | ||
| 395 | $self->log("found duplicate job: " . $job->{_id}); | ||
| 396 | $self->job($job); | ||
| 397 | return $job, 1; | ||
| 398 | } | ||
| 399 | |||
| 400 | $msg->{meta}->{id} = $id; | ||
| 401 | $job = { | ||
| 402 | _id => $id, | ||
| 403 | created => $created, | ||
| 404 | updated => $created, | ||
| 405 | message => $msg, | ||
| 406 | status => 'new', | ||
| 407 | }; | ||
| 408 | |||
| 409 | $id = $self->couchdb->put_doc({ doc => $job }); | ||
| 410 | $self->couchdb->db($old_db); | ||
| 411 | |||
| 412 | unless ($id) { | ||
| 413 | carp 'creating job failed: ' . $self->couchdb->error; | ||
| 414 | return; | ||
| 415 | } | ||
| 416 | |||
| 417 | $self->job($job); | ||
| 418 | $self->lock_job($msg); | ||
| 419 | |||
| 420 | return $job; | ||
| 421 | } | ||
| 422 | |||
| 423 | |||
| 424 | sub start_job { | ||
| 425 | my ($self, $workflow, $options, $priority) = @_; | ||
| 426 | |||
| 427 | unless ($workflow) { | ||
| 428 | carp 'workflow not defined'; | ||
| 429 | return; | ||
| 430 | } | ||
| 431 | |||
| 432 | $options = {} unless $options; | ||
| 433 | |||
| 434 | my $frame = { | ||
| 435 | meta => { | ||
| 436 | lang => 'en', | ||
| 437 | user => $options->{user_id} || undef, | ||
| 438 | }, | ||
| 439 | data => { | ||
| 440 | command => $workflow, | ||
| 441 | options => $options, | ||
| 442 | }, | ||
| 443 | }; | ||
| 444 | |||
| 445 | # add priority, default normal | ||
| 446 | $frame->{meta}->{priority} = $priority | ||
| 447 | if defined $priority and $priority =~ m/^(high|low)$/; | ||
| 448 | |||
| 449 | # tell the new job who created it | ||
| 450 | $frame->{meta}->{created_by} = $self->job->{message}->{meta}->{id} | ||
| 451 | if exists $self->job->{message}->{meta}->{id}; | ||
| 452 | |||
| 453 | $self->log("starting '$workflow' workflow with:\n" . $self->dump($frame)) | ||
| 454 | if $self->debug; | ||
| 455 | $self->queue('workflow', $frame); | ||
| 456 | |||
| 457 | return; | ||
| 458 | } | ||
| 459 | |||
| 460 | |||
| 461 | sub update_job { | ||
| 462 | my ($self, $msg, $status) = @_; | ||
| 463 | |||
| 464 | unless ((ref($msg) eq 'HASH') | ||
| 465 | and (exists $msg->{meta} and exists $msg->{meta}->{id})) | ||
| 466 | { | ||
| 467 | $self->log("not a JOB, just a message, nothing to see here") | ||
| 468 | if $self->debug; | ||
| 469 | return; | ||
| 470 | } | ||
| 471 | |||
| 472 | my $old_db = $self->couchdb->db; | ||
| 473 | $self->couchdb->db($self->jobqueue_db); | ||
| 474 | my $job = $self->get_job($msg->{meta}->{id}); | ||
| 475 | $self->couchdb->db($old_db); | ||
| 476 | |||
| 477 | return unless $job; | ||
| 478 | |||
| 479 | $self->job($job); | ||
| 480 | |||
| 481 | $job->{updated} = time; | ||
| 482 | $job->{status} = $status || $job->{status}; | ||
| 483 | $job->{message} = $msg; | ||
| 484 | |||
| 485 | $old_db = $self->couchdb->db; | ||
| 486 | $self->couchdb->db($self->jobqueue_db); | ||
| 487 | ($job->{_id}, $job->{_rev}) = $self->couchdb->put_doc({ doc => $job }); | ||
| 488 | $self->couchdb->db($old_db); | ||
| 489 | |||
| 490 | if ($self->couchdb->has_error) { | ||
| 491 | carp 'updating job failed! id: ' | ||
| 492 | . $msg->{meta}->{id} | ||
| 493 | . ', error: ' | ||
| 494 | . $self->couchdb->error; | ||
| 495 | return; | ||
| 496 | } | ||
| 497 | |||
| 498 | $self->job($job); | ||
| 499 | |||
| 500 | return $job; | ||
| 501 | } | ||
| 502 | |||
| 503 | |||
| 504 | sub job_done { | ||
| 505 | my ($self, $msg) = @_; | ||
| 506 | |||
| 507 | return $self->update_job($msg, 'done'); | ||
| 508 | } | ||
| 509 | |||
| 510 | |||
| 511 | sub job_failed { | ||
| 512 | my ($self, $msg) = @_; | ||
| 513 | |||
| 514 | return $self->update_job($msg, 'failed'); | ||
| 515 | } | ||
| 516 | |||
| 517 | |||
| 518 | sub job_pending { | ||
| 519 | my ($self, $msg) = @_; | ||
| 520 | |||
| 521 | return $self->update_job($msg, 'pending'); | ||
| 522 | } | ||
| 523 | |||
| 524 | |||
| 525 | sub log_worker { | ||
| 526 | my ($self, $msg) = @_; | ||
| 527 | |||
| 528 | # only log worker if message has a job ID | ||
| 529 | return $msg unless exists $msg->{meta} and exists $msg->{meta}->{id}; | ||
| 530 | |||
| 531 | my $worker = | ||
| 532 | (exists $msg->{meta}->{worker}) | ||
| 533 | ? $msg->{meta}->{worker} | ||
| 534 | : $self->name; | ||
| 535 | |||
| 536 | $self->log("logging worker '$worker'") if $self->debug; | ||
| 537 | |||
| 538 | # no log yet? create it | ||
| 539 | unless (exists $msg->{meta}->{log}) { | ||
| 540 | push(@{ $msg->{meta}->{log} }, $worker); | ||
| 541 | return $msg; | ||
| 542 | } | ||
| 543 | |||
| 544 | # last worker same as current? ignore | ||
| 545 | unless ($msg->{meta}->{log}->[-1] eq $worker) { | ||
| 546 | push(@{ $msg->{meta}->{log} }, $worker); | ||
| 547 | } | ||
| 548 | |||
| 549 | return $msg; | ||
| 550 | } | ||
| 551 | |||
| 552 | |||
| 553 | sub find_job { | ||
| 554 | my ($self, $how, $key, $all) = @_; | ||
| 555 | |||
| 556 | my @result; | ||
| 557 | my $old_db = $self->couchdb->db; | ||
| 558 | $self->couchdb->db($self->jobqueue_db); | ||
| 559 | @result = $self->couchdb->get_view_array({ | ||
| 560 | view => "find/$how", | ||
| 561 | opts => { | ||
| 562 | key => $key, | ||
| 563 | include_docs => 'true', | ||
| 564 | }, | ||
| 565 | }); | ||
| 566 | $self->couchdb->db($old_db); | ||
| 567 | |||
| 568 | if (@result) { | ||
| 569 | return @result if $all; | ||
| 570 | |||
| 571 | $self->job($result[0]); | ||
| 572 | return $result[0]; | ||
| 573 | } | ||
| 574 | |||
| 575 | return; | ||
| 576 | } | ||
| 577 | |||
| 578 | |||
| 579 | sub find_all_jobs { | ||
| 580 | my ($self, @args) = @_; | ||
| 581 | |||
| 582 | return $self->find_job(@args[ 0, 1 ], 'all'); | ||
| 583 | } | ||
| 584 | |||
| 585 | |||
| 586 | sub stop_here { | ||
| 587 | my ($self) = @_; | ||
| 588 | |||
| 589 | $self->dont_reply if exists $self->job->{message}->{meta}->{id}; | ||
| 590 | |||
| 591 | return; | ||
| 592 | } | ||
| 593 | |||
| 594 | |||
| 595 | sub recalculate { | ||
| 596 | my ($self) = @_; | ||
| 597 | |||
| 598 | unless (ref $self->job eq 'HASH') { | ||
| 599 | carp 'needs a job HashRef in the job attribute!'; | ||
| 600 | return; | ||
| 601 | } | ||
| 602 | |||
| 603 | my $data = $self->job->{message}->{data}->{options}; | ||
| 604 | |||
| 605 | unless (exists $data->{ $self->items_key } | ||
| 606 | and scalar(@{ $data->{ $self->items_key } || [] }) > 0) | ||
| 607 | { | ||
| 608 | carp 'no ' . $self->items_key . ' found, can\'t recalculate'; | ||
| 609 | return; | ||
| 610 | } | ||
| 611 | |||
| 612 | $self->log("re-calculating total!"); | ||
| 613 | |||
| 614 | my $old_total = $data->{total} || 0; | ||
| 615 | |||
| 616 | my $basket = Basket::Calc->new( | ||
| 617 | currency => ($data->{currency} || 'USD'), | ||
| 618 | debug => $self->debug, | ||
| 619 | ); | ||
| 620 | |||
| 621 | # add tax if applicable | ||
| 622 | $basket->tax($data->{tax} / 100) | ||
| 623 | if (exists $data->{tax} and $data->{tax}); | ||
| 624 | |||
| 625 | # add item prices | ||
| 626 | for my $item (@{ $data->{ $self->items_key } }) { | ||
| 627 | next unless exists $item->{amount}; | ||
| 628 | next unless looks_like_number($item->{amount}); | ||
| 629 | next unless $item->{amount} > 0; | ||
| 630 | |||
| 631 | $basket->add_item({ | ||
| 632 | price => $item->{amount}, | ||
| 633 | quantity => ($item->{interval} || 1), | ||
| 634 | }); | ||
| 635 | } | ||
| 636 | |||
| 637 | # add discounts if applicable | ||
| 638 | # TODO: support for other discount types, e.g. fixed amount | ||
| 639 | $basket->add_discount( | ||
| 640 | { type => 'percent', value => $data->{discount} / 100 }) | ||
| 641 | if (exists $data->{discount} and $data->{discount}); | ||
| 642 | |||
| 643 | # calculate and update totals | ||
| 644 | my $result = $basket->calculate; | ||
| 645 | $data->{total} = $result->{value}; | ||
| 646 | |||
| 647 | if ($result->{tax_amount}) { | ||
| 648 | $data->{tax_amount} = $result->{tax_amount}; | ||
| 649 | $data->{net} = $result->{net}; | ||
| 650 | } | ||
| 651 | |||
| 652 | $self->log("new total: " | ||
| 653 | . $data->{total} . ' ' | ||
| 654 | . $data->{currency} | ||
| 655 | . ' was: ' | ||
| 656 | . $old_total . ' ' | ||
| 657 | . $data->{currency}); | ||
| 658 | |||
| 659 | return 1; | ||
| 660 | } | ||
| 661 | |||
| 662 | |||
| 663 | sub find_item_index { | ||
| 664 | my ($self, $data, $item) = @_; | ||
| 665 | |||
| 666 | for (my $i = 0; $i <= $#{ $data->{ $self->items_key } }; $i++) { | ||
| 667 | if ($data->{ $self->items_key }->[$i]->{ $self->item_key } eq $item) { | ||
| 668 | return $i; | ||
| 669 | } | ||
| 670 | } | ||
| 671 | |||
| 672 | return; | ||
| 673 | } | ||
| 674 | |||
| 675 | |||
| 676 | sub remove_items { | ||
| 677 | my ($self, $data, @items_to_remove) = @_; | ||
| 678 | |||
| 679 | my @removed; | ||
| 680 | my $dest_key = 'removed_' . $self->items_key; | ||
| 681 | |||
| 682 | foreach (@items_to_remove) { | ||
| 683 | my $i = $self->find_item_index($data, $_); | ||
| 684 | |||
| 685 | # remove this item from job | ||
| 686 | push(@removed, splice(@{ $data->{ $self->items_key } }, $i, 1)) | ||
| 687 | if defined $i; | ||
| 688 | } | ||
| 689 | |||
| 690 | # store removed items in removed_items key for record | ||
| 691 | if (exists $data->{$dest_key} and ref $data->{$dest_key} eq 'ARRAY') { | ||
| 692 | push(@{ $data->{$dest_key} }, $_) for (@removed); | ||
| 693 | } | ||
| 694 | elsif (@removed) { | ||
| 695 | $data->{$dest_key} = \@removed; | ||
| 696 | } | ||
| 697 | |||
| 698 | return @removed; | ||
| 699 | } | ||
| 700 | |||
| 701 | 1; | ||
| 702 | |||
| 703 | __END__ | ||