Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Catalyst: Users

Additional scripts accessing the Model and Config?

 

 

Catalyst users RSS feed   Index | Next | Previous | View Threaded


luisemunoz at gmail

Jun 7, 2012, 3:17 PM

Post #1 of 9 (325 views)
Permalink
Additional scripts accessing the Model and Config?

Hi there,

During the last couple of days I've been trying various ways to do this, but it all seems more complicated than it should be.

I have a Catalyst app that implements a number of controllers that modify data through a model. Let's say it's a DBIC model for simplicity. I need to write various sets of scripts to be run from the command line, that will access the data and produce reports that are to be fed to other processes.

What I would like is to have a simple construct where the body of the script would be something like this

#!/usr/bin/perl

use strict;
use warnings;

use Catalyst::ScriptRunner;
Catalyst::ScriptRunner->run('My::Fancy::App', 'Dump');

exit 0;

And then in My::Fancy::App::Script::Dump.pm I would have the main body of the script, in a way similar to what the _create.pl et al scripts operate.

The part that's stumping me is how to go within Dump.pm about obtaining a $c that allows me to get to the configuration (taken from the same place where the web application takes it) and the model.

My current (simplified) contents of the Dump.pm file are as follows:

package My::Fancy::App::Script::Dump;
use Moose;
use MooseX::Types::Moose qw/Bool Str/;
use namespace::autoclean;

has output => (
traits => [qw(Getopt)],
cmd_aliases => 'o',
isa => Str,
is => 'ro',
documentation => 'Output name for the generated file(s)',
);

sub run {
my ($self) = @_;

printf ( "some_custom_data=%s\n", $self->my_custom_db_config ( 'custom.data' ));

die ( "Error: Must specify things to process using --thing\n" )
unless $self->tld;
$self->_getopt_full_usage if !$self->ARGV->[0];
$self->_getopt_full_usage ();
}

__PACKAGE__->meta->make_immutable;

Where ->my_custom_db_config is a method that fetches process-specific configuration from the Schema. I would like to access things like ->log, ->model and the utility methods that I use in the Controllers from within the script. And of course, I would like to use the same configuration file.

Has anybody dealt with a similar problem before? Or am I on my own :-)

Best regards

-lem
_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


bobtfish at bobtfish

Jun 8, 2012, 10:15 AM

Post #2 of 9 (319 views)
Permalink
Re: Additional scripts accessing the Model and Config? [In reply to]

On 7 Jun 2012, at 23:17, Luis Muñoz wrote:

> The part that's stumping me is how to go within Dump.pm about obtaining a $c that allows me to get to the configuration (taken from the same place where the web application takes it) and the model.

You don't.

$c implies a web request context, which you don't have.

You can, however, call things as class methods, so:

$self->application_name->model('DB')->schema->foo

should work from inside your script, for example.

Cheers
t0m


_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


luisemunoz at gmail

Jun 8, 2012, 11:05 AM

Post #3 of 9 (310 views)
Permalink
Re: Additional scripts accessing the Model and Config? [In reply to]

On Jun 8, 2012, at 1:15 PM, Tomas Doran wrote:

> You don't.
> $c implies a web request context, which you don't have.

That explains a lot :-)

> You can, however, call things as class methods, so:
>
> $self->application_name->model('DB')->schema->foo
>
> should work from inside your script, for example.

But still, I would like to have access to the configuration data (for instance, how to setup the database/ldap/whatever Model, etc). Does that happen as well when using the class method?

Best regards

-lem


_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


domm at cpan

Jun 8, 2012, 11:23 AM

Post #4 of 9 (312 views)
Permalink
Re: Additional scripts accessing the Model and Config? [In reply to]

Hi!

On Fri, Jun 08, 2012 at 02:05:31PM -0400, Luis Muñoz wrote:

> But still, I would like to have access to the configuration data (for
> instance, how to setup the database/ldap/whatever Model, etc). Does
> that happen as well when using the class method?

