mirror of
https://github.com/rickbarrette/stopwatch.git
synced 2026-11-17 19:50:36 -05:00
Timer safety: length limit, server-side saving, stop on close
- Maximum timer length setting (default 10h): a timer that runs past it is stopped at the limit and its entry's comment is flagged, so a forgotten timer can't add a night or weekend of hours - StopwatchTimerTickJob saves a running timer's time every minute on the server (no browser needed) and stops it at the limit on time; open tabs keep saving too - Timer updates lock the user's preference row and re-read the timer, so simultaneous saves (several tabs, the job) never add time twice - Closing an issue stops timers running on it before anything else (e.g. billing) uses its time; deleting a time entry only stops the timer running on that entry - Issue timers require a visible issue; starting again moves the timer instead of raising "timer is already running" - Tests updated and extended (limit, locking, tick job, close, delete, visibility, issue context menu); CI runs on Redmine 7.0 - Upstream's German notes translated Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
This commit is contained in:
15 files changed
+366
-36
No files matched your search
@@ -41,8 +41,11 @@ class StopwatchIssueTimersController < StopwatchController
|
||||
User.current.allowed_to?(:log_time, @project) or deny_access
|
||||
end
|
||||
|
||||
# Only issues the user can see (log_time on the project alone isn't enough)
|
||||
def find_issue
|
||||
@issue = Issue.find params[:issue_id]
|
||||
@issue = Issue.visible.find_by(id: params[:issue_id])
|
||||
return render_404 unless @issue
|
||||
|
||||
@project = @issue.project
|
||||
end
|
||||
end
|
||||
@@ -1,12 +1,12 @@
|
||||
# weiter
|
||||
# Ideas carried over from upstream (translated from the original German notes):
|
||||
#
|
||||
# - update title / menu item after starting new entry
|
||||
# - update time entry row after context menu stop (reflect saved time)
|
||||
# - 0:60 anstelle 1:00? Redmine-bug?
|
||||
# - update title / menu item after starting a new entry
|
||||
# - update the time entry row after stopping from the context menu (show the saved time)
|
||||
# - 0:60 instead of 1:00? Redmine bug?
|
||||
#
|
||||
# - remember last activity, preselect that in 'new' form
|
||||
# - same for project, unless we are in a project context
|
||||
# - focus first field that needs an action, depending on above
|
||||
# - remember the last activity and preselect it in the 'new' form
|
||||
# - same for the project, unless we are in a project context
|
||||
# - focus the first field that needs input, depending on the above
|
||||
#
|
||||
class StopwatchTimersController < StopwatchController
|
||||
|
||||
@@ -86,6 +86,8 @@ class StopwatchTimersController < StopwatchController
|
||||
|
||||
private
|
||||
|
||||
# Saves the elapsed time into the running entry (also done every minute on the server by
|
||||
# StopwatchTimerTickJob). Safe from several tabs at once: Timer#update locks the timer record.
|
||||
def find_timer
|
||||
@timer = Stopwatch::Timer.new User.current
|
||||
@timer.update
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
# Saves a running timer's time into its time entry every minute on the server, so the issue shows the
|
||||
# hours live even when nobody has Redmine open, and a forgotten timer is stopped at the maximum timer
|
||||
# length on time. Each start gets its own chain of jobs (identified by a token); a chain ends when the
|
||||
# timer stops or is restarted. Pending jobs are lost if Redmine restarts with the default in-process
|
||||
# job runner; the next start, or an open tab's once-a-minute check, keeps the time up to date meanwhile.
|
||||
class StopwatchTimerTickJob < ActiveJob::Base
|
||||
queue_as :default
|
||||
|
||||
INTERVAL = 1.minute
|
||||
|
||||
# Schedules the next tick, if the job backend supports delayed jobs (the :inline backend doesn't)
|
||||
def self.schedule(user_id, token)
|
||||
set(wait: INTERVAL).perform_later(user_id, token)
|
||||
rescue NotImplementedError => e
|
||||
Rails.logger.info "[Stopwatch] Server-side timer saving unavailable with this job backend: #{e.message}"
|
||||
end
|
||||
|
||||
def perform(user_id, token)
|
||||
user = User.find_by(id: user_id)
|
||||
return unless user
|
||||
|
||||
timer = Stopwatch::Timer.new(user)
|
||||
return unless timer.running? && timer.tick_token == token
|
||||
|
||||
timer.update
|
||||
self.class.schedule(user_id, token) if timer.running? && timer.tick_token == token
|
||||
rescue StandardError => e
|
||||
Rails.logger.error "[Stopwatch] Timer tick failed for user ##{user_id}: #{e.class}: #{e.message}"
|
||||
end
|
||||
end
|
||||
@@ -3,6 +3,12 @@
|
||||
<%= select_tag 'settings[default_activity]', options_from_collection_for_select( [['always_ask', t('.label_always_ask')], ['system', t('.label_system')]] + TimeEntryActivity.system.active.to_a.pluck(:id, :name), :first, :last, Stopwatch.settings['default_activity'] ) %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<label for="settings_max_timer_hours"><%= t '.label_max_timer_hours' %></label>
|
||||
<%= text_field_tag 'settings[max_timer_hours]', Stopwatch.settings['max_timer_hours'] || '10', size: 4, id: 'settings_max_timer_hours' %>
|
||||
<em class="info"><%= t '.text_max_timer_hours' %></em>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<label for="settings_highlighted_links"><%= t '.label_highlighted_links' %> </label>
|
||||
<%= hidden_field_tag 'settings[highlighted_links]', '0', id: nil %>
|
||||
|
||||
Reference in new issue
Block a user