Horizo​​n的Laravel Excel块导入作业

时间:2018-12-14 16:46:52

标签: laravel ftp xlsx laravel-excel

我每天尝试从FTP服务器将大(70k行).xlsx文件导入数据库。该作业在本地Redis服务器上运行良好,但是一旦将其放在生产服务器上并调度了该作业,该作业似乎会在实际导入时停止,并且不会出现任何错误消息。它只是站在那儿。 该文件似乎已导入,因为我之前已经收到错误消息,因为FTP服务器上的文件名错误并且truncate函数正在运行(它删除了我放入其中的一些测试数据)。我从来没有使用这种方法将任何东西放到表中,即使块和批次的数量降到了10。所以问题似乎是导入代码或Laravel Excel扩展,但是我真的不知道为什么在我的本地服务器上工作,但不在生产环境上工作。工作本身:

public function handle()
{
    $downloadcontroller = new DownloadController();

    $suffixFileName = '_data.xlsx';
    $today = Carbon::today()->toDateString();
    $split = explode('-' ,$today);
    $fileName = $split[0].$split[1].$split[2].$suffixFileName;
    $file = $downloadcontroller->downloadSupplyFile($fileName);
    Supply::truncate();

    Excel::import(new SuppliesImport(), $file);

    return;
}
The SuppliesImport file:

class SuppliesImport implements ToModel, WithBatchInserts, 
WithChunkReading, WithHeadingRow
{
public function model(array $row)
{
    $data = SupplyTransformer::transform($row);
    if (!$data) {
        return null;
    }

    return new Supply($data);
}

public function batchSize(): int
{
    return 150;
}

public function chunkSize(): int
{
    return 200;
}
}

转换函数:

public static function transform($data)
{

    if ($data['isopen'] && !$data['isforeclosure']) {

        $data['kvhx'] = $data['kvh'] . '-' . $data['x'];
        $chain = Chain::where('external_id', $data['id'])->first();

        if (!$chain) {
            return null;
        }

        return [
            'kvhx'                    => $data['kvh'] . '-' . $data['x'],
            'current_supply_duration' => date_diff(self::formatDate ($data['fromdate']),
                self::formatDate($data['todate']))->days,
            'total_supply_duration'   => date_diff(self::formatDate($data['xfromdate']), self::formatDate($data['xtodate']))->days,
            'chain_id'                => $chain->id,
            'type'           => $data['xid']
        ];
    }

    return null;
}

public static function formatDate($date) {
    return Date::excelToDateTimeObject($date);
}

0 个答案:

没有答案