FAQ
Hi guys

I am new to Catalyst and have a question :)

I'd like the ability to run some of the code of my website from the
command line ( a cron job ).

Usually this would be achieved by a person logging into the website and
clicking on a particular button which hits a controller.

I saw that there is a myapp_test.pl that can call a particular address,
so i tried this:

This works fine:
$> perl script/myapp_test.pl /login

I can see the HTML get printed out on my screen.

But when I try to get to a deeper URL it does not work:
perl script/isrms_test.pl /my/other/url

I think this is because there is authentication on the site?

Is this the way I should be going about trying to do this? Is there a
better way to do this?

Thanks

Jon

Search Discussions

  • Tai Hsiao at Nov 3, 2010 at 4:52 am
    Hi Jon,

    I haven't tried this myself, but check out the Catalyst tutorial. It
    looks like it shows exactly what you need:


    http://search.cpan.org/~zarquon/Catalyst-Manual-5.8005/lib/Catalyst/Manual/Tutorial/08_Testing.pod

    -Tai
    On Tue, Nov 2, 2010 at 9:08 PM, Jonathon Soong wrote:

    Hi guys

    I am new to Catalyst and have a question :)

    I'd like the ability to run some of the code of my website from the
    command line ( a cron job ).

    Usually this would be achieved by a person logging into the website and
    clicking on a particular button which hits a controller.

    I saw that there is a myapp_test.pl that can call a particular address,
    so i tried this:

    This works fine:
    $> perl script/myapp_test.pl /login

    I can see the HTML get printed out on my screen.

    But when I try to get to a deeper URL it does not work:
    perl script/isrms_test.pl /my/other/url

    I think this is because there is authentication on the site?

    Is this the way I should be going about trying to do this? Is there a
    better way to do this?

    Thanks

    Jon


    _______________________________________________
    List: Catalyst@lists.scsys.co.uk
    Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
    Searchable archive:
    http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
    Dev site: http://dev.catalyst.perl.org/
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20101102/117d5da5/attachment.htm
  • David Schmidt at Nov 3, 2010 at 6:31 am

    On Wed, Nov 3, 2010 at 5:08 AM, Jonathon Soong wrote:
    Hi guys

    I am new to Catalyst and have a question :)

    I'd like the ability to run some of the code of my website from the
    command line ( a cron job ).

    Usually this would be achieved by a person logging into the website and
    clicking on a particular button which hits a controller.

    I saw that there is a myapp_test.pl that can call a particular address,
    so i tried this:

    This works fine:
    $> ?perl script/myapp_test.pl /login

    I can see the HTML get printed out on my screen.

    But when I try to get to a deeper URL it does not work:
    ?perl script/isrms_test.pl /my/other/url

    I think this is because there is authentication on the site?

    Is this the way I should be going about trying to do this? Is there a
    better way to do this?

    Thanks

    Jon


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

    What kind of code do you actually want to run?
    Perhaps it would be smarter to put that code into the model and then
    run a perl script from cron that makes use of that model thus
    bypassing the HTML authentication (which might be ok considering the
    code is run from the system)

    david
  • Jonathon Soong at Nov 3, 2010 at 6:40 am
    Hi
    What kind of code do you actually want to run?
    Perhaps it would be smarter to put that code into the model and then
    run a perl script from cron that makes use of that model thus
    bypassing the HTML authentication (which might be ok considering the
    code is run from the system)
    Yes I realise now it might make more sense in the Model, but at the moment it is in the Controller (it is someone else's code, so more difficult for me to refactor).

    There are two questions I have then:
    1. How do you call a Model's function from the command line?
    2. Is there no way to call controller methods that require authentication?

    Thanks


    Jon
  • Octavian Rasnita at Nov 3, 2010 at 6:58 am
    From: "Jonathon Soong" <jon@investmentscience.com.au>
    Yes I realise now it might make more sense in the Model, but at the moment
    it is in the Controller (it is someone else's code, so more difficult for
    me to refactor).

    There are two questions I have then:
    1. How do you call a Model's function from the command line?
    2. Is there no way to call controller methods that require authentication?

    Thanks


    Jon

    The model can be just a thin wrapper for your non-catalyst modules like
    MyApp::Model::DB could be for your DBIC schema.
    And you can use your non-catalyst module from a non-catalyst script that can
    be ran by a cron job.

    If the code is in the controller and you can access it by web only, you can
    create a script that accesses your running Catalyst application, does the
    login (submits the login form with the necessary username and password),
    saves the cookie and resends that cookie for all the requests you need to do
    after logging in.

    Octavian
  • David Schmidt at Nov 3, 2010 at 7:10 am

    On Wed, Nov 3, 2010 at 7:40 AM, Jonathon Soong wrote:
    Hi
    What kind of code do you actually want to run?
    Perhaps it would be smarter to put that code into the model and then
    run a perl script from cron that makes use of that model thus
    bypassing the HTML authentication (which might be ok considering the
    code is run from the system)
    Yes I realise now it might make more sense in the Model, but at the moment it is in the Controller (it is someone else's code, so more difficult for me to refactor).

    There are two questions I have then:
    1. How do you call a Model's function from the command line?
    2. Is there no way to call controller methods that require authentication?

    Thanks


    Jon


    _______________________________________________
    List: Catalyst@lists.scsys.co.uk
    Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
    Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
    Dev site: http://dev.catalyst.perl.org/
    1) put code in an app independent module, your model uses that module
    and so can you.
    2) http://search.cpan.org/~petdance/WWW-Mechanize-1.66/lib/WWW/Mechanize.pm

    ... oh and what Octavian said.
  • Eden Cardim at Nov 3, 2010 at 1:08 pm
    "Jonathon" == Jonathon Soong writes:
    Jonathon> Yes I realise now it might make more sense in the Model,
    Jonathon> but at the moment it is in the Controller (it is someone
    Jonathon> else's code, so more difficult for me to refactor).

    That's barely an excuse, if you can run the code, you can read the code,
    and if you can read the code, you can at least copy it into a model and
    drop the usage of the original controller, unless there are legal
    constraints in place which forbid that specific practice.

    Jonathon> There are two questions I have then: 1. How do you call a
    Jonathon> Model's function from the command line? 2. Is there no
    Jonathon> way to call controller methods that require
    Jonathon> authentication?

    for the model case, you can

    use MyApp;
    MyApp->model('Foo')->bar;

    for the authentication case, you're very probably relying on the
    authentication API inside the controller code, which for most cases
    won't work at all in a non-web environment. You'd have to mimic the
    entire environment, which is a lot more trouble than just factoring your
    code correctly.

    --
    Eden Cardim Need help with your Catalyst or DBIx::Class project?
    Code Monkey http://www.shadowcat.co.uk/catalyst/
    Shadowcat Systems Ltd. Want a managed development or deployment platform?
    http://blog.edencardim.com/ http://www.shadowcat.co.uk/servers/

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcatalyst @
categoriescatalyst, perl
postedNov 3, '10 at 4:08a
activeNov 3, '10 at 1:08p
posts7
users5
websitecatalystframework.org
irc#catalyst

People

Translate

site design / logo © 2022 Grokbase