Class: Writefully::Process

Inherits:
Struct
  • Object
show all
Defined in:
lib/writefully/process.rb

Constant Summary collapse

JOBS =
{ 
  write:  -> (index) { Writefully.add_job :journalists, index.merge({task: :publish}) },
  remove: -> (index) { Wrotefully.add_job :journalists, index.merge({task: :remove})  }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config

Returns:

  • (Object)

    the current value of config



23
24
25
# File 'lib/writefully/process.rb', line 23

def config
  @config
end

Instance Method Details

#boot_listener!Object

this listener listens to the specified content folder queues the changes detected into the job queue



81
82
83
84
85
86
87
# File 'lib/writefully/process.rb', line 81

def boot_listener!
  listener = Listen.to config[:content], wait_for_delay: 2, &process_message
  listener.start
  while listener.listen?
    sleep 0.5
  end
end

#connect_to_database!Object

connect to db



51
52
53
# File 'lib/writefully/process.rb', line 51

def connect_to_database!
  ActiveRecord::Base.establish_connection(Writefully.db_config)
end

#listenObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/writefully/process.rb', line 25

def listen
  set_title
  set_options
  log_start
  load_models
  connect_to_database!
  start_news_agency!
  start_dispatcher!
  boot_listener!
end

#load_modelsObject



44
45
46
47
48
# File 'lib/writefully/process.rb', line 44

def load_models
  Writefully::Source.to_load.each do |model|
    require File.join(config[:app_directory], 'app', 'models', model)
  end
end

#log_startObject



67
68
69
# File 'lib/writefully/process.rb', line 67

def log_start
  Writefully.logger.info("This is doctor Frasier Crane. I'm listening...")
end

#process_messageObject



71
72
73
74
75
76
77
# File 'lib/writefully/process.rb', line 71

def process_message
  Proc.new do |modified, added, removed|
    queue_jobs(Indices.build_from(modified), :write)
    queue_jobs(Indices.build_from(added),    :write)
    queue_jobs(Indices.build_from(removed),  :remove)
  end
end

#queue_jobs(indices, action) ⇒ Object



94
95
96
# File 'lib/writefully/process.rb', line 94

def queue_jobs indices, action
  indices.uniq.each { |index| JOBS[action].call(index) if Source.valid_resources.include?(index[:resource]) }
end

#set_optionsObject



40
41
42
# File 'lib/writefully/process.rb', line 40

def set_options
  Writefully.options = config
end

#set_titleObject



36
37
38
# File 'lib/writefully/process.rb', line 36

def set_title
  $0 = "Writefully #{Writefully::VERSION}"
end

#start_dispatcher!Object

Dispatcher monitors job queue and throws job at workers



56
57
58
59
# File 'lib/writefully/process.rb', line 56

def start_dispatcher!
  Tools::Dispatcher.supervise_as :dispatch
  Tools::Retryer.supervise_as    :retryer
end

#start_news_agency!Object

Supervises the actors that manage all the work with converting content and sorting it into its place



63
64
65
# File 'lib/writefully/process.rb', line 63

def start_news_agency!
  NewsAgency.run!
end