/work/smata · jobs & work orders

Stopping cancelled work coming back to life.

In a busy work-order system, small timing gaps add up: a cancelled job reopens itself, an assignment half-finishes, an email goes out twice. I tracked down these gaps in SMATA's job lifecycle and closed them so they stay closed.

Product
SMATA, strata management SaaS
Scale
500,000+ users on the platform
My role
Investigation, 7 fixes and 92 regression tests
Stack
Rails · PostgreSQL · Sidekiq · RSpec

/problem

What was going wrong.

  • Cancelled work could still change state, and recurring work was rescheduled after cancellation.
  • A closed job's pending scheduled send could still fire, and a scheduled send could overwrite due dates.
  • Assigning a contractor could fail halfway and leave a stranded, half-assigned record.
  • Invoice updates wrote every history entry twice.
  • Work could still be actioned on accounts that were no longer managed.

/my-part

What I did.

01

One central guard

Added a single guard in the job state machine that blocks every transition after cancellation, instead of relying on checks scattered across callers.

02

No more ghost schedules

Stopped closed or cancelled work from re-firing scheduled sends and reminders.

03

All or nothing

Wrapped contractor assignment in a database transaction, with a spec proving it rolls back cleanly, and split the service into two clearer classes.

04

Clean history, right accounts

Removed duplicate history logging, and added guards so accounts that are no longer managed can't be acted on.

/approach

Fixed once, proven by tests.

Every fix came with regression tests, and about half of the 92 cover failure or 'must not' paths, such as a test that forces an assignment to fail and checks nothing is left behind. The central guard is cheap: at most one lookup per state change, and none when cancelling.

assign_contractor (simplified)
Job.transaction do
  job.assign_contractor!(contractor)
  job.schedule_notifications!
end
# all or nothing: no half-assigned jobs

/result

What changed.

7job-lifecycle bugs fixed, each with regression tests
92regression tests added, about half on failure paths
3 : 1lines of tests for every line of fix
  • Cancelled and closed jobs stay that way: one guard blocks every change after cancellation.
  • No half-assigned jobs and no duplicate history entries.
  • The bug classes behind ghost jobs, duplicate emails and overwritten due dates are closed.
  • 92 regression tests, about 3 lines of test for every line of fix, keep these bugs from coming back.

Credit where it's due. I worked on this as part of SMATA's core Rails team, with code review from teammates. The investigations, fixes and specs described here are my own commits.

Got a Rails app with problems like these?

Finding and closing gaps like these is what a rescue engagement does. Tell me what's happening, or book a call.

/more-work

Start a project