Last active
April 30, 2017 01:52
-
-
Save xenoterracide/5372703 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package OurApp::BreadBoard; | |
| use Moose; | |
| use Class::Load 0.20 'load_class'; | |
| use Bread::Board::Declare; | |
| has config_file => ( # you can override the location in your .psgi by passing this param to new | |
| isa => 'Str', | |
| is => 'ro', | |
| value => 'my.conf', | |
| ); | |
| has config => ( # not actually using overridden files but allows use of env vars, you could swap for Config::Merge, etc. | |
| isa => 'HashRef', | |
| is => 'ro', | |
| lifecycle => 'Singleton', | |
| dependencies => ['config_file'], | |
| block => sub { | |
| my $s = shift; | |
| my $cfg | |
| = load_class('Config::Any')->load_files({ | |
| flatten_to_hash => 1, | |
| use_ext => 1, | |
| files => [ $s->param('config_file') ], | |
| General => { | |
| -InterPolateEnv => 1, | |
| -InterPolateVars => 1, | |
| }, | |
| }); | |
| return $cfg->{ $s->param('config_file') }; | |
| }, | |
| ); | |
| has 'zmq_service_discovery' => ( | |
| is => 'ro', | |
| isa => 'OurApp::ServiceDiscovery', | |
| dependencies => ['config'], | |
| block => sub { | |
| my $s = shift; | |
| # get our class from the config file | |
| return load_class( $s->param('config')->{service_discover}{class} )->new; | |
| }, | |
| ); | |
| has 'some_worker' => ( | |
| is => 'ro', | |
| isa => 'OurApp::Some::Worker', | |
| dependencies => { | |
| service_discoverer => 'zmq_service_discovery' | |
| } | |
| ); | |
| # regular script launching the worker | |
| use OurApp::BreadBoard; | |
| my $bb = OurApp::BreadBoard->new( name=>'SomeWorker' ); | |
| my $app = $bb->resolve( service => 'Apps/some_worker' ); | |
| $app->run; | |
| # a "meta" script that starts all components, each running in a fork | |
| # this should also start the web front/backend (via plackup) | |
| use OurApp::BreadBoard; | |
| my $bb = OurApp::BreadBoard->new( | |
| name=>'MetaRunner', | |
| environment=>'devel' | |
| ); | |
| foreach my $app (qw[some_worker]) { | |
| if (my $pid = fork()) { | |
| # parent, manage childs etc | |
| } | |
| else { | |
| my $app = $bb->resolve( service => 'Apps/'.$app ); | |
| $app->run; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment