Perform very heavy task in the background php

3

I upload a file from my website with php, but the task may take about 5 minutes to finish, in principle the size of the file may increase.

The question is, is it possible to perform the task in the background?

I have seen that both a POST or GET request in a form or an Ajax query depends on the duration of its execution of the Timeout property.

Is there a way to perform a task without taking into account the timeout?

    
asked by nachfren 26.09.2016 в 21:18
source

2 answers

2

Good, from what I know, what you could do would be to locate setTimeout and increase-lo. You would do it in the following way:

In the php.ini file, locate these lines and modify them:

upload_max_filesize = 2M

max_execution_time = 60

link

I add what I found, you could use a worker and the function would be GearmanClient::addTaskBackground you would do it in the following way:

<?php

# El script del cliente

# Creamos el cliente gearman
$gmc= new GearmanClient();

# Añade el servidor de trabajos por defecto
$gmc->addServer();

# Establece un par de llamadas de retorno de modo que pueda seguirse el progreso
$gmc->setCompleteCallback("reverse_complete");
$gmc->setStatusCallback("reverse_status");

# Añade una tarea para la función "reverse"
$task= $gmc->addTask("reverse", "Hello World!", null, "1");

# Añade otra tarea, pero esta se ejecuta en segundo plano
$task= $gmc->addTaskBackground("reverse", "!dlroW olleH", null, "2");

if (! $gmc->runTasks())
{
    echo "ERROR " . $gmc->error() . "\n";
    exit;
}

echo "DONE\n";

function reverse_status($task)
{
    echo "STATUS: " . $task->unique() . ", " . $task->jobHandle() . " - " . $task->taskNumerator() . 
         "/" . $task->taskDenominator() . "\n";
}

function reverse_complete($task)
{
    echo "COMPLETE: " . $task->unique() . ", " . $task->data() . "\n";
}

?>
<?php

# El script del trabajador

echo "Starting\n";

# Creamos el objeto trabajador
$gmworker= new GearmanWorker();

# Añade el servidor por defecto (localhost)
$gmworker->addServer();

# Registra la función "reverse" en el servidor
$gmworker->addFunction("reverse", "reverse_fn");

print "Waiting for job...\n";
while($gmworker->work())
{
  if ($gmworker->returnCode() != GEARMAN_SUCCESS)
  {
    echo "return_code: " . $gmworker->returnCode() . "\n";
    break;
  }
}

function reverse_fn($job)
{
  echo "Received job: " . $job->handle() . "\n";

  $workload = $job->workload();
  $workload_size = $job->workloadSize();

  echo "Workload: $workload ($workload_size)\n";

  # Este bucle de estado no es neceasario, únicamente muestra cómo funciona
  for ($x= 0; $x < $workload_size; $x++)
  {
    echo "Sending status: " . $x + 1 . "/$workload_size complete\n";
    $job->sendStatus($x+1, $workload_size);
    $job->sendData(substr($workload, $x, 1));
    sleep(1);
  }

  $result= strrev($workload);
  echo "Result: $result\n";

  # Retorna lo que se quiere enviar al cliente
  return $result;
}

?>
    
answered by 26.09.2016 в 22:30
0

To set or remove the time limit of a PHP script it is necessary to add the following to the beginning of the file:

// 300 segundos -> 5 minutos
ini_set( 'max_execution_time' , 300 );
// 0 -> Quita el limite de tiempo
ini_set( 'max_execution_time' , 0 );

Also, if the task allows it, you can use a CRON-type script to perform this task every so often in parallel with the execution of the application.

    
answered by 26.09.2016 в 22:30