L
L
l4m3r2018-10-17 11:49:13
Laravel
l4m3r, 2018-10-17 11:49:13

How can you save instance of an object between requests?

Meaning : execute a long artisan command, and monitor its execution (console buffer output) at the front.
When the page loads, I run the command (sync ()) with Ajax and then on the timer, I try to get getBuffer () and display it on the screen. Of course, this does not work properly, since the BufferedOutput object stored in the cache will already be a new object when deserialized.
How can the problem be solved?

public function sync(Request $request)
{
  if ($request->ajax()) {
    $sessionId = $request->get('sessionId');
    /** @var BufferedOutput $buffer */
    $buffer = Cache::remember("youtrack.buffer:$sessionId", 10, function () {
      return new BufferedOutput;
    });

    set_time_limit(600);
    Artisan::call('youtrack:sync', [], $buffer);

    return ['success' => true, 'output' => nl2br(htmlspecialchars($buffer->fetch()))];
  }

  return view('youtrack.sync');
}

public function getBuffer(Request $request, $sessionId)
{
  /** @var BufferedOutput $buffer */
  $buffer = Cache::get("youtrack.buffer:$sessionId");

  if (!$buffer instanceof BufferedOutput) {
    abort(500, 'Buffer not found.');
  }

  return nl2br(htmlspecialchars($buffer->fetch()));
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Abdula Magomedov, 2018-10-17
@l4m3r

No way. You can write the current process to a "file" at run time. And already from the file to take the finished data.
As I understand it, in this place you are calling your own console application. And "youtrack:sync" is some kind of command. It's there inside that you need to write the process to a file. Artisan::call('youtrack:sync', [], $buffer);

H
hOtRush, 2018-10-17
@hOtRush

For such a task, the good old Gearman is perfect.
When executing a task in the queue, you can write the execution status in percentage/quantity, for example sendStatus(10, 100) - 10% completed. And from anywhere in the application, get this status by job id.
php.net/manual/en/gearmanclient.jobstatus.php
php.net/manual/en/gearmanjob.sendstatus.php
Well, in general, having this in the controller is a very bad practice) There are queues for this, as it were

set_time_limit(600);
Artisan::call('youtrack:sync', [], $buffer);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question