1

Sorry, I really tried but I can't find a solution to this simple problem.

I need to get id session in plack, but not via browser, I want to do internally background in main namespace perl plack code.

This is my code:

#!/usr/bin/perl 
use Plack::Builder;
use YAML;
use Plack::Session::Store::File;

my $app = sub {
    my $env = shift;
    #$global = 'write thsi text to file';
    $global = $env->{'psgix.session.options'}{id};
    return [ 200, [], [ "App session ID: " . $global ] ];
};
$app->();

open my $fileHandle, ">>", "file" or die "Can't open '\n";
print $fileHandle $global;
close $fileHandle;

my $id = sub {
    my $env = shift;
    return [ 200, [], [ 'The ID session is: ' . $global ] ];
};

builder {
    enable 'Session', store => Plack::Session::Store::File->new(
                dir => './sessiondir',
                serializer   => sub { YAML::DumpFile( reverse @_ ) },
                deserializer => sub { YAML::LoadFile( @_ ) },
            );
    mount '/id' => $id;
    mount '/' => $app;

};

If I visit the root path http://192.168.1.1:5000, I get:

App session ID: 33d2e5c9fc9d57ca79679675130d7a06b7ccc1f6

If I visit http://192.168.1.1:5000/id

The ID session is: 33d2e5c9fc9d57ca79679675130d7a06b7ccc1f6

I want to write the id session in the file from $global variable:

open my $fileHandle, ">>", "file" or die "Can't open '\n";
print $fileHandle $global;
close $fileHandle;

In the destination file I get nothing, but if I change to variable to this, it work:

#$global = 'write thsi text to file';

Maybe the problem is because this text:

Threads in the thread pool are managed by the system. These threads are not bound to the current request. Therefore, Session is not available for them.

Source: Can I access sessions in background thread?

My target is just get the id and open this file to do another things from background. I want to avoid to save another file to this simple task, avoid sql, etc.

Because I already save the data session in a file:

enable 'Session', store => Plack::Session::Store::File->new(
                dir => './sessiondir',
                serializer   => sub { YAML::DumpFile( reverse @_ ) },
                deserializer => sub { YAML::LoadFile( @_ ) },
            );

I appreciate any recommendation, maybe this is possible in another way.

Thanks so much.

Imylor
  • 384
  • 1
  • 9
  • There is nothing about threads in the code you have shown. And where do you have the code with `$fileHandle`? Is that part of the application, or part of the psgi file, or is it a completely different program? Please try to tell us why you want to do this. – simbabque Nov 10 '17 at 18:10
  • Oh. You are not using `strict`, and your `$global` in the app is not lexical. So it gets boggled up all the time. If you want the session ID of the current request, tell your program to do that. If you run this current code with a threaded or forking server, it might well end up doing weird things. – simbabque Nov 10 '17 at 18:15
  • Dear simbabque, thanks for your comment. I genereate dynamically the router url in bluider, and I need to get the random number is store in the file session to build the url dynamically. So first the user login, it generate a random number like token, then after login I load the url dynamically in plack builder mount according to that user it is done from file session, thanks again for your participation. – Imylor Nov 10 '17 at 18:20
  • I don't really understand. The idea behind the session is that the user gets a cookie with the session id in it. What's wrong with that? Also, how do you run this code? What server are you using? (That's the `plackup` part...). – simbabque Nov 10 '17 at 18:23

0 Answers0