Module: Heimdall

Extended by:
Heimdall
Included in:
Heimdall
Defined in:
lib/heimdall/heimdall_que.rb,
lib/heimdall/heimdall_web.rb,
lib/heimdall/heimdall_base.rb,
lib/heimdall/heimdall_model.rb,
lib/heimdall/heimdall_proxy.rb,
lib/heimdall/heimdall_start.rb,
lib/heimdall/heimdall_worker.rb,
lib/heimdall/heimdall_database.rb,
lib/heimdall/heimdall_model_task.rb,
lib/heimdall/heimdall_model_schedule.rb

Overview

Defined Under Namespace

Classes: Database, ObjectProxy, Que, Schedule, Task, Web, Worker

Constant Summary collapse

PER_PAGE =
50
Model =
Class.new(Sequel::Model(Heimdall::CONFIG[:db]))

Instance Method Summary collapse

Instance Method Details

#add(job_klass = nil, opts = {}) ⇒ Object

add job to job server



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/heimdall/heimdall_base.rb', line 87

def add job_klass = nil, opts = {}
  if block_given?
    Thread.current[:_heimdall_batch] = [Time.now.to_f, Digest::SHA1.hexdigest(rand.to_s)[0, 10]].join('').sub('.', '')

    begin
      yield self
    ensure
      Thread.current[:_heimdall_batch] = nil
    end
  else
    if Thread.current[:_heimdall_batch]
      opts = opts.merge _batch: Thread.current[:_heimdall_batch]
    end

    if job_klass.class == Symbol
      job_klass = "#{job_klass}_job".classify.constantize
    end

    server.add(job_klass, opts)

    true
  end
end

#call(env) ⇒ Object



37
38
39
# File 'lib/heimdall/heimdall_base.rb', line 37

def call env
  ::Heimdall::Web.call env
end

#configObject



45
46
47
# File 'lib/heimdall/heimdall_base.rb', line 45

def config
  CONFIG
end

#dbObject



82
83
84
# File 'lib/heimdall/heimdall_base.rb', line 82

def db
  CONFIG[:db]
end

#get_nextObject

run next job from job server



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
# File 'lib/heimdall/heimdall_base.rb', line 116

def get_next
  if (task = server.pop)
    que.add do
      begin
        job_class = task.job_class.new

        task.logger 'STARTED'

        Timeout::timeout job_class.cattr.timeout.to_i do
          job_class.call task.opts.to_hwia
          task.set_done job_class.log
        end
      rescue => error
        log_data = ["#{error.class}: #{error.message}"]

        if job_class.cattr.backtrace
          log_data.push error.backtrace.map{|el| "  #{el}"}.join($/)
          log_data.push ''
        end

        task.set_fail log_data.join("\n")
      end
    end
  end
end

#job_on(name, &block) ⇒ Object

Heimdall.job_on :fail do |task, status| …



143
144
145
146
147
148
149
150
151
# File 'lib/heimdall/heimdall_base.rb', line 143

def job_on name, &block
  allowed = [:all, :done, :start, :error, :fail]

  unless allowed.include?(name)
    raise ArgumentError, 'Event kind %s does not exist' % name
  end

  EVENT_HOOKS[name] = block
end

#log(text, screen = false) ⇒ Object



61
62
63
64
65
# File 'lib/heimdall/heimdall_base.rb', line 61

def log text, screen = false
  return if CONFIG.silent
  puts text if screen
  CONFIG.logger.info text
end

#loggerObject



67
68
69
# File 'lib/heimdall/heimdall_base.rb', line 67

def logger
  CONFIG.logger
end

#queObject



41
42
43
# File 'lib/heimdall/heimdall_base.rb', line 41

def que
  CONFIG.que
end

#restart(job_id) ⇒ Object



111
112
113
# File 'lib/heimdall/heimdall_base.rb', line 111

def restart job_id
  Task[job_id.to_i].update status_sid: 'a', remaining_runs: 1, scheduled_at: Time.at(1)
end

#runObject



6
7
8
9
# File 'lib/heimdall/heimdall_base.rb', line 6

def run
  start
  run_runner
end

#run_runnerObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/heimdall/heimdall_base.rb', line 11

