
Security News
AGENTS.md Gains Traction as an Open Format for AI Coding Agents
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
This gem execute passed block by once using database unique key.
class IdempotentExecutor < ApplicationRecord
# user_id :bigint(8) not null
# block_type :integer(11) not null
# signature :string(255) not null
# expired_time :datetime not null
#
# Indexes
#
# unique_index (user_id, type, signature) UNIQUE
include IdempotentBlock
enum block_type: [:post_create]
register_idempotent_column :user_id, :block_type, :signature
end
exec = IdempotentExecutor.new(user_id: user.id, block_type: :post_create, signature: 'abcdefg')
exec.start do
user.user_posts.create(params[:new_post])
end
# we don't execute block and raise IdempotentBlock::IdempotentError
exec.start do
user.user_posts.create(params[:new_post])
end
Add this line to your application's Gemfile:
gem 'idempotent_block'
And then execute:
$ bundle
Or install it yourself as:
$ gem install idempotent_block
exec_1 = IdempotentExecutor.new(user_id: user.id, block_type: :post_create, signature: 'abcdefg')
# first time, we got no error and executed block
exec_1.finished?
# => false
exec.start do
user.user_posts.create(params[:new_post])
end
user.user_posts.count
# => 1
exec_1.finished?
# => true
exec_1.executed?
# => true
# second time, we didn't execute block
exec_2 = IdempotentExecutor.new(user_id: user.id, block_type: :post_create, signature: 'abcdefg')
exec_2.finished?
# => true
exec_2.executed?
# => false
exec_2.start do
user.user_posts.create(params[:new_post])
end
user.user_posts.count
# => 1 we don't execute block
exec_2.finished?
# => true
exec_2.executed?
# => false
When you pass force option, we always execute block and update state.
exec.finished?
# => true
exec.start(force: true) do
user.user_posts.create(params[:new_post])
end
exec.executed?
# => true
If you raise error in block, we stop execute block and rollback state.
exec.start do
raise 'error'
end
# when retry, execute block because above block didn't completed
exec.start do
user.user_posts.create(params[:new_post])
end
exec = IdempotentExecutor.new(user_id: user.id, type: :post_create, signature: 'abcdefg')
exec.start do
user.user_posts.create(params[:new_post])
end
We use transaction so we rewrite this code like this.
exec = IdempotentExecutor.new(user_id: user.id, type: :post_create, signature: 'abcdefg')
ActiveRecord::Base.transaction do
user.user_posts.create(params[:new_post])
begin
exec.save!
exec.executed = true
exec.finished = true
rescue => ActiveRecord::RecordNotUnique
exec.finished = true
exec.executed = false
end
end
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/idempotent_block. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the IdempotentBlock project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
FAQs
Unknown package
We found that idempotent_block demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
Security News
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.