Jmrtech's avatar

File operations within Jobs

I wanted to handle the file operations in my job so the user isn't waiting for the upload. I first ran into errors passing the $request object because it considered it a closure. I then only passed the image itself, but then it said that the image was empty. So I moved the file operations to the controller and left the uploading portion to the job and ended up with this error:

[ErrorException]                               
  file_get_contents(): Filename cannot be empty

Here is code from controller:

// if mms dispatch job
        if ((bool) $request->mms) {
            $twilioParams = [
                'to' => $this->to,
                'from' => $this->from,
                'body' => $this->body
            ];

            $image = $request->file('image');

            $imageFileName = $patient->id . '_' . time() . '.' . $image->getClientOriginalExtension();

            // create file path
            $filePath = '/twilio/' . $hospital->id . '/' . $imageFileName;

            $fileInfo = [
                'file' => file_get_contents($image),
                'filePath' => $filePath
            ];

            $fileExtension = $request->file('image')->getClientOriginalExtension();

            $job = (new UploadFileForMms($fileInfo, $hospital, $twilioParams))->onQueue('mms');

            $this->dispatch($job);

And here is code from job:

public function handle()
    {
        $s3 = Storage::disk('s3');

        // store file on s3
        // file must be public for twilio to access
        $s3_response = $s3->put($this->file['filePath'], $this->file['file'], 'public');

        if ($s3_response) {
            $this->twilioParams['mediaUrl'] = env('S3_URL') . $this->file['filePath'];

            $job = (new SendMms($this->hospital, $this->twilioParams));

            $this->dispatch($job);

        }
    }

My thought was that by adding this process to a job it would return the user back to the web page quickly versus having these steps performed synchronously.

Edit: I am now getting this error:

[ErrorException]      
  Undefined index: job 

Maybe this is an iron.io error...

0 likes
0 replies

Please or to participate in this conversation.