nawasara/sync 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nawasara/sync

Composer 安装命令:

composer require nawasara/sync

包简介

Foundation for the DB-cache + queue pattern shared across Nawasara service packages (WHM, Cloudflare, Keycloak, etc.) — sync job tracker, repository contract, snapshot trait, and audit UI.

README 文档

README

The foundation package for the DB-cache + queue pattern used across every Nawasara service integration. Provides the sync-job tracker, repository contract, snapshot trait, audit log UI, and a system-health dashboard.

The pattern

External services (Cloudflare, WHM, Keycloak, etc.) are slow, rate-limited, and unreliable. Hitting them on every page render is wrong. Instead, every Nawasara service package follows this shape:

External API  ◄─────  Mutation Job (queue)  ◄─────  User action
     │
     ▼  scheduled / on-demand sync
DB snapshot  ─────►  Repository  ─────►  Livewire page (paginated, fast)
  • Reads come from a local snapshot table (paginated, indexed, fast)
  • Writes dispatch queued mutation jobs that update the external API and the local snapshot atomically with content-hash conflict detection
  • Audit log at /admin/sync/jobs records every dispatched job (action, target, user, payload with sensitive fields masked, status, duration, error)

What this package ships

  • SyncJob model — the audit row for every queued action across every package
  • AbstractSyncJob — base for queued mutation/sync jobs. Auto-creates the tracker row in the constructor, marks it running/success/failed/conflict in handle(), and masks sensitive payload keys (password, secret, token, key) before persisting
  • SyncedRepository contract — list / find / all / create / update / delete / syncNow / lastSyncedAt. Every service package implements this consistently
  • HasSyncStatus trait — adds sync_status, sync_error, last_synced_at, and content_hash columns plus convenience scopes (pendingSync, failed, synced) and helpers (markPending, markSynced, markFailed)
  • TracksLastSync trait — repository helper that resolves the last successful sync time from nawasara_sync_jobs instead of from per-record last_synced_at (which only updates when content changes)
  • <x-nawasara-sync::sync-badge> — pill component that visualises a row's sync state in tables
  • System Health at /admin/sync/health — totals, per-service breakdown with success rate, recent failures, and a window switcher
  • Sync Jobs at /admin/sync/jobs — searchable, filterable audit log with retry and detail modal

Installation

composer require nawasara/sync
php artisan migrate
php artisan db:seed --class="Nawasara\Sync\Database\Seeders\PermissionSeeder" --force

Auto-discovered.

Implementing a service package

use Nawasara\Sync\Concerns\HasSyncStatus;
use Nawasara\Sync\Concerns\TracksLastSync;
use Nawasara\Sync\Contracts\SyncedRepository;
use Nawasara\Sync\Jobs\AbstractSyncJob;

// 1. Snapshot model
class WhmEmailAccount extends Model
{
    use HasSyncStatus;

    public function computeContentHash(): string
    {
        return hash('sha256', json_encode([
            'email' => $this->email,
            'quota_mb' => $this->quota_mb,
            // … fields you want to detect drift on
        ]));
    }
}

// 2. Repository
class WhmEmailAccountRepository implements SyncedRepository
{
    use TracksLastSync;

    public function list(array $filters = [], int $perPage = 25) { /* ... */ }
    public function create(array $data): ?SyncJob { /* dispatch CreateWhmEmailJob */ }
    public function update($id, array $data): ?SyncJob { /* dispatch ChangeWhmEmail*Job */ }
    public function delete($id): ?SyncJob { /* dispatch DeleteWhmEmailJob */ }
    public function syncNow(): ?SyncJob { /* dispatch SyncWhmEmailsJob */ }

    public function lastSyncedAt(): ?Carbon
    {
        return $this->lastSuccessfulSyncAt('whm', 'sync_emails');
    }
}

// 3. Job
class ChangeWhmEmailQuotaJob extends AbstractSyncJob
{
    protected function service(): string { return 'whm'; }
    protected function action(): string { return 'quota_email'; }
    protected function targetType(): ?string { return 'WhmEmailAccount'; }
    protected function targetId(): ?string { return $this->payload['email']; }

    protected function execute(): array
    {
        // call external API…
        // update snapshot row…
        return ['quota_mb' => $newQuota];
    }
}

Permissions

Permission Description
sync.job.view View sync jobs audit log
sync.job.manage Retry or delete sync job rows

Author

Pringgo J. Saputro <odyinggo@gmail.com>

License

MIT

nawasara/sync 适用场景与选型建议

nawasara/sync 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 140 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 05 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「queue」 「sync」 「laravel」 「repository-pattern」 「db-cache」 「audit-log」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 nawasara/sync 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 nawasara/sync 我们能提供哪些服务?
定制开发 / 二次开发

基于 nawasara/sync 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 140
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 49
  • 依赖项目数: 11
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-05-09