Class: BatchKit::Database::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/batch-kit/database/schema.rb

Overview

Manages the database tables and connection used to record batch-kit events.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Schema

Create a batch-kit schema instance

Parameters:

  • options (Hash) (defaults to: {})

    An options hash.

Options Hash (options):

  • :log_level (Symbol)

    The level of database messages that should be visible (default is :error).



21
22
23
24
# File 'lib/batch-kit/database/schema.rb', line 21

def initialize(options = {})
    @log = BatchKit::LogManager.logger('batch-kit.database.schema')
    @log.level = options.fetch(:log_level, :error)
end

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



13
14
15
# File 'lib/batch-kit/database/schema.rb', line 13

def log
  @log
end

Instance Method Details

#connect(*args) ⇒ Object

Connects to the database determined by args.

See Also:

  • for details on the different arguments that are required to connect to your database.


37
38
39
40
41
42
43
44
# File 'lib/batch-kit/database/schema.rb', line 37

def connect(*args)
    Sequel.default_timezone = :utc
    @conn = Sequel.connect(*args)
    @conn.loggers << @log
    @conn.autosequence = true

    create_tables unless deployed?
end

#connectionObject

Returns the Sequel database connection.



28
29
30
# File 'lib/batch-kit/database/schema.rb', line 28

def connection
    @conn
end

#create_tablesObject

Creates all database tables needed for tracking batch-kit processes.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/batch-kit/database/schema.rb', line 70

