Skip to content

Commit

Permalink
Autotweets (#1)
Browse files Browse the repository at this point in the history
* Sidekiq to tweet announcements

* Add staging env

* Sidekiq number of workers

* Correct twitter env vars

* Add deployment notes
  • Loading branch information
veelenga authored Jul 13, 2017
1 parent 8067a5a commit b5c25fb
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 28 deletions.
2 changes: 2 additions & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
web: ./app
worker: ./sidekiq -c 3
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,22 @@ $ crystal db/seed.cr
$ amber watch
```

## Deployment
## Deployment to Heroku

```
$ heroku create app-name --buildpack https://github.com/crystal-lang/heroku-buildpack-crystal.git
$ heroku buildpacks:add https://github.com/veelenga/heroku-buildpack-sidekiq.cr
$ git push heroku master
```

And set environment variables:

```
$ heroku config:set AMBER_ENV=production
$ heroku config:set GITHUB_ID=github_client_id
$ heroku config:set GITHUB_SECRET=github_client_secret
$ git push heroku master
$ heroku config:set TWITTER_CONSUMER_KEY=twitter_consumer_key
$ heroku config:set TWITTER_CONSUMER_SECRET=twitter_consumer_secret
$ heroku config:set TWITTER_ACCESS_TOKEN=twitter_access_token
$ heroku config:set TWITTER_ACCESS_TOKEN_SECRET=twitter_access_token_secret
```
31 changes: 10 additions & 21 deletions config/application.cr
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
require "amber"
require "yaml"
require "sidekiq"
require "multi_auth"
require "option_parser"

OptionParser.parse! do |opts|
opts.on("-p PORT", "--port PORT", "") do |opt_port|
end
end
AMBER_PORT = ENV["PORT"]? || 3008
AMBER_ENV = ENV["AMBER_ENV"]? || "development"

AMBER_ENV = ENV["AMBER_ENV"]? || "development"
SITE = YAML.parse(File.read "config/site.yml")[AMBER_ENV]
Sidekiq::Client.default_context = Sidekiq::Client::Context.new
MultiAuth.config("github", ENV.fetch("GITHUB_ID", ""), ENV.fetch("GITHUB_SECRET", ""))

Amber::Server.instance.config do |app|
app_path = __FILE__
app.name = "Crystal [ANN] web application."
app.port = (ENV["PORT"]? || "3008").to_i
app.port = AMBER_PORT.to_i
app.env = AMBER_ENV.to_s
app.log = ::Logger.new(STDOUT)
app.log.level = ::Logger::INFO
end

require "yaml"
SITE = YAML.parse(File.read "config/site.yml")[AMBER_ENV]

require "multi_auth"
MultiAuth.config("github", ENV.fetch("GITHUB_ID", ""), ENV.fetch("GITHUB_SECRET", ""))

require "micrate"
require "pg"
if AMBER_ENV != "development"
puts "Migrating data"
Micrate::DB.connection_url = ENV["DATABASE_URL"]?
Micrate::Cli.run_up
puts "Migration finished"
end
4 changes: 4 additions & 0 deletions config/site.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ development:
<<: *default
url: http://localhost:3008

staging:
<<: *default
url: https://crystal-ann-staging.herokuapp.com

production:
<<: *default
url: https://crystal-ann.com
4 changes: 1 addition & 3 deletions db/seed.cr
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require "amber"
require "../src/controllers/**"
require "../config/application"
require "../src/models/**"
require "../src/views/**"
require "../config/*"
require "./seeds/*"

ENV["AMBER_ENV"] = "development"
Expand Down
28 changes: 28 additions & 0 deletions shard.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ shards:
github: Amber-Crystal/amber
version: 0.1.4

baked_file_system:
github: schovi/baked_file_system
version: 0.9.4

db:
github: crystal-lang/crystal-db
version: 0.4.2
Expand All @@ -12,6 +16,18 @@ shards:
github: kemalyst/granite-orm
version: 0.6.2

kemal:
github: kemalcr/kemal
version: 0.19.0

kemal-csrf:
github: kemalcr/kemal-csrf
version: 0.2.0

kemal-session:
github: kemalcr/kemal-session
commit: 5cb3b5b207db92852651a7b7ec4f95d6478f8580

kemalyst-validators:
github: kemalyst/kemalyst-validators
version: 0.2.0
Expand All @@ -32,6 +48,10 @@ shards:
github: will/crystal-pg
version: 0.13.3

pool:
github: ysbaddaden/pool
version: 0.2.3

radix:
github: luislavena/radix
commit: 211418416adba540b594af2260e1b5d0c8877c9e
Expand All @@ -40,7 +60,15 @@ shards:
github: stefanwille/crystal-redis
version: 1.8.0

sidekiq:
github: mperham/sidekiq.cr
commit: d91d68f8064ea83d252a4002cf492f65516c7a70

slang:
github: jeromegn/slang
commit: b817c89c7e5ae39562710c0d6c7f42cee685e14f

twitter:
github: veelenga/twitter.cr
commit: d12bd67edcfcfe0867b33c9e5ebae2052b991857

7 changes: 7 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,10 @@ dependencies:

micrate:
github: juanedi/micrate

sidekiq:
github: mperham/sidekiq.cr
branch: master

twitter:
github: veelenga/twitter.cr
4 changes: 2 additions & 2 deletions src/controllers/announcement_controller.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "./application_controller"
require "../workers/tweet_announcement"

class AnnouncementController < ApplicationController
PER_PAGE = 10
Expand Down Expand Up @@ -33,10 +34,9 @@ class AnnouncementController < ApplicationController
announcement.user_id = current_user!.id

if announcement.valid? && announcement.save
flash["success"] = "Created Announcement successfully."
Workers::TweetAnnouncement.async.perform(announcement.id.not_nil!)
redirect_to "/announcements"
else
flash["danger"] = "Could not create Announcement!"
render("new.slang")
end
end
Expand Down
9 changes: 9 additions & 0 deletions src/crystal-ann.cr
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
require "amber"
require "micrate"
require "pg"
require "./controllers/**"
require "./mailers/**"
require "./models/**"
require "./views/**"
require "../config/*"

if AMBER_ENV != "development"
puts "Migrating data"
Micrate::DB.connection_url = ENV["DATABASE_URL"]?
Micrate::Cli.run_up
puts "Migration finished"
end

Amber::Server.instance.run
4 changes: 4 additions & 0 deletions src/models/announcement.cr
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,8 @@ class Announcement < Granite::ORM
def user
User.find(user_id)
end

def path
"/announcements/#{id}"
end
end
7 changes: 7 additions & 0 deletions src/sidekiq.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "../config/application"
require "./workers/**"
require "sidekiq/cli"

cli = Sidekiq::CLI.new
server = cli.configure { }
cli.run(server)
37 changes: 37 additions & 0 deletions src/workers/tweet_announcement.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require "sidekiq"
require "twitter"
require "../models/announcement"

module Workers
class TweetAnnouncement
include Sidekiq::Worker

def perform(id : Int64)
if announcement = Announcement.find(id)
tweet(announcement)
end
end

def twitter_client
@twitter_client ||=
Twitter::REST::Client.new ENV["TWITTER_CONSUMER_KEY"],
ENV["TWITTER_CONSUMER_SECRET"],
ENV["TWITTER_ACCESS_TOKEN"],
ENV["TWITTER_ACCESS_TOKEN_SECRET"]
end

def tweet(announcement)
logger.error "Tweeting Announcement ##{announcement.id}"
status = tweet_template announcement
twitter_client.post("/1.1/statuses/update.json", {"status" => status})
rescue e
logger.error "Unable to tweet Announcement ##{announcement.id} (#{status})"
logger.error "Reason: #{e.message}"
end

def tweet_template(announcement)
site_url = SITE["url"]
"#{announcement.title} #{site_url}#{announcement.path} #crystallang"
end
end
end

0 comments on commit b5c25fb

Please sign in to comment.