add framework for post update pushes to pubsub

This commit is contained in:
r888888888
2016-08-19 17:51:47 -07:00
parent 5de9adf0f5
commit 3c42df51c9
5 changed files with 56 additions and 7 deletions

View File

@@ -1383,15 +1383,9 @@ class Post < ActiveRecord::Base
end
def notify_pubsub
return
return unless Danbooru.config.google_api_project
pubsub = Google::Apis::PubsubV1::PubsubService.new
pubsub.authorization = Google::Auth.get_application_default([Google::Apis::PubsubV1::AUTH_PUBSUB])
topic = "projects/#{Danbooru.config.google_api_project}/topics/post_updates"
request = Google::Apis::PubsubV1::PublishRequest.new(messages: [])
request.messages << Google::Apis::PubsubV1::Message.new(data: id.to_s)
pubsub.publish_topic(topic, request)
PostUpdate.insert(id)
end
end

23
app/models/post_update.rb Normal file
View File

@@ -0,0 +1,23 @@
class PostUpdate
def self.insert(post_id)
ActiveRecord::Base.execute_sql("insert into post_updates (post_id) values (?) on conflict do nothing", post_id)
end
def self.get
ActiveRecord::Base.select_values_sql("delete from post_updates returning post_id")
end
def self.push
return unless Danbooru.config.google_api_project
pubsub = Google::Apis::PubsubV1::PubsubService.new
pubsub.authorization = Google::Auth.get_application_default([Google::Apis::PubsubV1::AUTH_PUBSUB])
topic = "projects/#{Danbooru.config.google_api_project}/topics/post_updates"
post_ids = get()
post_ids.in_groups_of(1_000, false).each do |group|
request = Google::Apis::PubsubV1::PublishRequest.new(messages: group.map {|x| Google::Apis::PubsubV1::Message.new(data: x.to_s)})
pubsub.publish_topic(topic, request)
end
end
end