Show when a QuickBooks sync is running

Syncs record their state in a new qbo_sync_statuses table: queued when asked for, running with how
many records have been read, removed when finished (stale rows are ignored after 6 hours). The
business report says when estimates, invoices or payments are still syncing and refreshes every 30
seconds until they finish; the settings page's Synchronization table shows the same per type.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
This commit is contained in:
Claude committed 2026-10-31 10:03:12 -04:00
1 parent bf7c7b0905
commit 28538d4c7f
11 files changed
+157 -3

No files matched your search

+1 -1
View File
@@ -144,7 +144,7 @@ Estimates and invoices keep their number, date, customer, total and **man-hours*
* **Hours**: estimated hours on estimates, issues' estimated time, invoiced hours on invoices and time spent, with invoiced vs. spent and spent vs. estimated. * **Hours**: estimated hours on estimates, issues' estimated time, invoiced hours on invoices and time spent, with invoiced vs. spent and spent vs. estimated.
* **By month**: every figure month by month, with totals. * **By month**: every figure month by month, with totals.
Estimates, invoices and payments count by their QuickBooks date. Issues' estimated time counts issues created in the period, leaving out parents so subtasks aren't counted twice. Spent time counts time logged in the period. Estimates and invoices synced before these figures were added have no total or hours until a full sync (the report says how many); run one again after changing the labor products/services. Estimates, invoices and payments count by their QuickBooks date. Issues' estimated time counts issues created in the period, leaving out parents so subtasks aren't counted twice. Spent time counts time logged in the period. While estimates, invoices or payments are syncing, the report says so (with how many have been read) and refreshes every 30 seconds; the settings page's Synchronization table shows the same per type. Estimates and invoices synced before these figures were added have no total or hours until a full sync (the report says how many); run one again after changing the labor products/services.
### QuickBooks custom fields ### QuickBooks custom fields
@@ -18,5 +18,7 @@ class QboReportsController < ApplicationController
def index def index
@report = RedmineQboBilling::BusinessReport.new(period: params[:period].presence || 'this_year', @report = RedmineQboBilling::BusinessReport.new(period: params[:period].presence || 'this_year',
from: params[:from], to: params[:to], today: User.current.today) from: params[:from], to: params[:to], today: User.current.today)
# Syncs still running for what the report counts: its figures are incomplete until they finish
@syncing = QboSyncStatus.by_entity(%w[Estimate Invoice Payment]).values.sort_by(&:entity)
end end
end end
+4 -1
View File
@@ -40,7 +40,10 @@ class QboSyncDispatcher
end end
end end
jobs.each { |job| QboSyncJob.perform_later(entity: job, full_sync: full_sync) } jobs.each do |job|
QboSyncStatus.queued(job.to_s)
QboSyncJob.perform_later(entity: job, full_sync: full_sync)
end
end end
def self.log(msg) def self.log(msg)
+6
View File
@@ -25,7 +25,13 @@ class QboSyncJob < ApplicationJob
elsif doc_number.present? elsif doc_number.present?
service.sync_by_doc_number(doc_number) service.sync_by_doc_number(doc_number)
else else
# Shown on the business report and the settings page while it runs
QboSyncStatus.running(entity)
begin
service.sync(full_sync: full_sync) service.sync(full_sync: full_sync)
ensure
QboSyncStatus.finished(entity)
end
end end
end end
+1
View File
@@ -70,6 +70,7 @@ class QboBaseModel < ActiveRecord::Base
# Sync all entities, typically triggered by a scheduled task or manual sync request # Sync all entities, typically triggered by a scheduled task or manual sync request
def self.sync def self.sync
QboSyncStatus.queued(model_name.name)
QboSyncJob.perform_later(entity: model_name.name, full_sync: true) QboSyncJob.perform_later(entity: model_name.name, full_sync: true)
end end
+70
View File
@@ -0,0 +1,70 @@
#The MIT License (MIT)
#
#Copyright (c) 2026 rick barrette
#
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# A QuickBooks sync that's queued or running, one row per entity (Invoice, Estimate...), with how many records
# it has processed so far. The row is removed when the sync finishes. Rows not touched for STALE are ignored,
# so a sync that died (e.g. Redmine restarted mid-sync) doesn't leave a notice up forever.
class QboSyncStatus < ActiveRecord::Base
STALE = 6.hours
scope :active, -> { where('updated_at > ?', STALE.ago) }
# A sync was asked for and is waiting for the job to start
def self.queued(entity)
record(entity) { |s| s.state = 'queued' unless s.state == 'running' }
end
# The sync started
def self.running(entity)
record(entity) do |s|
s.state = 'running'
s.processed = 0
s.started_at = Time.now
end
end
# count more records were processed
def self.progress(entity, count)
where(entity: entity.to_s).update_all(['processed = processed + ?, updated_at = ?', count.to_i, Time.now])
rescue StandardError => e
Rails.logger.warn "[QboSyncStatus] #{e.message}"
end
def self.finished(entity)
where(entity: entity.to_s).delete_all
rescue StandardError => e
Rails.logger.warn "[QboSyncStatus] #{e.message}"
end
# Active syncs by entity name: { "Invoice" => QboSyncStatus, ... }, optionally only for some entities
def self.by_entity(entities = nil)
scope = active
scope = scope.where(entity: Array(entities).map(&:to_s)) if entities
scope.index_by(&:entity)
rescue StandardError
{}
end
def self.record(entity)
status = find_or_initialize_by(entity: entity.to_s)
status.state ||= 'queued'
yield status
status.updated_at = Time.now
status.save!
rescue StandardError => e
Rails.logger.warn "[QboSyncStatus] #{e.message}"
end
private_class_method :record
def running?
state == 'running'
end
end
+1
View File
@@ -38,6 +38,7 @@ class SyncServiceBase
entries.each do |remote| entries.each do |remote|
persist(remote) persist(remote)
end end
QboSyncStatus.progress(@entity.name, entries.size)
end end
end end
+12 -1
View File
@@ -45,6 +45,7 @@
[l(:label_items), Item, sync_items_path, items_path], [l(:label_items), Item, sync_items_path, items_path],
[l(:label_accounts), Account, sync_accounts_path, accounts_path] [l(:label_accounts), Account, sync_accounts_path, accounts_path]
] %> ] %>
<% sync_statuses = QboSyncStatus.by_entity %>
<h3><%= l(:label_management_and_sync) %></h3> <h3><%= l(:label_management_and_sync) %></h3>
<div class="box"> <div class="box">
<table class="list qbo-sync-table"> <table class="list qbo-sync-table">
@@ -61,7 +62,16 @@
<tr> <tr>
<td class="name"><%= list_path ? link_to(label, list_path) : label %></td> <td class="name"><%= list_path ? link_to(label, list_path) : label %></td>
<td class="count"><%= model.count %></td> <td class="count"><%= model.count %></td>
<td class="updated"><%= model.last_sync %></td> <td class="updated">
<% status = sync_statuses[model.name] %>
<% if status&.running? %>
<span class="icon icon-reload qbo-sync-busy"><%= l(:text_qbo_sync_running_short, count: number_with_delimiter(status.processed)) %></span>
<% elsif status %>
<span class="qbo-sync-busy"><%= l(:text_qbo_sync_queued) %></span>
<% else %>
<%= model.last_sync %>
<% end %>
</td>
<td class="buttons"><%= link_to sprite_icon('reload', l(:label_sync)), sync_path, class: 'icon icon-reload' %></td> <td class="buttons"><%= link_to sprite_icon('reload', l(:label_sync)), sync_path, class: 'icon icon-reload' %></td>
</tr> </tr>
<% end %> <% end %>
@@ -88,6 +98,7 @@
table.qbo-sync-table td.updated { width: 14em; } table.qbo-sync-table td.updated { width: 14em; }
table.qbo-sync-table td.buttons { width: 6em; } table.qbo-sync-table td.buttons { width: 6em; }
.qbo-sync-all .button { margin-left: 8px; } .qbo-sync-all .button { margin-left: 8px; }
.qbo-sync-busy { font-style: italic; color: #b06d00; }
</style> </style>
<%# Billing: estimates, issues from estimates and invoices %> <%# Billing: estimates, issues from estimates and invoices %>
+25
View File
@@ -27,6 +27,30 @@
<p class="qbo-report-range"><%= format_date(@report.from) %> &ndash; <%= format_date(@report.to) %></p> <p class="qbo-report-range"><%= format_date(@report.from) %> &ndash; <%= format_date(@report.to) %></p>
<% if Array(@syncing).any? %>
<% sync_labels = { 'Estimate' => l(:label_estimates), 'Invoice' => l(:label_invoices), 'Payment' => l(:label_payments) } %>
<div class="warning qbo-report-syncing">
<%= l(:text_report_syncing) %>
<ul>
<% @syncing.each do |status| %>
<li>
<strong><%= sync_labels[status.entity] || status.entity %></strong>:
<% if status.running? %>
<%= l(:text_qbo_sync_running, count: number_with_delimiter(status.processed),
time: format_time(status.started_at)) %>
<% else %>
<%= l(:text_qbo_sync_queued) %>
<% end %>
</li>
<% end %>
</ul>
</div>
<%= javascript_tag do %>
// Refresh while QuickBooks is syncing, so the figures fill in
setTimeout(function () { window.location.reload(); }, 30000);
<% end %>
<% end %>
<% if @report.unsynced_count > 0 %> <% if @report.unsynced_count > 0 %>
<p class="warning"><%= l(:text_report_needs_sync, count: @report.unsynced_count) %></p> <p class="warning"><%= l(:text_report_needs_sync, count: @report.unsynced_count) %></p>
<% end %> <% end %>
@@ -131,6 +155,7 @@
.qbo-bar-issue_hours { background: #9b7fc0; } .qbo-bar-issue_hours { background: #9b7fc0; }
.qbo-report-ratios { margin: 8px 4px; line-height: 1.7; } .qbo-report-ratios { margin: 8px 4px; line-height: 1.7; }
.qbo-report-range { color: #666; margin: 4px 0 10px; } .qbo-report-range { color: #666; margin: 4px 0 10px; }
.qbo-report-syncing ul { display: inline-block; text-align: left; margin: 6px 0 0; }
table.qbo-report-months td, table.qbo-report-months th { text-align: right; white-space: nowrap; } table.qbo-report-months td, table.qbo-report-months th { text-align: right; white-space: nowrap; }
table.qbo-report-months .month { text-align: left; } table.qbo-report-months .month { text-align: left; }
table.qbo-report-months .hours-start { border-left: 2px solid #ddd; } table.qbo-report-months .hours-start { border-left: 2px solid #ddd; }
+4
View File
@@ -328,6 +328,10 @@ en:
label_report_open_balance: "Still owed on these invoices" label_report_open_balance: "Still owed on these invoices"
label_report_invoiced_of_spent: "Invoiced vs. spent hours" label_report_invoiced_of_spent: "Invoiced vs. spent hours"
label_report_spent_of_estimated: "Spent vs. issues' estimated hours" label_report_spent_of_estimated: "Spent vs. issues' estimated hours"
text_report_syncing: "QuickBooks is still syncing, so these figures are incomplete until it finishes. This page refreshes every 30 seconds."
text_qbo_sync_running: "syncing, %{count} read so far (started %{time})"
text_qbo_sync_running_short: "Syncing… %{count} read"
text_qbo_sync_queued: "waiting to sync"
text_report_needs_sync: "%{count} estimates or invoices in this period don't have their total or hours yet. Run a full sync from QuickBooks to fill them in." text_report_needs_sync: "%{count} estimates or invoices in this period don't have their total or hours yet. Run a full sync from QuickBooks to fill them in."
text_report_notes: "Estimates, invoices and payments count by their QuickBooks date; their hours are the quantities of the labor products/services. Issues' estimated time counts issues created in the period, without subtasks. Spent time counts time logged in the period." text_report_notes: "Estimates, invoices and payments count by their QuickBooks date; their hours are the quantities of the labor products/services. Issues' estimated time counts issues created in the period, without subtasks. Spent time counts time logged in the period."
notice_payment_not_found: "Payment not found in QuickBooks" notice_payment_not_found: "Payment not found in QuickBooks"
@@ -0,0 +1,31 @@
#The MIT License (MIT)
#
#Copyright (c) 2026 rick barrette
#
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# Which QuickBooks syncs are queued or running, and how far along they are (shown on the business report and
# the settings page while a long sync runs)
class CreateQboSyncStatuses < ActiveRecord::Migration[7.0]
def up
return if table_exists?(:qbo_sync_statuses)
create_table :qbo_sync_statuses do |t|
t.string :entity, null: false
t.string :state, null: false
t.integer :processed, null: false, default: 0
t.datetime :started_at
t.timestamps
end
add_index :qbo_sync_statuses, :entity, unique: true
end
def down
drop_table :qbo_sync_statuses if table_exists?(:qbo_sync_statuses)
end
end