首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Laravel队列系统的问题

使用Laravel队列系统的问题
EN

Stack Overflow用户
提问于 2018-02-11 12:23:21
回答 1查看 1K关注 0票数 0

整个晚上我都在为这件事而挣扎。我正在实现一个系统,用户请求的报告将在以后排队并发送。我使用的是本地Laravel解决方案,但是,每当我从同步转移到数据库时,我就会收到错误。我已经查看了laravel调试日志,并且可以看到,每当我使用数据库时,我都会遇到“试图获取非对象属性”错误。

2018-02-11 12:05:46 production.ERROR: ErrorException:试图在production.ERROR中获取非对象的属性 堆栈跟踪: /var/www/appname/app/Libraries/InstagramEngine.php(143):Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8,#0‘试图获取p.’,/var/www/app.,143,数组) appname/app/Jobs/ProcessCollectionReport.php(81):App\Libraries\InstagramEngine::getAccountMediaByUserId(Object(App\Models\Account),1515715200,1518393599)

我的代码如下-我将从控制器开始,它非常直接:

代码语言:javascript
复制
/**
 * Builds excel report for collection
 * @param         $slug
 * @param Request $request
 */
public function export(Request $request, $slug)
{
    // get logged in user
    $user = User::find(Auth::user()->id);

    // get collection
    $collection = Collection::where('slug', $slug)->firstOrFail();

    // get start and end date
    $start = $request->input('start', Carbon::now()->subDays(30)->startOfDay()->timestamp);
    $end   = $request->input('end', Carbon::now()->endOfDay()->timestamp);

    // queue the job
    ProcessCollectionReport::dispatch($user->id, $collection->id, $start, $end);
}

如您所见,我的控制器确实获取了一些细节,并在此基础上创建了一个排队项目。我甚至在使用Laravel序列化问题的情况下使用I创建了这个项目(我最初认为这可能导致了我的问题,但他们仍然只使用这个代码库)。

我的工种在下面

代码语言:javascript
复制
<?php

namespace App\Jobs;

use App\Notifications\CollectionReportGenerated;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

use App\Models\User;
use App\Models\Collection;
use App\Libraries\InstagramEngine;
use App\Libraries\Profile;
use Excel;

class ProcessCollectionReport implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $userId;
    protected $collectionId;
    protected $start;
    protected $end;

    /**
     * The number of times the job may be attempted.
     *
     * @var int
     */
    public $tries = 5;

    /**
     * The number of seconds the job can run before timing out.
     *
     * @var int
     */
    public $timeout = 210;

    /**
     * Create job instance
     * ProcessCollectionReport constructor.
     * @param User       $userId
     * @param Collection $collectionId
     * @param            $start
     * @param            $end
     */
    public function __construct($userId, $collectionId, $start, $end)
    {
        // access arguments
        $this->userId          = $userId;
        $this->collectionId    = $collectionId;
        $this->start           = $start;
        $this->end             = $end;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // set base profiles
        $profiles = new \stdClass();
        $profiles->ranges = dateRanges($this->start, $this->end);
        $profiles->accounts = array();

        // get collection
        $collection = Collection::find($this->collectionId);
        $user       = User::find($this->userId);

        // loop through accounts in collection
        foreach($collection->accounts as $account)
        {
            // create new profile instance
            $profile = new Profile($this->start, $this->end);

            // gets media for account
            $media = InstagramEngine::getAccountMediaByUserId($account, $this->start, $this->end);
}
}
}

现在您可以看到,在我的工作中,我对一个类+函数getAccountMediaByUserId进行了外部调用--这个类接受一个帐户模型作为它的第一个参数,但是从上面的错误来看,它看起来是在传递包含所有模型的整个数组。该函数的代码如下:

代码语言:javascript
复制
/**
     * Returns account media within specified range for Instagram Account by ID
     * @param Account $account
     * @param $start
     * @param $end
     * @return array
     */
    public static function getAccountMediaByUserId(Account $account, $start, $end)
    {
        // set up unique cached key for API call
        $cache_key = sha1($account->username.$start.$end);

        // check if we have this API call cached
        if(Cache::has($cache_key))
        {
            // return results
            return Cache::get($cache_key);
        }

        // set up default response
        $return = array();

        // set breaker to false
        $breaker = false;
        $page = 1;

        // set initial URL for API call
        $url = 'https://api.instagram.com/v1/users/'.$account->instagram_id.'/media/recent/?count=30&access_token='.Auth::user()->primaryAccount->access_token;
}

真正令我困惑的是,如果设置了缓存而不是代码,这是很奇怪的,因为如果我要传递整个数组,那么为什么$account->username工作,而不是$account->Instagram_id

我真的不明白为什么只有当我使用数据库而不是同步时才会发生这种情况。

提前谢谢你

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-22 15:29:58

已解决:

我在我的工作中使用了auth::user,在作为作业运行时是不可用的。

谢谢

丹尼尔

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48731427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档