carlin / laravel-resumable
Lightweight resumable checkpoint package for Laravel 8-13 — read last recovery point, run your business logic, save recovery point.
v1.0.0
2026-12-01 07:56 UTC
Requires
- php: >=8.1 <8.5
- illuminate/cache: ^8.0|^9.0|^10.0|^11.0|^12.0|^13.0
- illuminate/contracts: ^8.0|^9.0|^10.0|^11.0|^12.0|^13.0
- illuminate/support: ^8.0|^9.0|^10.0|^11.0|^12.0|^13.0
Requires (Dev)
- orchestra/testbench: ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- phpunit/phpunit: ^9.5|^10.0|^11.0|^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-12-01 08:10:47 UTC
README
专为 Laravel 8 – 13(PHP 8.1 – 8.4)打造的极简无数据库断点续作扩展包:读取上次恢复点 → 写正常业务逻辑 → 保存恢复点。
安装与配置
composer require carlin/laravel-resumable
发布配置文件(可选):
php artisan vendor:publish --tag=resumable-config
config/resumable.php 内置 4 种无数据库存储驱动,通过 RESUMABLE_STORE 环境变量或 Resumable::store(...) 随时切换:
file(默认):本地文件 +flock+.tmp原子替换redis:Redis Hash + Lua 原子 CAS + 分布式锁cache:复用 LaravelCache存储与锁memory:内存驱动(适合单次测试)
核心用法
1. 闭包托管模式(Resumable::run / Resumable::tryRun)
自动加锁、自动加载上次恢复点、正常结束自动标记完成(下次跳过),报错时保留最后一次 save() 的恢复点:
use Carlin\Resumable\Checkpoint; use Carlin\Resumable\Facades\Resumable; Resumable::run('es:orders:import', function (Checkpoint $cp) { $lastId = $cp->get('last_id', 0); while ($rows = Order::where('id', '>', $lastId)->orderBy('id')->limit(500)->get()) { if ($rows->isEmpty()) { break; } bulkWriteToEs($rows); $lastId = $rows->last()->id; $cp->save(['last_id' => $lastId]); // 保存恢复点 } });
- 指定存储驱动:
Resumable::store('redis')->run(...) - Cron 定时任务防并发:
Resumable::tryRun(...)(拿不到锁静默返回null) - 提前让出下次续跑:循环中调用
$cp->interrupt(); return;,不标为完成,下次接着跑 - 强制重跑:
Resumable::run($key, $fn, force: true)或Resumable::reset($key)
2. Shopee 15 天时间窗 × 游标同步
Resumable::run("shopee:orders:{$shopId}", function (Checkpoint $cp) use ($start, $end) { $from = $cp->get('from', $start); $cursor = $cp->get('cursor'); while ($from < $end) { $to = min($end, $from + 15 * 86400); $resp = $shopee->getOrderList($from, $to, $cursor); saveOrders($resp['orders']); if ($resp['has_more']) { $cursor = $resp['next_cursor']; } else { $from = $to; $cursor = null; } $cp->save(['from' => $from, 'cursor' => $cursor]); } });
3. 文件断点下载
Resumable::run('download:' . md5($url), function (Checkpoint $cp) use ($url, $totalSize, $partFile, $finalFile) { $offset = $cp->get('offset', 0); while ($offset < $totalSize) { $end = min($totalSize - 1, $offset + 1024 * 1024 - 1); $bytes = downloadRange($url, $offset, $end); writeAtOffset($partFile, $offset, $bytes); $offset += strlen($bytes); $cp->save(['offset' => $offset]); } rename($partFile, $finalFile); });
4. 分片上传(Controller 手动读写 Resumable::open)
$cp = Resumable::open("upload:{$fileHash}"); $chunks = $cp->get('chunks', []); if (! in_array($chunkIndex, $chunks, true)) { saveChunkFile($fileHash, $chunkIndex, $bytes); $chunks[] = $chunkIndex; $cp->save(['chunks' => $chunks]); } if (count($chunks) === $totalChunks) { mergeChunks($fileHash, $totalChunks); $cp->finish(); }
5. 扩展自定义驱动(Resumable::extend)
Resumable::extend('custom', function ($app, array $config) { return new MyCustomStore($config); });