Advertisement
Guest User

Untitled

a guest
Jan 27th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. package MyApp::TheSchwartz::Client;
  2.  
  3. use TheSchwartz;
  4. sub theschwartz {
  5. my $theschwartz = TheSchwartz->new(
  6. databases => [ {
  7. dsn => 'dbi:mysql:theschwartz',
  8. user => 'user',
  9. pass => 'pass',
  10. } ],
  11. verbose => 1,
  12. );
  13. return $theschwartz;
  14. }
  15.  
  16. package MyApp::TheSchwartz::Worker::Test;
  17.  
  18. use base qw( TheSchwartz::Moosified::Worker );
  19. use MyApp::Model::DB; # Catalyst DB connect_info
  20. use MyApp::Schema; # Catalyst DB schema
  21.  
  22. sub work {
  23. my $class = shift;
  24. my $job = shift;
  25. my ($args) = $job->arg;
  26. my ($arg1, $arg2) = @$args;
  27.  
  28. # re-use Catalyst DB schema
  29. my $connect_info = MyApp::Model::DB->config->{connect_info};
  30. my $schema = MyApp::Schema->connect($connect_info);
  31.  
  32. # do the heavy lifting
  33.  
  34. $job->completed();
  35. }
  36.  
  37. use MyApp::TheSchwartz::Client qw/theschwartz/; # db connection
  38. use MyApp::TheSchwartz::Worker::Test;
  39. use Parallel::Prefork;
  40.  
  41. my $client = theschwartz();
  42.  
  43. my $pm = Parallel::Prefork->new({
  44. max_workers => 16,
  45. trap_signals => {
  46. TERM => 'TERM',
  47. HUP => 'TERM',
  48. USR1 => undef,
  49. }
  50. });
  51.  
  52. while ($pm->signal_received ne 'TERM') {
  53. $pm->start and next;
  54.  
  55. $client->can_do('MyApp::TheSchwartz::Worker::Test');
  56. my $delay = 10; # When no job is available, the working process will sleep for $delay seconds
  57. $client->work( $delay );
  58.  
  59. $pm->finish;
  60. }
  61. $pm->wait_all_children();
  62.  
  63. use MyApp::TheSchwartz::Client qw/theschwartz/;
  64. $client = theschwartz();
  65. $client->insert(‘MyApp::TheSchwartz::Worker::Test’, [ $arg1, $arg2 ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement