Class: Rooftop::SpektrixSync::SyncTask

Inherits:
Object
  • Object
show all
Defined in:
lib/rooftop/spektrix_sync/lib/sync_task.rb

Constant Summary collapse

PIDFILE =
ARGV.find{|a| a=~/pidname=[^$]+/}.try(:split, '=').try(:last) || 'sync'
PIDPATH =
"/tmp/rooftop-spektrix-#{PIDFILE}.pid"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(starting_at, opts = {}) ⇒ SyncTask

Returns a new instance of SyncTask.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rooftop/spektrix_sync/lib/sync_task.rb', line 16

def initialize(starting_at, opts={})
  if defined?(Rooftop::Rails)
    Rooftop::Rails.configuration.perform_object_caching = false
  end

  begin
    Rooftop.include_drafts = true
    if defined?(Rooftop::Rails)
      Rooftop::Rails.configuration.perform_object_caching = false
    end
    @starting_at = starting_at || DateTime.now
    @logger = SpektrixSync.logger || Logger.new(STDOUT)
    default_opts = {
      import_price_bands: false,
      import_ticket_types: false,
      import_prices: false,
      import_events: true,
      delete_orphan_events: false,
      accept_empty_rooftop_events: false,
      import_spektrix_description: true
    }
    @options = default_opts.merge!(opts)
    @logger.info("[spektrix] Running with options: #{@options.select {|k,v| k if v}.keys.join("[spektrix] , ")}")
  rescue => e
    @logger.fatal("[spektrix] Couldn't start sync: #{e}")
  end
end

Instance Attribute Details

#error_fileObject

Returns the value of attribute error_file.



4
5
6
# File 'lib/rooftop/spektrix_sync/lib/sync_task.rb', line 4

def error_file
  @error_file
end

#loggerObject

Returns the value of attribute logger.



4
5
6
# File 'lib/rooftop/spektrix_sync/lib/sync_task.rb', line 4

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/rooftop/spektrix_sync/lib/sync_task.rb', line 5

def options
  @options
end

#rooftop_eventsObject (readonly)

Returns the value of attribute rooftop_events.



5
6
7
# File 'lib/rooftop/spektrix_sync/lib/sync_task.rb', line 5

def rooftop_events
  @rooftop_events
end

#rooftop_price_bandsObject (readonly)

Returns the value of attribute rooftop_price_bands.



5
6
7
# File 'lib/rooftop/spektrix_sync/lib/sync_task.rb', line 5

def rooftop_price_bands
  @rooftop_price_bands
end

#rooftop_price_listsObject (readonly)

Returns the value of attribute rooftop_price_lists.



5
6
7
# File 'lib/rooftop/spektrix_sync/lib/sync_task.rb', line 5

def rooftop_price_lists
  @rooftop_price_lists
end

#rooftop_ticket_typesObject (readonly)

Returns the value of attribute rooftop_ticket_types.



5
6
7
# File 'lib/rooftop/spektrix_sync/lib/sync_task.rb', line 5

def rooftop_ticket_types
  @rooftop_ticket_types
end

#spektrix_eventsObject (readonly)

Returns the value of attribute spektrix_events.



5
6
7
# File 'lib/rooftop/spektrix_sync/lib/sync_task.rb', line 5

def spektrix_events
  @spektrix_events
end

#spektrix_price_listsObject (readonly)

Returns the value of attribute spektrix_price_lists.



5
6
7
# File 'lib/rooftop/spektrix_sync/lib/sync_task.rb', line 5

def spektrix_price_lists
  @spektrix_price_lists
end

#starting_atObject

Returns the value of attribute starting_at.



4
5
6
# File 'lib/rooftop/spektrix_sync/lib/sync_task.rb', line 4

def starting_at
  @starting_at
end

Class Method Details