def run_runner
  return if CONFIG['running']

  CONFIG.running = true

  CONFIG.active_jobs = ObjectSpace
    .each_object(Class)
    .select { |klass| klass < ::Heimdall::Worker }

  log 'HEIMDALL started with %s jobs (%s)' % [CONFIG.active_jobs.length, CONFIG.active_jobs.join(', ')], true

  # will run forever in background and get new jobs every CONFIG.sleep
  Thread.new do
    while CONFIG.running
      while que.has_free? && get_next
        # add all new jobs to a que with minimal sleep
        sleep 0.01
      end

      sleep CONFIG.sleep_lookup
    end
  end

  Schedule.run
end

#serverObject



78
79
80
# File 'lib/heimdall/heimdall_base.rb', line 78

def server
  CONFIG[:server] || raise('Heimdall not started')
end

#startObject



6
7
8
9
10
11
12
13
14
15
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
43
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/heimdall/heimdall_start.rb', line 6

def start
  return if CONFIG['db']

  CONFIG.db = Sequel.connect ENV.fetch('HEIMDALL_DB')

  # init tables unless exist
  unless CONFIG.db.tables.include?(:tasks)
    CONFIG.db.create_table :tasks do
      primary_key :id
      Time     :created_at
      Time     :scheduled_at
      Time     :started_at
      Time     :finished_at
      Time     :restart_at
      Integer  :total_runs
      Integer  :remaining_runs
      String   :batch
      String   :uid
      String   :job
      String   :opts
      String   :log
      String   :status_sid, limit: 1
    end

    CONFIG.db.add_index :tasks, :scheduled_at
    CONFIG.db.add_index :tasks, :restart_at
    CONFIG.db.add_index :tasks, :remaining_runs
    CONFIG.db.add_index :tasks, :uid
    CONFIG.db.add_index :tasks, :batch
    CONFIG.db.add_index :tasks, :status_sid
  end

  unless CONFIG.db.tables.include?(:schedules)
    CONFIG.db.create_table :schedules do
      primary_key :id
      String   :name
      String   :every
      String   :cron
      Time     :next_run_at
      Time     :last_run_at
    end
  end

  # allow custom locations
  log_location = ENV['HEIMDALL_LOG'] || './log/heimdall.log'
  log_location = STDOUT if log_location.upcase == 'STDOUT'

  # log errors to screen and file
  error_log_locations = [STDOUT]

  if log_location != STDOUT
    error_log_locations.push log_location.sub('.log', '_sql_errors.log')
  end

  for el in error_log_locations
    logger = Logger.new el, 1, 1_240_000
    logger.level = :error
    CONFIG.db.loggers << logger
  end

  # models can be loaded only when we have DB connection (Sequel gem req)
  require_relative 'heimdall_model'
  require_relative 'heimdall_model_schedule'
  require_relative 'heimdall_model_task'

  unless CONFIG[:server]
    # how many seconds to wait before getting new jobs
    CONFIG[:sleep_lookup] ||= 1

    # default number of retries
    CONFIG[:retries]      ||= 2

    # repeat failed job after
    CONFIG[:repeat_after] ||= 1.minute

    CONFIG[:logger_file]  ||= ENV['HEIMDALL_LOG'] || './log/heimdall.log'
    CONFIG[:logger_file]    = STDOUT if CONFIG[:logger_file].upcase == 'STDOUT'
    CONFIG[:logger]       ||= Logger.new(CONFIG.logger_file, 3, 10_240_000)
    CONFIG[:silent]       ||= false
    CONFIG[:que_size]     ||= 5

    CONFIG.que     = Heimdall::Que.new CONFIG.que_size
    CONFIG.server  = Heimdall::Database.new
  end
end

#stopObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/heimdall/heimdall_base.rb', line 49

def stop
  log 'HEIMDALL is stopping'

  CONFIG.running = false

  while !poll.done?
    sleep 0.3
  end

  log 'HEIMDALL STOPED'
end

#tasks(filter = {}, limit = 100) ⇒ Object



71
72
73
74
75
76
# File 'lib/heimdall/heimdall_base.rb', line 71

def tasks filter = {}, limit = 100
  Task
    .order(Sequel.lit('id desc'))
    .limit(limit)
    .all
end