Hello,

I have a nice mojolicious application, and one of it's features is to 
import data from an uploaded file. This works out fine except when the file 
is too large and the web browser session times out. I have put in place a 
mechanism where this failure is handled gracefully, but it still leaves me 
with the problem of having to chunk up CSV files into potentially tens of 
chunks and then uploading them manually. Unfortunately much of the import 
code is also shared with the client code. For example, when something is 
saved, validation occurs and a change log is automatically kept. Therefore 
separating out the import code is not feasible. 

Unless somebody can come up with a better option, I was wondering if it 
would be possible to essentially create a fake $self in a command line 
utility and then use it to call the client subroutines. I am by no means a 
perl guru, so I am hoping that somebody can help me achieve my goal. I 
believe my main issue is with the database handle and logging. Basically I 
am not savy enough with perl to even begin to address the issues. An 
example of the subroutine I would like to call is:

sub ImportClients {
  my ($Self, $Type, $Content) = @_;

  $Self->app->log->info("ImportClients: Importing clients...");
  
  my $sth = $Self->app->mdb->prepare("select * from Clients");

 ...

}

To get the database handle, in the client startup I have:

    $Self->attr(mdb => sub { DBI->connect('dbi:mysql:dbname', $dbuser, 
$dbpass), { RaiseError => 1, AutoCommit => 1 }); });
 
Of course the first error I get is when trying to call $Self->app, which is 
where I get the error "Can't call method "app" on unblessed reference". I 
get that error when I try to access anything in $Self, like stash or 
session. Once I figure out how to get around the bless issue, I think the 
only other issue I will have is the logging. Specifically, how to make 
$Self->app->log->info call my subroutine to print the text.

My first attempt is below, and it suffers from the bless issue and the log 
issue. Any help or suggestions would be greatly appreciated.

#------------------------------------------------------------------------------
sub GetFakeSelf {
  my ($mdbh, $Organization) = @_;

  my $Log = { debug => \&LogThis('DEBUG'), info => \&LogThis('INFO'), warn 
=> \&LogThis('WARN'), 
          error => \&LogThis('ERROR'), fatal => \&LogThis('FATAL') };

  my $App = { mdb => $mdbh, log => $Log };

  my $Stash = { Organization => $Organization };
  my $Session = { ClientType => 1  };
  my $Self = { app => $App, session => $Session, stash => $Stash };

  return $Self;
}
#------------------------------------------------------------------------------
sub LogThis {
  my ($Level, $Text) = @_;

  print($Level . ' - ' . $Text);
}

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to