Closed ota42y closed 8 years ago
bundle init
Gemfile gem "rails" gem 'delayed_job_active_record'
bundle install --path vendor/bundle Using bundler 1.11.2 Installing delayed_job 4.1.2 Installing rails 4.2.6
If you plan to use delayed_job with Mongoid〜
note rails generate delayed_job:active_record rails newしろよーエラー
note
Depending on your version of ruby, you may need to install ruby rdoc/ri data:
<= 1.8.6 : unsupported
= 1.8.7 : gem install rdoc-data; rdoc-data --install
= 1.9.1 : gem install rdoc-data; rdoc-data --install
>= 1.9.2 : nothing to do! Yay!
note
Installing delayed_job 4.1.2
Installing delayed_job_active_record 4.1.1
code
Running via Spring preloader in process 16874
create bin/delayed_job
chmod bin/delayed_job
create db/migrate/20160611022955_create_delayed_jobs.rb
code
rake aborted!
Gem::LoadError: You have already activated rake 10.4.2, but your Gemfile requires rake 11.1.2. Prepending `bundle exec` to your command may solve this.
files/workspace/delayed_job_test/config/boot.rb:3:in `<top (required)>'
files/workspace/delayed_job_test/config/application.rb:1:in `<top (required)>'
files/workspace/delayed_job_test/Rakefile:4:in `<top (required)>'
LoadError: cannot load such file -- bundler/setup
files/workspace/delayed_job_test/config/boot.rb:3:in `<top (required)>'
files/workspace/delayed_job_test/config/application.rb:1:in `<top (required)>'
files/workspace/delayed_job_test/Rakefile:4:in `<top (required)>'
(See full trace by running task with --trace)
code
rake --version
rake, version 10.4.2
bundle exec rake --version
rake, version 11.1.2
note
gem uninstall rake
ERROR: While executing gem ... (Gem::InstallError)
gem "rake" cannot be uninstalled because it is a default gem
readme にbundle exec rakeにして欲しい
note
rake --version
rake, version 11.1.2
note
== 20160611022955 CreateDelayedJobs: migrating ================================
-- create_table(:delayed_jobs, {:force=>true})
-> 0.0033s
-- add_index(:delayed_jobs, [:priority, :run_at], {:name=>"delayed_jobs_priority"})
-> 0.0006s
== 20160611022955 CreateDelayedJobs: migrated (0.0041s) =======================
どう扱うか検討しよう
note
invoke active_record
create db/migrate/20160611045154_create_users.rb
create app/models/user.rb
invoke test_unit
create test/models/user_test.rb
create test/fixtures/users.yml
note
class User < ActiveRecord::Base
def update_name(new_name)
@name = new_name
@name.save
end
end
note
== 20160611045154 CreateUsers: migrating ======================================
-- create_table(:users)
-> 0.0014s
== 20160611045154 CreateUsers: migrated (0.0015s) =============================
irb(main):001:0> u = User.create
(0.1ms) begin transaction
SQL (0.4ms) INSERT INTO "users" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2016-06-11 04:57:29.630331"], ["updated_at", "2016-06-11 04:57:29.630331"]]
(53.2ms) commit transaction
=> #<User id: 1, name: nil, created_at: "2016-06-11 04:57:29", updated_at: "2016-06-11 04:57:29">
irb(main):002:0> u.name = "test"
=> "test"
irb(main):003:0> User.first
User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
=> #<User id: 1, name: nil, created_at: "2016-06-11 04:57:29", updated_at: "2016-06-11 04:57:29">
irb(main):004:0> u.save
(0.1ms) begin transaction
SQL (0.3ms) UPDATE "users" SET "name" = ?, "updated_at" = ? WHERE "users"."id" = ? [["name", "test"], ["updated_at", "2016-06-11 05:02:18.719362"], ["id", 1]]
(50.6ms) commit transaction
=> true
irb(main):009:0* User.first
User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
=> #<User id: 1, name: "test", created_at: "2016-06-11 04:57:29", updated_at: "2016-06-11 05:02:18">
def update_name(new_name)
name = new_name
save
end
irb(main):006:0* u = User.create(name: 'before')
(0.2ms) begin transaction
SQL (0.3ms) INSERT INTO "users" ("name", "created_at", "updated_at") VALUES (?, ?, ?) [["name", "before"], ["created_at", "2016-06-11 05:06:42.352407"], ["updated_at", "2016-06-11 05:06:42.352407"]]
(14.8ms) commit transaction
=> #<User id: 5, name: "before", created_at: "2016-06-11 05:06:42", updated_at: "2016-06-11 05:06:42">
irb(main):007:0> u.update_name('after_sync')
(0.1ms) begin transaction
(0.1ms) commit transaction
=> true
irb(main):008:0> u
=> #<User id: 5, name: "before", created_at: "2016-06-11 05:06:42", updated_at: "2016-06-11 05:06:42">
irb(main):009:0> User.last
User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
=> #<User id: 5, name: "before", created_at: "2016-06-11 05:06:42", updated_at: "2016-06-11 05:06:42">
u = User.create(name: 'async_before')
(0.1ms) begin transaction
SQL (0.3ms) INSERT INTO "users" ("name", "created_at", "updated_at") VALUES (?, ?, ?) [["name", "async_before"], ["created_at", "2016-06-11 05:07:38.432976"], ["updated_at", "2016-06-11 05:07:38.432976"]]
(52.3ms) commit transaction
=> #<User id: 6, name: "async_before", created_at: "2016-06-11 05:07:38", updated_at: "2016-06-11 05:07:38">
u.delay.update_name('after_async')
(0.1ms) begin transaction
SQL (0.4ms) INSERT INTO "delayed_jobs" ("handler", "run_at", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["handler", "--- !ruby/object:Delayed::PerformableMethod\nobject: !ruby/object:User\n raw_attributes:\n name: async_before\n id: 6\n created_at: &1 2016-06-11 05:07:38.432976000 Z\n updated_at: *1\n attributes: !ruby/object:ActiveRecord::AttributeSet\n attributes: !ruby/object:ActiveRecord::LazyAttributeHash\n types:\n id: &4 !ruby/object:ActiveRecord::Type::Integer\n precision: \n scale: \n limit: \n range: !ruby/range\n begin: -2147483648\n end: 2147483648\n excl: true\n name: &3 !ruby/object:ActiveRecord::Type::String\n precision: \n scale: \n limit: \n created_at: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter\n subtype: &2 !ruby/object:ActiveRecord::Type::DateTime\n precision: \n scale: \n limit: \n updated_at: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter\n subtype: *2\n values:\n id: \n name: \n created_at: \n updated_at: \n additional_types: {}\n materialized: true\n delegate_hash:\n name: !ruby/object:ActiveRecord::Attribute::FromUser\n name: name\n value_before_type_cast: async_before\n type: *3\n value: async_before\n id: !ruby/object:ActiveRecord::Attribute::FromUser\n name: id\n value_before_type_cast: 6\n type: *4\n value: 6\n created_at: !ruby/object:ActiveRecord::Attribute::FromUser\n name: created_at\n value_before_type_cast: *1\n type: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter\n subtype: *2\n value: *1\n updated_at: !ruby/object:ActiveRecord::Attribute::FromUser\n name: updated_at\n value_before_type_cast: *1\n type: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter\n subtype: *2\n value: *1\n new_record: false\n active_record_yaml_version: 0\nmethod_name: :update_name\nargs:\n- after_async\n"], ["run_at", "2016-06-11 05:08:24.300926"], ["created_at", "2016-06-11 05:08:24.301219"], ["updated_at", "2016-06-11 05:08:24.301219"]]
(11.1ms) commit transaction
=> #<Delayed::Backend::ActiveRecord::Job id: 1, priority: 0, attempts: 0, handler: "--- !ruby/object:Delayed::PerformableMethod\nobject...", last_error: nil, run_at: "2016-06-11 05:08:24", locked_at: nil, failed_at: nil, locked_by: nil, queue: nil, created_at: "2016-06-11 05:08:24", updated_at: "2016-06-11 05:08:24">
u
=> #<User id: 6, name: "async_before", created_at: "2016-06-11 05:07:38", updated_at: "2016-06-11 05:07:38">
User.last
User Load (0.3ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
=> #<User id: 6, name: "async_before", created_at: "2016-06-11 05:07:38", updated_at: "2016-06-11 05:07:38">
Delayed::Backend::ActiveRecord::Job.first
Delayed::Backend::ActiveRecord::Job Load (0.2ms) SELECT "delayed_jobs".* FROM "delayed_jobs" ORDER BY "delayed_jobs"."id" ASC LIMIT 1
=> #<Delayed::Backend::ActiveRecord::Job id: 1, priority: 0, attempts: 0, handler: "--- !ruby/object:Delayed::PerformableMethod\nobject...", last_error: nil, run_at: "2016-06-11 05:08:24", locked_at: nil, failed_at: nil, locked_by: nil, queue: nil, created_at: "2016-06-11 05:08:24", updated_at: "2016-06-11 05:08:24">
irb(main):034:0> User.last
User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
=> #<User id: 6, name: "async_before", created_at: "2016-06-11 05:07:38", updated_at: "2016-06-11 05:07:38">
irb(main):035:0> u
=> #<User id: 6, name: "async_before", created_at: "2016-06-11 05:07:38", updated_at: "2016-06-11 05:07:38">
irb(main):036:0> u.reload
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 6]]
=> #<User id: 6, name: "async_before", created_at: "2016-06-11 05:07:38", updated_at: "2016-06-11 05:07:38">
irb(main):037:0> Delayed::Backend::ActiveRecord::Job.count
(0.2ms) SELECT COUNT(*) FROM "delayed_jobs"
=> 0
irb(main):038:0> u
=> #<User id: 6, name: "async_before", created_at: "2016-06-11 05:07:38", updated_at: "2016-06-11 05:07:38">
''' [Worker(host:MBA.local pid:46727)] Starting job worker [Worker(host:MBA.local pid:46727)] Job User#update_name (id=1) RUNNING [Worker(host:MBA.local pid:46727)] Job User#update_name (id=1) COMPLETED after 0.0196 [Worker(host:MBA.local pid:46727)] 1 jobs processed at 15.6516 j/s, 0 failed '''
t IS NULL) ORDER BY priority ASC, run_at ASC LIMIT 5
SQL (51.0ms) UPDATE "delayed_jobs" SET "locked_at" = '2016-06-11 05:29:20.436216', "locked_by" = 'host:MBA.local pid:46727' WHERE "delayed_jobs"."id" IN (SELECT "delayed_jobs"."id" FROM "delayed_jobs" WHERE ((run_at <= '2016-06-11 05:29:20.435786' AND (locked_at IS NULL OR locked_at < '2016-06-11 01:29:20.435800') OR locked_by = 'host:MBA.local pid:46727') AND failed_at IS NULL) AND "delayed_jobs"."id" = ? ORDER BY priority ASC, run_at ASC) [["id", 3]]
Delayed::Backend::ActiveRecord::Job Load (0.3ms) SELECT "delayed_jobs".* FROM "delayed_jobs" WHERE "delayed_jobs"."id" = ? LIMIT 1 [["id", 3]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 6]]
2016-06-11T14:29:20+0900: [Worker(host:MBA.local pid:46727)] Job User#update_name (id=3) RUNNING
(0.1ms) begin transaction
(0.1ms) commit transaction
(0.1ms) begin transaction
SQL (0.5ms) DELETE FROM "delayed_jobs" WHERE "delayed_jobs"."id" = ? [["id", 3]]
(55.9ms) commit transaction
2016-06-11T14:29:20+0900: [Worker(host:MBA.local pid:46727)] Job User#update_name (id=3) COMPLETED after 0.0597
class User < ActiveRecord::Base
def update_name(new_name)
puts new_name
name = new_name
save
end
end
irb(main):002:0> u.update_name('test')
test
(0.1ms) begin transaction
SQL (0.3ms) UPDATE "users" SET "name" = ?, "updated_at" = ? WHERE "users"."id" = ? [["name", "test"], ["updated_at", "2016-06-11 05:34:36.231199"], ["id", 8]]
(49.6ms) commit transaction
=> true
irb(main):003:0> u.
Display all 375 possibilities? (y or n)
irb(main):003:0> User.last
User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
=> #<User id: 8, name: "test", created_at: "2016-06-11 05:34:30", updated_at: "2016-06-11 05:34:36">
User.last
User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
=> #<User id: 8, name: "test_async", created_at: "2016-06-11 05:34:30", updated_at: "2016-06-11 05:35:11">
できてた
u.nameではアクセスできるのに、インスタンスメソッド内部で
def update_name(new_name)
name = new_name
save
end
とやるとアクセスできない…(´・_・`)?
prority無し
def update_name_low(new_name)
self.name = new_name
save
end
def update_name_high(new_name)
self.name = new_name
save
end
u = User.last
u.delay.update_name_low('low_name')
u.delay.update_name_high('hih_name')
User.last # not change
User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
=> #<User id: 8, name: "test_async", created_at: "2016-06-11 05:34:30", updated_at: "2016-06-11 05:35:11">
bundle exec rake jobs:work 実行
rails 2 not support so delete this line? See the 2.0 branch for Rails 2.
bundle exec rakeが正しいのか bin/rakeが正しいのか?
railsのおすすめドキュメントにどう書いてあるか
OSS Gate へようこそ。
Workshop では一人ずつ GitHub issue を立ててそこで作業ログを残していきながら進めます。 過去のビギナーのものはこちらで見られます。 https://github.com/oss-gate/workshop/issues?q=is%3Aissue+is%3Aclosed
この issue 作成時点でまずやること
作業開始してからやること
以下を参考にしながらやってみます。
https://github.com/oss-gate/workshop/blob/master/tutorial/scenario.md
作業ログについての説明(わからなくなったら都度読み返してください)
作業ログは、ビギナーが、このワークショップを通して得るものをより増やすために重要になります。なぜなら作業ログがメンターからのフィードバックをより充実させるからです。
作業ログを元にメンターと「ふりかえり」をするタイミングがあります。「ふりかえり」では次のことをします。
メンターは次のようなフィードバックをします。これは、ビギナーとは違う視点からビギナーの行動を観察することおよびメンターの経験があるからできることです。(「ふりかえり」の前にこんなフィードバックをよろしくお願いします!とお願いすると効果が高まるので実践してみましょう。)
このように、ビギナーの作業をメンター視点で一緒に整理し、ビギナーの今後の行動に活かす活動がここでいう「ふりかえり」です。そのため、「ビギナーにとって」ログに残すべきかどうか、という視点ではなく、「とりあえずログに残す」という視点でログを残してください。これは、ビギナーが重要だと判断しなくてもメンターの視点から見たら大事なこともあるからです。
ログに残すときは次のようなときです。
ログに残すことは次のことです。「備考」以外は作業の邪魔にならないように一言でよいです。備考は作業に役立つので必要な分だけ書いてください。
ログはコメントとして追記していってください。テンプレートは次の通りです。
例1(備考なし):
例2:
必要なファイルが足りないのかなぁ。