Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions app/controllers/clock_ins_controller.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
class ClockInsController < ApplicationController
include UserFinder
include Retryable

def upsert
current_time = Time.now

begin
with_retry do
ActiveRecord::Base.transaction do
last_unused_clock_in = ClockIn.where(
user_id: @user.id,
is_clocked_out: false)
is_clocked_out: false,
user_id: @user.id
)
.order(clocked_in_at: :desc)
.limit(1)
.lock(true)
Expand All @@ -27,14 +29,12 @@ def upsert
else
ClockIn.create!(
user_id: @user.id,
clocked_in_at: Time.now
clocked_in_at: current_time
)

render_success(@user.clock_ins_json, I18n.t('success.messages.clock_in_success'))
end
end
rescue ActiveRecord::ActiveRecordError => e
render_error(:unprocessable_entity, e.message)
end
end

Expand Down
28 changes: 28 additions & 0 deletions app/controllers/concerns/retryable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Retryable
MAX_RETRIES = 3
RETRY_DELAY = 0.5 # seconds

def with_retry(max_retries: MAX_RETRIES, delay: RETRY_DELAY)
attempts = 0

begin
yield
rescue ActiveRecord::Deadlocked => e
attempts += 1
Rails.logger.warn("Retryable Error: #{e.class} - #{e.message}. Attempt #{attempts} of #{max_retries}.")
if attempts < max_retries
sleep(delay * attempts)
retry
else
Rails.logger.error("Retries exhausted for #{e.class}: #{e.message}")
raise e
end
rescue ActiveRecord::ActiveRecordError => e
Rails.logger.error("ActiveRecord Error: #{e.class} - #{e.message}")
render_error(:unprocessable_entity, e.message)
rescue StandardError => e
Rails.logger.error("Unexpected Error: #{e.class} - #{e.message}")
render_error(:internal_server_error, e.message)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ def up
end

def down
add_index :user_follows, name: 'index_user_follows_on_follower_id_and_followee_id'
remove_index :user_follows, name: 'index_user_follows_on_follower_id_and_followee_id'
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AddIndexClockInsOnIsClockedOutAndUserId < ActiveRecord::Migration[7.0]
def up
add_index :clock_ins, [:is_clocked_out, :user_id], name: 'index_clock_ins_on_is_clocked_out_and_user_id'
remove_index :clock_ins, [:user_id, :clocked_in_at]
end

def down
remove_index :user_follows, name: 'index_clock_ins_on_is_clocked_out_and_user_id'
add_index :clock_ins, [:user_id, :clocked_in_at]
end
end
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2025_01_02_060751) do
ActiveRecord::Schema[7.0].define(version: 2025_01_03_014113) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "clock_ins", force: :cascade do |t|
t.bigint "user_id", null: false
t.datetime "clocked_in_at", null: false
t.boolean "is_clocked_out", default: false, null: false
t.index ["user_id", "clocked_in_at"], name: "index_clock_ins_on_user_id_and_clocked_in_at"
t.index ["is_clocked_out", "user_id"], name: "index_clock_ins_on_is_clocked_out_and_user_id"
t.index ["user_id"], name: "index_clock_ins_on_user_id"
end

Expand Down
1 change: 1 addition & 0 deletions system-design/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This document contains the details of ideas and implementation for time tracker
3. For any clock-ins, every second clock-in will be considered a clock out for the last clock-in for us to calculate sleep record.
4. Since the feature is quite simple, most part is enough with simple error message, but it is better to implement proper logging.
5. Need proper error handling because ActiveModel::Error's full_messages return message with key.
6. Need to implement retry mechanism for concurrent requests to mitigate deadlocks on locking.

## Non-Functional Requirements
1. **Scalability**
Expand Down