Template of a custom feed generator service for the Bluesky network in Ruby
1#!/usr/bin/env ruby
2
3$LOAD_PATH.unshift(File.expand_path('..', __dir__))
4
5require 'bundler/setup'
6require 'app/firehose_stream'
7
8$stdout.sync = true
9
10ActiveRecord::Base.logger = nil
11
12def print_help
13 puts "Usage: #{$0} [options...]"
14 puts "Options:"
15 puts
16 puts " * Showing progress: [default: show in development]"
17 puts " -p = show progress dots for each received message"
18 puts " -np = don't show progress dots"
19 puts
20 puts " * Logging status changes: [default: log in any mode]"
21 puts " -ns = don't log status changes"
22 puts
23 puts " * Logging post text: [default: -lm in development, -nl in production]"
24 puts " -lm = log text of matching posts"
25 puts " -la = log text of every post"
26 puts " -nl = don't log posts"
27 puts
28 puts " * Saving posts to db: [default: -da in development, -dm in production]"
29 puts " -da = save all posts to database"
30 puts " -dm = save only matching posts to database"
31 puts " -nd = don't save any posts"
32 puts
33 puts " * Replaying missed events: [default: -nr in development, -r in production]"
34 puts " -r = pass a cursor param when connecting to replay any missed events"
35 puts " -nr = don't replay missed events"
36 puts " -r12345 = start from this specific cursor"
37end
38
39firehose = FirehoseStream.new(ENV['FIREHOSE'])
40
41ARGV.each do |arg|
42 case arg
43 when '-p'
44 firehose.show_progress = true
45 when '-np'
46 firehose.show_progress = false
47 when '-ns'
48 firehose.log_status = false
49 when '-lm'
50 firehose.log_posts = :matching
51 when '-la'
52 firehose.log_posts = :all
53 when '-nl'
54 firehose.log_posts = false
55 when '-dm'
56 firehose.save_posts = :matching
57 when '-da'
58 firehose.save_posts = :all
59 when '-nd'
60 firehose.save_posts = false
61 when '-r'
62 firehose.replay_events = true
63 when /^\-r(\d+)$/
64 firehose.replay_events = true
65 firehose.start_cursor = $1.to_i
66 when '-nr'
67 firehose.replay_events = false
68 when '-h', '--help'
69 print_help
70 exit 0
71 else
72 puts "Unrecognized option: #{arg}"
73 print_help
74 exit 1
75 end
76end
77
78trap("SIGINT") {
79 puts
80 firehose.log "Stopping..."
81
82 EM.add_timer(0) {
83 firehose.stop
84 }
85}
86
87trap("SIGTERM") {
88 firehose.log "Shutting down the service..."
89
90 EM.add_timer(0) {
91 firehose.stop
92 }
93}
94
95firehose.start