Class: Writefully::Process

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



27
28
29
# File 'lib/writefully/process.rb', line 27

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



87
88
89
90
91
92
93
# File 'lib/writefully/process.rb', line 87

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



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

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

#listen(config) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/writefully/process.rb', line 29

def listen config
  @config = config

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

#load_modelsObject



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

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

#log_startObject



73
74
75
# File 'lib/writefully/process.rb', line 73

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

#process_messageObject



77
78
79
80
81
82
83
# File 'lib/writefully/process.rb', line 77

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



100
101
102
# File 'lib/writefully/process.rb', line 100

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

#set_optionsObject



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

def set_options
  Writefully.options = config
end

#set_titleObject



42
43
44
# File 'lib/writefully/process.rb', line 42

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

#start_dispatcher!Object

Dispatcher monitors job queue and throws job at workers



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

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



69
70
71
# File 'lib/writefully/process.rb', line 69

def start_news_agency!
  NewsAgency.run!
end