.run(starting_at = nil, opts = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rooftop/spektrix_sync/lib/sync_task.rb', line 74

def self.run(starting_at=nil, opts={})
  sync_pid = Process.get_pid(Rooftop::SpektrixSync::SyncTask::PIDPATH)

  if sync_pid && Process.exists?(sync_pid.to_i)
    raise 'Rooftop::SpektrixSync::SyncRunning'
  end

  # if we're here, we can create a new pidfile and start the sync
  Process.create_pid(Rooftop::SpektrixSync::SyncTask::PIDPATH)
  self.new(starting_at,opts).run

  # remove the pid
  Process.remove_pidfile(Rooftop::SpektrixSync::SyncTask::PIDPATH)
end

.run_events_import(starting_at = nil, event_id = nil, opts = {}) ⇒ Object



89
90
91
92
# File 'lib/rooftop/spektrix_sync/lib/sync_task.rb', line 89

def self.run_events_import(starting_at=nil, event_id=nil, opts={})
  opts = event_id.present? ? opts.merge({spektrix_event_id: event_id}) : opts
  self.run(starting_at, opts)
end

.run_full_import(starting_at = nil, opts = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/rooftop/spektrix_sync/lib/sync_task.rb', line 94

def self.run_full_import(starting_at=nil, opts={})
  self.run(starting_at, {
    import_price_bands: true,
    import_ticket_types: true,
    import_prices: true,
    import_events: true,
    delete_orphan_events: false
  }.merge(opts))
end

.run_prices_import(starting_at = nil, opts = {}) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/rooftop/spektrix_sync/lib/sync_task.rb', line 104

def self.run_prices_import(starting_at=nil, opts={})
  self.run(starting_at, {
    import_price_bands: true,
    import_ticket_types: true,
    import_prices: true,
    import_events: false,
    delete_orphan_events: false
  }.merge(opts))
end

Instance Method Details

#fetch_rooftop_and_spektrix_dataObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rooftop/spektrix_sync/lib/sync_task.rb', line 44

def fetch_rooftop_and_spektrix_data
  @spektrix_events = @spektrix_events.present? ? @spektrix_events : Spektrix::Events::Event.all(instance_start_from: @starting_at.iso8601).to_a
  if @options[:spektrix_event_id]
    @logger.info("[spektrix] Selecting single Spektrix event")
    @spektrix_events = @spektrix_events.select {|e| e.id == @options[:spektrix_event_id].to_s}
  else
    @logger.info("[spektrix] Fetching all Spektrix events")
  end

  @logger.info("[spektrix] Fetching all Rooftop events")
  @rooftop_events = Rooftop::Events::Event.all.to_a
  unless @options[:accept_empty_rooftop_events]
    @logger.info("[spektrix] No Rooftop events")
    raise StandardError, "Rooftop returned an empty set of events which is probably wrong" if @rooftop_events.empty?
  end
  @logger.info("[spektrix] Fetching all Spektrix price lists")
  @spektrix_price_lists = @spektrix_price_lists.present? ? @spektrix_price_lists : Spektrix::Tickets::PriceList.all.to_a
  @logger.info("[spektrix] Fetching all Rooftop Price lists")
  @rooftop_price_lists = Rooftop::Events::PriceList.all.to_a

  if @options[:import_price_bands] || @options[:import_ticket_types] || @options[:import_prices]
    @logger.info("[spektrix] Fetching all Spektrix price lists")
    @spektrix_price_lists = @spektrix_price_lists.present? ? @spektrix_price_lists : Spektrix::Tickets::PriceList.all.to_a
    @logger.info("[spektrix] Fetching all Rooftop ticket types")
    @rooftop_ticket_types = Rooftop::Events::TicketType.all.to_a
    @logger.info("[spektrix] Fetching all Rooftop price bands")
    @rooftop_price_bands = Rooftop::Events::PriceBand.all.to_a
  end
end

#runObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/rooftop/spektrix_sync/lib/sync_task.rb', line 115

def run
  begin
    if @options[:import_price_bands]
      fetch_rooftop_and_spektrix_data
      create_or_update_price_bands
    end
    if @options[:import_ticket_types]
      fetch_rooftop_and_spektrix_data
      create_or_update_ticket_types
    end
    if @options[:import_prices]
      fetch_rooftop_and_spektrix_data
      create_or_update_prices
    end
    if @options[:import_events]
      fetch_rooftop_and_spektrix_data
      create_or_update_events
    end
    # TODO: the delete method is over-eager. Resolve the issue.
    # if @options[:delete_orphan_events]
    #   fetch_event_data
    #   delete_orphan_spektrix_events
    # end
  rescue => e
    @logger.fatal("[spektrix] #{e}")
  end

end