We pack our config (which basically is a big hash) in a class. To
provide the config to Catalyst, we use the class in my_app.pl (which is
like my_app.(yml|json|ini|...). If we need the config in some other
scripts, we just use the class there.

The non-Web code lives in classes (eg DBIC) that are used via Models in
Catalyst, or directly (or some Model-like wrappers, if it needs to be)
in other contexts.

Greetings,
domm


--
#!/usr/bin/perl http://domm.plix.at
for(ref bless{},just'another'perl'hacker){s-:+-$"-g&&print$_.$/}

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


will.trillich at serensoft

Jun 8, 2012, 11:52 AM

Post #5 of 9 (322 views)
Permalink
Re: Additional scripts accessing the Model and Config? [In reply to]

Example of pulling the standard catalyst config into a
"command-line-type-usage" script (works nicely from the scripts/ directory):


use FindBin qw($Bin);
use Path::Class;
use lib dir($Bin, '..', 'lib')->stringify; # cross-platform

# Now that we have the local LIB directory in our @INC path, get the module:
use MyApp::Schema::DB;
use Config::General;

use strict;

# Get info from the config file
my $conf = new Config::General( file( $Bin, '..', 'myapp.conf' ) );
my %conf = $conf->getall;

my $dbi = $conf{'Model::MyApp'}{connect_info};
my @dbi = map{ $dbi->{$_} } qw/dsn user password/;

# Connect to database
my $schema = MyApp::Schema::DB->connect( @dbi );

#...etc




On Fri, Jun 8, 2012 at 1:23 PM, Thomas Klausner <domm [at] cpan> wrote:

> Hi!
>
> On Fri, Jun 08, 2012 at 02:05:31PM -0400, Luis Muñoz wrote:
>
> > But still, I would like to have access to the configuration data (for
> > instance, how to setup the database/ldap/whatever Model, etc). Does
> > that happen as well when using the class method?
>
> We pack our config (which basically is a big hash) in a class. To
> provide the config to Catalyst, we use the class in my_app.pl (which is
> like my_app.(yml|json|ini|...). If we need the config in some other
> scripts, we just use the class there.
>
> The non-Web code lives in classes (eg DBIC) that are used via Models in
> Catalyst, or directly (or some Model-like wrappers, if it needs to be)
> in other contexts.
>
> Greetings,
> domm
>
>
> --
> #!/usr/bin/perl http://domm.plix.at
> for(ref bless{},just'another'perl'hacker){s-:+-$"-g&&print$_.$/}
>
> _______________________________________________
> List: Catalyst [at] lists
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst [at] lists/
> Dev site: http://dev.catalyst.perl.org/
>



--
Will Trillich :: 812.454.6431

“Waiting for perfect is never as smart as making progress.” -- Seth Godin


bobtfish at bobtfish

Jun 8, 2012, 2:04 PM

Post #6 of 9 (308 views)
Permalink
Re: Additional scripts accessing the Model and Config? [In reply to]

On 8 Jun 2012, at 19:05, Luis Muñoz wrote:

> he configuration data (for instance, how to setup the database/ldap/whatever Model, etc). Does that happen as well when using the class method?

Sorry - I'm confused - how does calling into the app to get stuff as $self->application_name->model('Foo')->some_method(@stuff)

not fix your problem?

Cheers
t0m


_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


luisemunoz at gmail

Jun 8, 2012, 2:33 PM

Post #7 of 9 (308 views)
Permalink
Re: Additional scripts accessing the Model and Config? [In reply to]

On Jun 8, 2012, at 5:04 PM, Tomas Doran wrote:

> Sorry - I'm confused - how does calling into the app to get stuff as $self->application_name->model('Foo')->some_method(@stuff)

How does the Model, called like that, would know the details of where it is supposed to connect? Things like the DSN, username, password, etc are in config files whose reading is triggered "magically" by the Catalyst infrastructure. However that is not present when running as a script.

This is the reason for my question.

-lem



_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


geekout at gmail

Jun 8, 2012, 4:36 PM

Post #8 of 9 (315 views)
Permalink
Re: Additional scripts accessing the Model and Config? [In reply to]

Hello,

On Fri, Jun 8, 2012 at 11:33 AM, Luis Muñoz <luisemunoz [at] gmail> wrote:

>
> On Jun 8, 2012, at 5:04 PM, Tomas Doran wrote:
>
> > Sorry - I'm confused - how does calling into the app to get stuff as
> $self->application_name->model('Foo')->some_method(@stuff)
>
> How does the Model, called like that, would know the details of where it
> is supposed to connect? Things like the DSN, username, password, etc are in
> config files whose reading is triggered "magically" by the Catalyst
> infrastructure. However that is not present when running as a script.
>

I think the basic confusion is in that you are wanting Catalyst to "own"
your Model, Config, and Log and be able to use them from a script when
really what you want is to have those separate from Catalyst but used in
both Catalyst and a script. DBIC is essentially doing this with the Model
already. Checkout Log4perl or something similar for the Log, check out
Config::JFDI for the config.

I rely on an environment variable being set, both for Catalyst and my
scripts. Here is an example of something at the top of one of my scripts:

my $path = $ENV{MY_FANCY_APP_HOME} || die "Must set env";

use Config::JFDI;
my $config_jfdi = Config::JFDI->new( name => 'my_fancy_app', path => $path
);
my $config = $config_jfdi->get;

use Log::Log4perl;
Log::Log4perl->init_once( $path . '/log.conf' );
my $log = Log::Log4perl->get_logger('MyFancyApp');

use MyFancyApp::Model::DB;
my $model = MyFancyApp::Model::DB->new( $config->{'Model::DB'} );

# Log our foo config item if we have a row in our foo table
$log->info("Foo: " . $config->{foo}) if
$model->resultset('FooTable')->count;


And in your my_fancy_app.yaml config:

foo: 'bar'
'Model::DB':
schema_class: 'MyFancyApp::Schema'
connect_info:
dsn: 'dbi:mysql:dbname=fancyapp'
user: 'root'
password: 'secretpass'


Then you have a $log, $config, and $model, all of which should return the
same things as their respective $c context methods ($c->log, $c->config,
and $c->model).

You can, of course, hide all these details from your script if you like but
that should get you there.


> This is the reason for my question.
>
> -lem
>
>
>
> _______________________________________________
> List: Catalyst [at] lists
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst [at] lists/
> Dev site: http://dev.catalyst.perl.org/
>



--
~Tyler


catalyst at mikeraynham

Jun 10, 2012, 8:18 AM

Post #9 of 9 (309 views)
Permalink
Re: Additional scripts accessing the Model and Config? [In reply to]

On 08/06/12 19:05, Luis Muñoz wrote:
>
> But still, I would like to have access to the configuration data (for instance, how to setup the database/ldap/whatever Model, etc). Does that happen as well when using the class method?
>

As W. Tyler Gee stated, you appear to have fallen into the trap of
assuming that Catalyst owns your application configuration, and that you
therefore have to access it via the Catalyst context object. Catalyst
makes it easy to access the application configuration from within
Catalyst, which is perhaps why this trap can sneak up on you.

Dave Rolsky wrote an interesting blog article about how he uses
Catalyst, and in it, he talks about the use of configuration data:

http://blog.urth.org/2009/08/how-i-use-catalyst.html

The R2 project to which he refers is available on GitHub:

https://github.com/autarch/R2

If you look at lib/R2.pm, you'll see that it pulls configuration data in
from R2::Config - an application config module. I found looking through
the R2 code very informative.


Regards,

Mike

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/

Catalyst users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.