Welcome Guest, Not a member yet? Register   Sign In
need some help on the order in which code gets executed
#1

[eluser]tim1965[/eluser]
Hi

I know this is not specifically a CI issue, but want to post it anyway to see if anyone can help.

First the background, i am trying to develop a video upload section for a website. I am using uploadify for the uploading part and then running video conversion using FFMPEG. Because of the size of the uploads it can take up 2 mins for the file to be converted to a .flv file. So i thought i would just be able to redirect users to a view telling them to wait while the file was converted and then redirect them back to the upload screen after the uploaded file has been converted.
So i have the following code

Code:
$data['error']="Thank you, your video has been uploaded, please wait while we convert it to use on the website. We will redirect you back to Video upload page once we have finished converting the file";

                
$this->load->view('upload/error',$data);

exec("nohup ffmpeg -i ".$s." -ar 22050 -ab 32k -f flv -s 320x240 ".$d);
So all its trying to do is load a view with a message telling the user to wait and then running the FFMPEG conversion.

I would have expected the code to execute in the order that i have written it i.e. load the view and then run the exec command. However it seems to be running the exec command first which hangs the browser and then loads the view after it has converted the video file.


So my question is can any one tell me why the view is not loading until after exec has executed. I have also tried running this as a background process using nohup, but to no success.
Thanks in advance.
#2

[eluser]metaltapimenye[/eluser]
.. b'coz since exec() part between ob_start() =====>(CI && exec() Process)====> ob_flush(). i suggest you to..

Code:
#model
if($uploaded){
      $this->load->view('upload/success',$data);
}else{
      $this->load->view('upload/error',$data);
}
while in view/upload/success.php..
Code:
#view/upload
<img src="boorring.gif">
<js>
// .. ajax request to exec converting

// .. ajax response handler to redirect
</js>
#3

[eluser]tim1965[/eluser]
metaltapimenye thnaks for your response, im sorry but i dont understand what you are saying in your post.

The code i posted is from my controller. I am trying to get the code to load a view( ignore the error title) with a message telling the user to wait until the exec has finished, at which point i will re-direct them (code not shown above, but will be after the exec command). What is currently happening is the exec is starting before the view is displayed, so all the user will see is the browser hanging essentially and then after the exec has run , the viiew i want loaded before is then displayed. I hope i am explaining this correctly, and that you can help shed some light on this..
#4

[eluser]AndrewMalachel[/eluser]
I think what "metaltapimenye" tried to say is:

first:
you run your view first without doing the exec():
Code:
$data['error']="Thank you, your video has been .... we have finished converting the file";
                
$this->load->view('upload/error',$data);
but, also you add AJAX request in the view that call a controller to run the exec()
view:
Code:
&lt;html&gt;
&lt;head&gt;&lt;/head>
&lt;body&gt;
&lt;?=$error;?&gt;
<img src="ajax_wait.gif" />
<js>
/**
*  Do Ajax.request here, call your controller that run the exec()
*  exmpl: "ajax/do_exec"
*
*  on Ajax.success, do the redirect with javascript
*  exmpl: document.location.href = "/convert_success/";
*  or maybe: document.location.reload(); //if you want the page to be refreshed...
*  wherever your want it to be redirected
**/
// exmpl : goes something like these on jQuery....
    ajax_run = $.ajax({
        type: 'POST',
        url: '&lt;?=base_url();?&gt;ajax/do_exec', // your "ajax-to-do-the-exec" controller
        timeout: 999999, // or forever?? hahaha...
        data: data_or_parameter_of_the_video_your_exec_want_to_handle,
        success: function(response) {
            // if success, redirect this page to:
            if(response == 'exec_ok') /* see the return on your controller after this */
                document.location.href = '&lt;?=base_url();?&gt;video/convert_success';
            else /* if something happen during your exec run, redirect to: */
                document.location.href = '&lt;?=base_url();?&gt;video/convert_failed'
        },
        error: function() {
            // alse if any error occured, or timeout, redirect this page to:
            document.location.href = '&lt;?=base_url();?&gt;video/convert_failed'
        }
    })
