- go to memory first, good for caching
brew install redis
- start server
redis-server
$ redis-server
78495:C 18 Mar 13:20:12.711 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
78495:M 18 Mar 13:20:12.713 * Increased maximum number of open files to 10032 (it was originally set to 7168).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.0.7 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 78495
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
78495:M 18 Mar 13:20:12.714 # Server started, Redis version 3.0.7
78495:M 18 Mar 13:20:12.714 * The server is now ready to accept connections on port 6379
- gem install redis
2.2.3 :001 > require "redis"
true
2.2.3 :004 > client = Redis.new
#<Redis client v3.2.2 for redis://127.0.0.1:6379/0>
2.2.3 :005 > client.set"greeting", "hello"
"OK"
2.2.3 :006 > client.get "greeting"
"hello"
-
GUI manager is rdm
-
put the following in application.rb
config.autoload_paths << Rails.root.join("app", "jobs")
config.autoload_paths << Rails.root.join("app", "uploaders")
config.active_job.queue_adapter = :sidekiq
- generate a job
bin/rails g job determine_campaign_state
DeterminCampaignStateJob.perform_now(Campaign.last)
- DeterminCampaignStateJob.rb file:
class DetermineCampaignStateJob < ActiveJob::Base
queue_as :default
def perform(*args)
campaign = args[0]
pledges_amount = campaign.pledges.sum(:amount)
if pledges_amount >= campaign.goal
campaign.fund!
else
campaign.unfund!
end
end
end
- add pledges to the last campaign
c = Campaign.last
11.times {c.pledges.create(amount: 10000000)}
c.aasm_state
"published"
2.2.3 :011 > DetermineCampaignStateJob.perform_now(c)
Performing DetermineCampaignStateJob from Sidekiq(default) with arguments: gid://fundsy/Campaign/8
(12.0ms) SELECT SUM("pledges"."amount") FROM "pledges" WHERE "pledges"."campaign_id" = $1 [["campaign_id", 8]]
(0.1ms) BEGIN
Campaign Exists (0.5ms) SELECT 1 AS one FROM "campaigns" WHERE ("campaigns"."name" = 'Hello try reward' AND "campaigns"."id" != 8) LIMIT 1
SQL (0.3ms) UPDATE "campaigns" SET "aasm_state" = $1, "updated_at" = $2 WHERE "campaigns"."id" = $3 [["aasm_state", "funded"], ["updated_at", "2016-03-18 20:47:31.720825"], ["id", 8]]
FriendlyId::Slug Load (0.5ms) SELECT "friendly_id_slugs".* FROM "friendly_id_slugs" WHERE "friendly_id_slugs"."sluggable_id" = $1 AND "friendly_id_slugs"."sluggable_type" = $2 ORDER BY "friendly_id_slugs".id DESC LIMIT 1 [["sluggable_id", 8], ["sluggable_type", "Campaign"]]
(6.3ms) COMMIT
Performed DetermineCampaignStateJob from Sidekiq(default) in 647.37ms
true
2.2.3 :012 > DetermineCampaignStateJob.perform_later(c)
Enqueued DetermineCampaignStateJob (Job ID: 93982f82-97aa-4247-b491-83b7c41cf071) to Sidekiq(default) with arguments: gid://fundsy/Campaign/8
#<DetermineCampaignStateJob:0x007fbd884c3dd8 @arguments=[#<Campaign id: 8, name: "Hello try reward", description: "hello", goal: 30, end_date: "2016-03-15 20:32:00", created_at: "2016-03-15 20:32:51", updated_at: "2016-03-18 20:47:31", user_id: nil, slug: "hello-try-reward", image: nil, aasm_state: "funded", address: "142 W Hasting Street", longitude: -123.1086604, latitude: 49.2819605>], @job_id="93982f82-97aa-4247-b491-83b7c41cf071", @queue_name="default">
- then open a new console, go to this project folder, run sidekiq
$ sidekiq
m,
`$b
.ss, $$: .,d$
`$$P,d$P' .,md$P"'
,$$$$$bmmd$$$P^'
.d$$$$$$$$$$P'
$$^' `"^$$$' ____ _ _ _ _
$: ,$$: / ___|(_) __| | ___| | _(_) __ _
`b :$$ \___ \| |/ _` |/ _ \ |/ / |/ _` |
$$: ___) | | (_| | __/ <| | (_| |
$$ |____/|_|\__,_|\___|_|\_\_|\__, |
.d$$ |_|
2016-03-18T20:50:04.200Z 79735 TID-ovs2j30u8 INFO: Running in ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]
2016-03-18T20:50:04.200Z 79735 TID-ovs2j30u8 INFO: See LICENSE and the LGPL-3.0 for licensing details.
2016-03-18T20:50:04.200Z 79735 TID-ovs2j30u8 INFO: Upgrade to Sidekiq Pro for more features and support: http://sidekiq.org
2016-03-18T20:50:04.200Z 79735 TID-ovs2j30u8 INFO: Booting Sidekiq 4.1.1 with redis options {:url=>nil}
2016-03-18T20:50:04.202Z 79735 TID-ovs2j30u8 INFO: Starting processing, hit Ctrl-C to stop
2016-03-18T20:50:04.246Z 79735 TID-ovs38trcs DetermineCampaignStateJob JID-bd0d84f5800a290c2e2598cb INFO: start
2016-03-18T20:50:04.617Z 79735 TID-ovs38trcs DetermineCampaignStateJob JID-bd0d84f5800a290c2e2598cb INFO: done: 0.37 sec
DetermineCampaignStateJob.set(wait_until: Time.now + 10.days).perform_later(c)
- routes
mount Sidekiq::Web, at: '/sidekiq'
PledgesMailer.notify_campaign_owner(Pledge.last)
- publishings_controller
class PublishingsController < ApplicationController
before_action :authenticate_user
def create
campaign = current_user.campaigns.friendly.find params[:campaign_id]
if campaign.publish!
DetermineCampaignStateJob.set(wait_until: campaign.end_date).perform_later(campaign)
redirect_to campaign, notice: "Published!"
else
redirect_to campaign, alert: "Can't publish! Published already?"
end
end
end