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