Class: INatGet::App::Task

Inherits:
Object
  • Object
show all
Includes:
Data::DSL
Defined in:
lib/inat-get/app/core/task.rb

Direct Known Subclasses

ERB

Defined Under Namespace

Classes: ERB

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Data::DSL

#AND, #ANYTHING, #NOT, #NOTHING, #OR, #Q, #console, #erb_report, #finish_time, #get_identification, #get_observation, #get_place, #get_project, #get_taxon, #get_user, #now, #select_identifications, #select_observations, #select_places, #select_projects, #select_taxa, #select_users, #start_time, #time_range, #today, #version, #version!, #version?, #version_alias

Constructor Details

#initialize(path, config, **opts) ⇒ Task

Returns a new instance of Task.



29
30
31
32
33
34
35
36
37
# File 'lib/inat-get/app/core/task.rb', line 29

def initialize path, config, **opts
  @config = config
  @path = path
  @name = File.basename path, '.*'
  @opts = opts
  @console = opts[:console]
  @api = opts[:api]
  @code = File.read @path
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



43
44
45
# File 'lib/inat-get/app/core/task.rb', line 43

def db
  @db
end

#nameObject

Returns the value of attribute name.



22
23
24
# File 'lib/inat-get/app/core/task.rb', line 22

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



20
21
22
# File 'lib/inat-get/app/core/task.rb', line 20

def path
  @path
end

Instance Method Details

#executeObject



39
40
41
# File 'lib/inat-get/app/core/task.rb', line 39

def execute
  instance_eval @code, @path
end

#loggerObject (private)



81
82
83
# File 'lib/inat-get/app/core/task.rb', line 81

def logger
  @logger ||= INatGet::App::ConsoleLogger::new @console, progname: self.name
end

#prepareObject



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
# File 'lib/inat-get/app/core/task.rb', line 45

def prepare
  Thread::current[:api] = @api
  Thread::current[:console] = @console
  Thread::current[:logger] = logger
  db_connect = @config.dig(:database, :connect)
  db_options = { user: @config.dig(:database, :user), password: @config.dig(:database, :password) }.compact
  @db = Sequel::connect(db_connect, **db_options)
  if @db.database_type == :sqlite
    @db.extend_datasets do
      def literal_datetime(value)
        "'#{value.xmlschema}'"
      end
      def literal_time(value)
        if value.instance_of?(::Time)
          "'#{value.xmlschema}'"
        else
          super(value)
        end
      end
    end
    @db.execute 'PRAGMA journal_mode=WAL'
    @db.execute 'PRAGMA synchronous=NORMAL'
    @db.execute 'PRAGMA busy_timeout=5000000'
  end
  Sequel::Model.require_valid_table = false
  Sequel::Model.strict_param_setting = false
  Sequel::Model.raise_on_save_failure = true
  Sequel::Model.db = @db
  Sequel::Model.db.loggers << ::Logger::new("sequel.log", level: :info)
  require_relative '../../data/models'
  require_relative '../../data/managers'
  # # ... etc
end