{"action":"created","issue":{"url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-ruby/issues/393","labels_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-ruby/issues/393/labels{/name}","comments_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-ruby/issues/393/comments","events_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-ruby/issues/393/events","html_url":"https://github.com/GoogleCloudPlatform/gcloud-ruby/issues/393","id":112895908,"number":393,"title":"Question: Most Ruby way to create a Pub/Sub topic?","user":{"login":"jeffmendoza","id":771387,"avatar_url":"https://avatars.githubusercontent.com/u/771387?v=3","gravatar_id":"","url":"https://api.github.com/users/jeffmendoza","html_url":"https://github.com/jeffmendoza","followers_url":"https://api.github.com/users/jeffmendoza/followers","following_url":"https://api.github.com/users/jeffmendoza/following{/other_user}","gists_url":"https://api.github.com/users/jeffmendoza/gists{/gist_id}","starred_url":"https://api.github.com/users/jeffmendoza/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jeffmendoza/subscriptions","organizations_url":"https://api.github.com/users/jeffmendoza/orgs","repos_url":"https://api.github.com/users/jeffmendoza/repos","events_url":"https://api.github.com/users/jeffmendoza/events{/privacy}","received_events_url":"https://api.github.com/users/jeffmendoza/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":17,"created_at":"2015-10-22T21:31:22Z","updated_at":"2015-10-26T18:20:17Z","closed_at":"2015-10-22T21:42:31Z","body":"This library allows a lot of flexibility\r\n\r\n```ruby\r\n# I think I'll go with\r\ntopic = pubsub.get_topic $topic_name\r\nif topic.nil?\r\n  topic = pubsub.create_topic $topic_name\r\nend\r\n\r\n#----\r\n\r\ntopic = pubsub.create_topic $topic_name unless pubsub.topic($topic_name).exists?\r\ntopic = pubsub.topic $topic_name\r\n\r\n#---\r\n\r\nif pubsub.topic($topic_name).exists?\r\n  topic = pubsub.topic $topic_name\r\nelse\r\n  topic = pubsub.create_topic $topic_name\r\nend\r\n\r\n#---\r\n\r\n# use autocreate\r\ntopic = pubsub.topic $topic_name\r\ntopic.publish \"message\"\r\n# need to create explicitly first to add a subscription though...\r\n\r\n#---\r\n```\r\ncc @thagomizer"},"comment":{"url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-ruby/issues/comments/151235900","html_url":"https://github.com/GoogleCloudPlatform/gcloud-ruby/issues/393#issuecomment-151235900","issue_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-ruby/issues/393","id":151235900,"user":{"login":"tmatsuo","id":41815,"avatar_url":"https://avatars.githubusercontent.com/u/41815?v=3","gravatar_id":"","url":"https://api.github.com/users/tmatsuo","html_url":"https://github.com/tmatsuo","followers_url":"https://api.github.com/users/tmatsuo/followers","following_url":"https://api.github.com/users/tmatsuo/following{/other_user}","gists_url":"https://api.github.com/users/tmatsuo/gists{/gist_id}","starred_url":"https://api.github.com/users/tmatsuo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tmatsuo/subscriptions","organizations_url":"https://api.github.com/users/tmatsuo/orgs","repos_url":"https://api.github.com/users/tmatsuo/repos","events_url":"https://api.github.com/users/tmatsuo/events{/privacy}","received_events_url":"https://api.github.com/users/tmatsuo/received_events","type":"User","site_admin":false},"created_at":"2015-10-26T18:20:17Z","updated_at":"2015-10-26T18:20:17Z","body":"There is one thing to note about the current Cloud Pub/Sub service.\r\n\r\nIf a topic doesn't have any subscriptions, the messages which sent to the topic will effectively get lost.\r\n\r\nSo:\r\n\r\n```ruby\r\ntopic = gcloud.pubsub.topic $topic_name, autocreate: true\r\ntopic.publish message\r\n#=> If the topic has just created and there are no subs, this message will go nowhere.\r\n```\r\n\r\nThis is raising another annoying issue where you observe some messages will get lost for no obvious reasons, unless you are aware of this fact.\r\n\r\nI would recommend that we advertise a clear strategy for ensuring your resources are ready, unless Cloud Pub/Sub changes the behavior.\r\n\r\nJust a thought, how about to have some methods for ensuring the existence of resources like `must_exist` and `must_have_subs`?\r\n\r\n```ruby\r\ntopic = gcloud.pubsub.topic.must_exist $topic_name # It will auto create the topic\r\n# We need to have multiple subscriptions\r\ntopic.must_have_subs $sub_array\r\n```\r\n\r\nThese are very similar for get_or_create, but they work naturally for both topic and subscriptions (think how we can provide get_or_create for multiple subscriptions). We need to state that no one should use these methods in any loops to avoid additional round trips. Maybe such code should run only once on application startup?\r\n\r\nBTW\r\nRe: fire-and-forget\r\nIt should not be discussed only for resource creations, but for garbage collections. No?\r\n"}}