Code Monkey home page Code Monkey logo

Comments (1)

adipasquale avatar adipasquale commented on June 2, 2024

after digging in the aasm codebase I understood a few things and I think I found a way to make it work:

Firstly aasm_fire_event is not the name of a hook that we can use like after, after_commit and others.
So event :run, aasm_fire_event: :do_something_inside_transaction actually does nothing, the aasm_fire_event kwarg is ignored.

Iā€™m not sure why the README says:

There are currently 3 transactional callbacks that can be handled on the event, and 2 transactional callbacks for all events.

  event           before_all_transactions
  event           before_transaction
  event           aasm_fire_event (within transaction)
  event           after_commit (if event successful)
  event           after_transaction
  event           after_all_transactions

I think that the correct hook name to use in my case is (simply) after, which seems to run after the actual state transition, but still within the transaction.

require "bundler/inline"

gemfile do
  source "https://rubygems.org"
  gem "aasm"
  gem "after_commit_everywhere"
  gem "activerecord"
  gem "sqlite3"
end

puts "AASM gem installed version: #{AASM::VERSION}"

conn = ActiveRecord::Base.establish_connection adapter: "sqlite3", database: "test.db"
conn.connection.execute "DROP TABLE IF EXISTS jobs;"
ActiveRecord::Migration.class_eval do
  create_table :jobs do |t|
    t.string :aasm_state
  end
end

class Job < ActiveRecord::Base
  include AASM

  attr_reader :state

  aasm do
    state :sleeping, initial: true
    state :running, :cleaning

    event :run, after: :do_something_inside_transaction do
      transitions from: :sleeping, to: :running
    end
  end

  def do_something_inside_transaction
    puts "in do_something_inside_transaction"
    raise StandardError, "raise on purpose"
  end
end

begin
  job = Job.create!
  puts "before, job.state is #{job.aasm_state}"
  res = job.run!
  puts "after, job state is #{job.reload.aasm_state}"
rescue StandardError => e
  puts "rescueing from exception, job state is #{job.reload.aasm_state}"
end

# AASM gem installed version: 5.5.0
# -- create_table(:jobs)
# -> 0.0005s
# before, job.state is sleeping
# in do_something_inside_transaction
# rescueing from exception, job state is sleeping

from aasm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.