// done!!!

</js>
&lt;/body&gt;&lt;/html>

and your ajax controller would look like these:
Code:
class Ajax extends Controller {
    function Ajax() {
        parent::Controller();
        // ... and all your construction needed...
    }

    function do_exec() {
        // ... do all your stuff for your exec parameters
        // and then your exec:
        exec("nohup ffmpeg -i ".$s." -ar 22050 -ab 32k -f flv -s 320x240 ".$d);
        // after the exec success, return this for your ajax:
        echo "exec_ok";
/*
or do something like try..catch so if error occurred,
you can send other message to the ajax something like "exec_KO"
*/
    }
}

well, that's it, I suppose...

Oyeah, last but not least, sorry for my lack of english.... :-D :-D :-D
#5

[eluser]tim1965[/eluser]
Thanks for your detailed response on this, I will give this a go now. Your English is fine. Thanks again as i have really been banging my head against a brick wall with this one.
#6

[eluser]tim1965[/eluser]
Hi i have tried this and cannot seem to be able to call the exec script (execute_conversion) from your ajax call in the view. Ajax is not my area of expertise, so maybe you can give this a quick eyeball to see if i am missing something.
My view
Code:
&lt;html&gt;
&lt;head&gt;&lt;/head>
&lt;body&gt;
&lt;?=$error;?&gt;
<img src="ajax_wait.gif" />
[removed]
/**
*  Do Ajax.request here, call your controller that run the exec()
*  exmpl: "ajax/do_exec"
*
*  on Ajax.success, do the redirect with javascript
*  exmpl: document.location.href = "/convert_success/";
*  or maybe: document.location.reload(); //if you want the page to be refreshed...
*  wherever your want it to be redirected
**/
// exmpl : goes something like these on jQuery....
    ajax_run = $.ajax({
        type: 'POST',
        url: '&lt;?=base_url();?&gt;upload/execute_conversion', // your "ajax-to-do-the-exec" controller
        timeout: 999999, // or forever?? hahaha...
        //data: data_or_parameter_of_the_video_your_exec_want_to_handle,
        success: function(response) {
            // if success, redirect this page to:
            if(response == 'exec_ok') /* see the return on your controller after this */
                document.location.href = '&lt;?=base_url();?&gt;video/convert_success';
            else /* if something happen during your exec run, redirect to: */
                document.location.href = '&lt;?=base_url();?&gt;video/convert_failed'
        },
        error: function() {
            // alse if any error occured, or timeout, redirect this page to:
            document.location.href = '&lt;?=base_url();?&gt;video/convert_failed'
        }
    })
// done!!!

[removed]
&lt;/body&gt;&lt;/html>

My exec function in controller upload
Code:
function execute_conversion()
{

$trans_id=$this->session->userdata('trans_id');

$this->load->model('m_temp_video_uploads');
$return_data=$this->m_temp_video_uploads->query_temp_video_uploads($trans_id);


$s=$return_data['path'].$return_data['file_name'];

$ext=".flv";
$d=$return_data['path'].$return_data['file_name'].$ext;
//ob_start();

exec("ffmpeg -i ".$s." -ar 22050 -ab 32k -f flv -s 320x240 ".$d);
echo "exec_ok";
}
#7

[eluser]tim1965[/eluser]
quick update i have now turned on firebug which i should have done in the first place and am getting an error

$ is not defined

for

ajax_run = $.ajax({

as i say this stuff isnt my strong point so i know i am missing something easy here, but would appreciate any input on this

Thnx
#8

[eluser]n0xie[/eluser]
Did you load the jQuery library?
#9

[eluser]tim1965[/eluser]
Er no, sorry bout that. Thanks for your help, is now doing what it should be doing. Apologies again.




Theme © iAndrew 2016 - Forum software by © MyBB