def create_tables
    # MD5 table, used to hold hashes of objects to detect version changes
    @conn.create_table?(:batch_md5) do
        primary_key :md5_id
        String :object_type, size: 30, null: false
        String :object_name, size: 255, null: false
        Fixnum :object_version, null: false
        String :md5_digest, size: 32, null: false
        DateTime :md5_created_at, null: false
        unique [:object_type, :object_name, :object_version]
    end

    # Job table, holds details of job definitions
    @conn.create_table?(:batch_job) do
        primary_key :job_id
        String :job_name, size: 80, null: false
        String :job_class, size: 80, null: false
        String :job_method, size: 80, null: false
        String :job_desc, size: 255, null: true
        String :job_host, size: 50, null: false
        String :job_file, size: 255, null: false
        Fixnum :job_version, null: false
        foreign_key :job_file_md5_id, :batch_md5, null: false
        Fixnum :job_run_count, null: false
        Fixnum :job_success_count, null: false
        Fixnum :job_fail_count, null: false
        Fixnum :job_abort_count, null: false
        Bignum :job_min_success_duration_ms, null: false
        Bignum :job_max_success_duration_ms, null: false
        Bignum :job_mean_success_duration_ms, null: false
        Bignum :job_m2_success_duration_ms, null: false
        DateTime :job_created_at, null: false
        DateTime :job_modified_at, null: false
        DateTime :job_last_run_at, null: true
        unique [:job_host, :job_name]
    end

    # Task table, holds details of task definitions
    @conn.create_table?(:batch_task) do
        primary_key :task_id
        foreign_key :job_id, :batch_job, null: false
        Fixnum :job_version, null: false
        String :task_name, size: 80, null: false
        String :task_class, size: 80, null: false
        String :task_method, size: 80, null: false
        String :task_desc, size: 255, null: true
        TrueClass :task_current_flag, null: false, default: true
        Fixnum :task_run_count, null: false
        Fixnum :task_success_count, null: false
        Fixnum :task_fail_count, null: false
        Fixnum :task_abort_count, null: false
        Bignum :task_min_success_duration_ms, null: false
        Bignum :task_max_success_duration_ms, null: false
        Bignum :task_mean_success_duration_ms, null: false
        Bignum :task_m2_success_duration_ms, null: false
        DateTime :task_created_at, null: false
        DateTime :task_modified_at, null: false
        DateTime :task_last_run_at, null: true
    end

    # Job run table, holds details of a single execution of a job
    @conn.create_table?(:batch_job_run) do
        primary_key :job_run
        foreign_key :job_id, :batch_job, null: false
        String :job_instance, size: 80, null: true
        Fixnum :job_version, null: false
        String :job_run_by, size: 50, null: false
        String :job_cmd_line, size: 2000, null: true
        DateTime :job_start_time, null: false
        DateTime :job_end_time, null: true
        String :job_status, size: 12, null: false
        Fixnum :job_pid, null: true
        Fixnum :job_exit_code, null: true
        TrueClass :job_purged_flag, null: false, default: false
    end

    # Job run arguments table, holds details of the arguments used on a job
    @conn.create_table?(:batch_job_run_arg) do
        foreign_key :job_run, :batch_job_run
        String :job_arg_name, size: 50, null: false
        String :job_arg_value, size: 255, null: true
        primary_key [:job_run, :job_arg_name]
    end

    # Job run log table, holds log records for a job
    @conn.create_table?(:batch_job_run_log) do
        foreign_key :job_run, :batch_job_run
        Fixnum :log_line, null: false
        DateTime :log_time, null: false
        String :log_name, size: 40, null: false
        String :log_level, size: 8, null: false
        String :thread_id, size: 8, null: true
        String :log_message, size: 1000, null: false
        primary_key [:job_run, :log_line]
    end

    # Job failure table, holds exception details for job failures
    @conn.create_table?(:batch_job_run_failure) do
        # We don't use an FK here, because we want to be able to retain
        # failure details longer than we retain job runs
        Fixnum :job_run, null: false
        foreign_key :job_id, :batch_job, null: false
        Fixnum :job_version, null: false
        DateTime :job_failed_at, null: false
        String :exception_message, size: 500, null: false
        String :exception_backtrace, size: 4000, null: false
    end

    # Task run table, holds details of a single execution of a task
    @conn.create_table?(:batch_task_run) do
        primary_key :task_run
        foreign_key :task_id, :batch_task, null: false
        foreign_key :job_run, :batch_job_run, null: false
        String :task_instance, size: 80, null: true
        DateTime :task_start_time, null: false
        DateTime :task_end_time, null: true
        String :task_status, size: 12, null: false
        Fixnum :task_exit_code, null: true
    end

    # Lock table, holds details of the current locks
    @conn.create_table?(:batch_lock) do
        String :lock_name, type: String, size: 50, unique_key: true
        foreign_key :job_run, :batch_job_run
        DateTime :lock_created_at, null: false
        DateTime :lock_expires_at, null: false
    end

    # Request table, holds details of job run requests
    @conn.create_table?(:batch_request) do
        primary_key :request_id
        String :request_type, size: 80, null: false
        String :request_label, size: 80, null: false
        String :request_source, size: 80, null: false
        String :job_host, size: 30, null: false
        String :job_file, size: 255, null: false
        String :job_args, size: 2000, null: true
        String :job_working_dir, size: 255, null: true
        TrueClass :processed_flag, default: false, null: false
        foreign_key :job_run, :batch_job_run, null: true
        DateTime :request_created_at, null: false
        DateTime :request_start_at, null: true
        DateTime :request_launched_at, null: true
    end

    # Requestor table, holds details of job run requestors
    @conn.create_table?(:batch_requestor) do
        foreign_key :request_id, :batch_request, null: false
        DateTime :requested_at, null: false
        String :requested_by, size: 80, null: false
        String :email_address, size: 100, null: true
    end
end

#deployed?Boolean

Returns true if the batch-kit database tables have been deployed.

Returns:

  • (Boolean)

    true if the batch-kit database tables have been deployed.



48
49
50
# File 'lib/batch-kit/database/schema.rb', line 48

def deployed?
    @conn.table_exists?(:batch_md5)
end

#drop_tablesObject

Drops all database tables used for tracking batch-kit processes.



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/batch-kit/database/schema.rb', line 54

def drop_tables
    @conn.drop_table(:batch_requestor)
    @conn.drop_table(:batch_request)
    @conn.drop_table(:batch_lock)
    @conn.drop_table(:batch_task_run)
    @conn.drop_table(:batch_task)
    @conn.drop_table(:batch_job_run_failure)
    @conn.drop_table(:batch_job_run_log)
    @conn.drop_table(:batch_job_run_arg)
    @conn.drop_table(:batch_job_run)
    @conn.drop_table(:batch_job)
    @conn.drop_table(:batch_md5)
end