vendredi 29 janvier 2016

Perl Session Variables Do Not Update During If Statement

This form on submit runs two types of AJAX calls, one just sends the form data and it gets stored into session variables (the first if statement) and the second polls the same form but hits the elsif statement to get a read out of what is being stored into the session variables.

The AJAX works and the session storage works, what seems to be the problem is that the session data isn't being updated whilst the first if statement is running.

The end goal of this program is to fire of many system scripts on submit and report back on their status via the polling AJAX. I've created a fake work script to test this even though I will be using non Perl scripts in the final version.

Like I said, it seems that if script 1 is complete, the session variable isn't updated until the entire if statement is over. I don't know how that is possible but I know for sure that the session variables are not being updated in real time as the poll comes back with either complete or null. (It only returns complete when I remove the $session->delete; line, which isn't meant to fire until after 30 seconds in this example.)

What I need is a solution so that as soon as a script finishes and updates the session storage, I can see that with my polling AJAX.

form.cgi

#!/usr/bin/perl

use strict;
use warnings;

use CGI;
use CGI::Carp 'fatalsToBrowser'; # sends error messages to the browser!
use CGI::Session;
use JSON;

my $workTimer = 15; # In seconds.

my $query = CGI->new();
my $session = CGI::Session->new($query);
print $session->header();

if(defined $query->param('name') && defined $query->param('number') && defined $query->param('email')){

    $session->param('name', $query->param('name'));
    $session->param('number', $query->param('number'));
    $session->param('email', $query->param('email'));

    # 'inactive', 'in-progress', 'failed', 'complete' -> To be sent to client.
    $session->param('script1', 'inactive');
    $session->param('script2', 'inactive');

    # Set script 1 up.
    $session->param('script1', 'in-progress');
    my $script1 = `perl do-work.pl $workTimer` || $session->param('script1', 'failed');
    # Check the return value of the script.
    if($script1 != 1){
        $session->param('script1', 'failed');
    }else{
        $session->param('script1', 'complete');
    }

    # Set script 2 up.
    $session->param('script2', 'in-progress');
    my $script2 = `perl do-work.pl $workTimer` || $session->param('script2', 'failed');
    # Check the return value of the script.
    if($script2 != 1){
        $session->param('script2', 'failed');
    }else{
        $session->param('script2', 'complete');
    }

    # Clear session here!
    $session->delete;
}
elsif(defined $query->param('polling')){
    my @pollingArray = ();

    push(@pollingArray, $session->param('script1'));
    push(@pollingArray, $session->param('script2'));

    my $json->{"poll-reply"} = \@pollingArray;
    my $json_text = to_json($json);

    print "$json_text";
}
else{
    HTML goes here...
}

do-work.pl

#!/usr/bin/perl

use strict;
use warnings;

# Declare initial variables.
my $workDuration;
my $sec = time();

# Check for arguments.
if(1 == scalar $#ARGV + 1){
    my $var = $ARGV[0];
    if(!($var - int($var))){
        $workDuration = $var;
    }
    else{
        print 0;
        exit;
    }
}
else{
    print 0;
    exit;
}

# Run while loop for duration of work.
while(time() < $sec + ($workDuration)){
    # Do work here...
}

print 1;
exit;

app.js

var polling;

$('#submit').on("click", function(e){
    e.preventDefault();

    $.ajax({
        url: "/form/form.cgi",
        type: "post",
        data: {
            name: $('#name').val(),
            number: $('#number').val(),
            email: $('#email').val()
        },
        success: function(data){
            console.log(data);
        }
    });

    polling = pollingForStatus();
});

function pollingForStatus(){
    return window.setInterval(function(){ 
        $.ajax({
            url: "/form/form.cgi",
            type: "post",
            data: {
                polling: "poll"
            },
            success: function(data){
                console.log(data);
                if(data === "off"){
                    window.clearInterval(polling);
                }
            }
        }); 
    }, 1000);
}

Aucun commentaire:

Enregistrer un commentaire