script_tracker 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -14
- data/README.md +59 -221
- data/lib/script_tracker/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1035933e320fe159fddb60f9f99e67f3d707250f8e6ba1901460973995df3567
|
|
4
|
+
data.tar.gz: 203bb6d7a9139e9e9069b0f98017cbe1e682a52c9f7fef67f6ccde5dc503a607
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6a48bc5347dfdb40f412bff93e89a872e6a07756643e705f2f6a49232b5eed14ca742c229254ffd6759142b5444d57d1753d4ef74f2e74bf58d945baa051ed7e
|
|
7
|
+
data.tar.gz: 71a2e8e39d6a20d0da06517d43f190fe6811ca5164115a2c4171238b6900ecad0bebd2cdf3d93430e4161d171ba68219d0720a3464fba238a592681c813af05f
|
data/CHANGELOG.md
CHANGED
|
@@ -7,12 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.1.3] - 2025-01-17
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
- Simplified and streamlined README documentation
|
|
14
|
+
- Cleaned up CHANGELOG format
|
|
15
|
+
|
|
10
16
|
## [0.1.2] - 2025-01-17
|
|
11
17
|
|
|
12
18
|
### Fixed
|
|
13
|
-
- Updated GitHub Actions workflows to use v4 actions
|
|
19
|
+
- Updated GitHub Actions workflows to use v4 actions
|
|
20
|
+
- Fixed GitHub release permissions
|
|
14
21
|
- Refactored workflows to match successful gem release pattern
|
|
15
|
-
- Fixed RubyGems authentication configuration
|
|
16
22
|
|
|
17
23
|
## [0.1.1] - 2025-01-17
|
|
18
24
|
|
|
@@ -22,29 +28,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
22
28
|
- Manual release workflow via GitHub Actions UI
|
|
23
29
|
- Local release script (`bin/release`)
|
|
24
30
|
|
|
25
|
-
### Changed
|
|
26
|
-
- Improved workflow documentation in README
|
|
27
|
-
|
|
28
31
|
## [0.1.0] - 2025-01-17
|
|
29
32
|
|
|
30
33
|
### Added
|
|
31
|
-
- Initial release
|
|
34
|
+
- Initial release
|
|
32
35
|
- Script execution tracking with status management
|
|
33
36
|
- Transaction support for script execution
|
|
34
37
|
- Built-in logging and progress tracking
|
|
35
38
|
- Batch processing helpers
|
|
36
39
|
- Timeout support for long-running scripts
|
|
37
40
|
- Stale script cleanup functionality
|
|
38
|
-
- Rake tasks for managing scripts
|
|
39
|
-
- `scripts:create` - Create new scripts
|
|
40
|
-
- `scripts:run` - Run pending scripts
|
|
41
|
-
- `scripts:status` - View script status
|
|
42
|
-
- `scripts:rollback` - Rollback scripts
|
|
43
|
-
- `scripts:cleanup` - Cleanup stale scripts
|
|
41
|
+
- Rake tasks for managing scripts
|
|
44
42
|
- Comprehensive RSpec test suite
|
|
45
|
-
- Full documentation and examples
|
|
46
43
|
|
|
47
|
-
[Unreleased]: https://github.com/a-abdellatif98/script_tracker/compare/v0.1.
|
|
44
|
+
[Unreleased]: https://github.com/a-abdellatif98/script_tracker/compare/v0.1.3...HEAD
|
|
45
|
+
[0.1.3]: https://github.com/a-abdellatif98/script_tracker/compare/v0.1.2...v0.1.3
|
|
48
46
|
[0.1.2]: https://github.com/a-abdellatif98/script_tracker/compare/v0.1.1...v0.1.2
|
|
49
47
|
[0.1.1]: https://github.com/a-abdellatif98/script_tracker/compare/v0.1.0...v0.1.1
|
|
50
48
|
[0.1.0]: https://github.com/a-abdellatif98/script_tracker/releases/tag/v0.1.0
|
data/README.md
CHANGED
|
@@ -1,70 +1,35 @@
|
|
|
1
1
|
# ScriptTracker
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A Ruby gem that provides a migration-like system for managing one-off scripts in Rails applications with execution tracking, transaction support, and built-in logging.
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
|
-
* **Execution Tracking
|
|
8
|
-
* **Transaction Support
|
|
9
|
-
* **Status Management
|
|
10
|
-
* **Built-in Logging
|
|
11
|
-
* **Batch Processing
|
|
12
|
-
* **Timeout Support
|
|
13
|
-
* **Stale Script Cleanup**: Automatically identify and cleanup stuck scripts
|
|
14
|
-
* **Migration Generator**: Generate timestamped script files with templates
|
|
7
|
+
* **Execution Tracking** - Automatically tracks which scripts have been run and their status
|
|
8
|
+
* **Transaction Support** - Wraps script execution in database transactions
|
|
9
|
+
* **Status Management** - Track scripts as success, failed, running, or skipped
|
|
10
|
+
* **Built-in Logging** - Convenient logging methods with timestamps
|
|
11
|
+
* **Batch Processing** - Helper methods for processing large datasets efficiently
|
|
12
|
+
* **Timeout Support** - Configure custom timeouts for long-running scripts
|
|
15
13
|
|
|
16
14
|
## Installation
|
|
17
15
|
|
|
18
|
-
Add
|
|
16
|
+
Add to your Gemfile:
|
|
19
17
|
|
|
20
18
|
```ruby
|
|
21
19
|
gem 'script_tracker'
|
|
22
20
|
```
|
|
23
21
|
|
|
24
|
-
|
|
22
|
+
Then run:
|
|
25
23
|
|
|
26
24
|
```bash
|
|
27
25
|
bundle install
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
Or install it yourself as:
|
|
31
|
-
|
|
32
|
-
```bash
|
|
33
|
-
gem install script_tracker
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
## Setup
|
|
37
|
-
|
|
38
|
-
1. Install the gem using the generator:
|
|
39
|
-
|
|
40
|
-
```bash
|
|
41
26
|
rails generate script_tracker:install
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
This will:
|
|
45
|
-
* Create a migration for the `executed_scripts` table
|
|
46
|
-
* Create the `lib/scripts` directory for your scripts
|
|
47
|
-
|
|
48
|
-
2. Run the migration:
|
|
49
|
-
|
|
50
|
-
```bash
|
|
51
27
|
rails db:migrate
|
|
52
28
|
```
|
|
53
29
|
|
|
54
|
-
|
|
30
|
+
## Quick Start
|
|
55
31
|
|
|
56
|
-
|
|
57
|
-
# config/initializers/script_tracker.rb
|
|
58
|
-
ScriptTracker.configure do |config|
|
|
59
|
-
config.scripts_path = Rails.root.join('lib', 'scripts')
|
|
60
|
-
end
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
## Usage
|
|
64
|
-
|
|
65
|
-
### Creating a New Script
|
|
66
|
-
|
|
67
|
-
Generate a new script with a descriptive name:
|
|
32
|
+
### Create a Script
|
|
68
33
|
|
|
69
34
|
```bash
|
|
70
35
|
rake scripts:create["update user preferences"]
|
|
@@ -73,136 +38,71 @@ rake scripts:create["update user preferences"]
|
|
|
73
38
|
This creates a timestamped script file in `lib/scripts/` :
|
|
74
39
|
|
|
75
40
|
```ruby
|
|
76
|
-
# lib/scripts/20231117120000_update_user_preferences.rb
|
|
77
41
|
module Scripts
|
|
78
42
|
class UpdateUserPreferences < ScriptTracker::Base
|
|
79
43
|
def self.execute
|
|
80
|
-
log "Starting script
|
|
81
|
-
|
|
82
|
-
# Your script logic here
|
|
44
|
+
log "Starting script"
|
|
45
|
+
|
|
83
46
|
User.find_each do |user|
|
|
84
47
|
user.update!(preferences: { theme: 'dark' })
|
|
85
48
|
end
|
|
86
|
-
|
|
87
|
-
log "Script completed
|
|
49
|
+
|
|
50
|
+
log "Script completed"
|
|
88
51
|
end
|
|
89
52
|
end
|
|
90
53
|
end
|
|
91
54
|
```
|
|
92
55
|
|
|
93
|
-
###
|
|
94
|
-
|
|
95
|
-
Run all pending (not yet executed) scripts:
|
|
96
|
-
|
|
97
|
-
```bash
|
|
98
|
-
rake scripts:run
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
### Checking Script Status
|
|
102
|
-
|
|
103
|
-
View the status of all scripts:
|
|
56
|
+
### Run Scripts
|
|
104
57
|
|
|
105
58
|
```bash
|
|
106
|
-
rake scripts:
|
|
59
|
+
rake scripts:run # Run all pending scripts
|
|
60
|
+
rake scripts:status # View script status
|
|
61
|
+
rake scripts:rollback[filename] # Rollback a script
|
|
62
|
+
rake scripts:cleanup # Cleanup stale scripts
|
|
107
63
|
```
|
|
108
64
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
```
|
|
112
|
-
Scripts:
|
|
113
|
-
[SUCCESS] 20231117120000_update_user_preferences.rb (2.5s)
|
|
114
|
-
[PENDING] 20231117130000_cleanup_old_data.rb
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
### Rolling Back a Script
|
|
118
|
-
|
|
119
|
-
Remove a script from execution history (allows it to be run again):
|
|
120
|
-
|
|
121
|
-
```bash
|
|
122
|
-
rake scripts:rollback[20231117120000_update_user_preferences.rb]
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
### Cleaning Up Stale Scripts
|
|
126
|
-
|
|
127
|
-
Mark scripts stuck in "running" status as failed:
|
|
128
|
-
|
|
129
|
-
```bash
|
|
130
|
-
rake scripts:cleanup
|
|
131
|
-
```
|
|
132
|
-
|
|
133
|
-
## Advanced Features
|
|
134
|
-
|
|
135
|
-
### Skipping Scripts
|
|
65
|
+
## Advanced Usage
|
|
136
66
|
|
|
137
|
-
Skip
|
|
67
|
+
### Skip Script Conditionally
|
|
138
68
|
|
|
139
69
|
```ruby
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
if User.where(needs_update: true).count.zero?
|
|
144
|
-
skip! "No users need updating"
|
|
145
|
-
end
|
|
146
|
-
|
|
147
|
-
# Your script logic here
|
|
148
|
-
end
|
|
149
|
-
end
|
|
70
|
+
def self.execute
|
|
71
|
+
skip! "No users need updating" if User.where(needs_update: true).count.zero?
|
|
72
|
+
# Your logic here
|
|
150
73
|
end
|
|
151
74
|
```
|
|
152
75
|
|
|
153
76
|
### Custom Timeout
|
|
154
77
|
|
|
155
|
-
Override the default 5-minute timeout:
|
|
156
|
-
|
|
157
78
|
```ruby
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
def self.timeout
|
|
161
|
-
3600 # 1 hour in seconds
|
|
162
|
-
end
|
|
163
|
-
|
|
164
|
-
def self.execute
|
|
165
|
-
# Long-running logic here
|
|
166
|
-
end
|
|
167
|
-
end
|
|
79
|
+
def self.timeout
|
|
80
|
+
3600 # 1 hour in seconds
|
|
168
81
|
end
|
|
169
82
|
```
|
|
170
83
|
|
|
171
84
|
### Batch Processing
|
|
172
85
|
|
|
173
|
-
Process large datasets efficiently:
|
|
174
|
-
|
|
175
86
|
```ruby
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
process_in_batches(users, batch_size: 1000) do |user|
|
|
182
|
-
user.update!(processed: true)
|
|
183
|
-
end
|
|
184
|
-
end
|
|
87
|
+
def self.execute
|
|
88
|
+
users = User.where(processed: false)
|
|
89
|
+
process_in_batches(users, batch_size: 1000) do |user|
|
|
90
|
+
user.update!(processed: true)
|
|
185
91
|
end
|
|
186
92
|
end
|
|
187
93
|
```
|
|
188
94
|
|
|
189
95
|
### Progress Logging
|
|
190
96
|
|
|
191
|
-
Track progress during execution:
|
|
192
|
-
|
|
193
97
|
```ruby
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
processed += 1
|
|
203
|
-
log_progress(processed, total) if (processed % 100).zero?
|
|
204
|
-
end
|
|
205
|
-
end
|
|
98
|
+
def self.execute
|
|
99
|
+
total = User.count
|
|
100
|
+
processed = 0
|
|
101
|
+
|
|
102
|
+
User.find_each do |user|
|
|
103
|
+
# Process user
|
|
104
|
+
processed += 1
|
|
105
|
+
log_progress(processed, total) if (processed % 100).zero?
|
|
206
106
|
end
|
|
207
107
|
end
|
|
208
108
|
```
|
|
@@ -211,49 +111,22 @@ end
|
|
|
211
111
|
|
|
212
112
|
### ScriptTracker:: Base
|
|
213
113
|
|
|
214
|
-
Base class for all scripts.
|
|
215
|
-
|
|
216
114
|
**Class Methods:**
|
|
217
|
-
|
|
218
|
-
* `execute` - Implement this method with your script logic (required)
|
|
219
|
-
* `run` - Execute the script with transaction and error handling
|
|
115
|
+
* `execute` - Implement with your script logic (required)
|
|
220
116
|
* `timeout` - Override to set custom timeout (default: 300 seconds)
|
|
221
|
-
* `skip!(reason)` - Skip script execution
|
|
222
|
-
* `log(message, level: :info)` - Log a message
|
|
223
|
-
* `log_progress(current, total
|
|
224
|
-
* `process_in_batches(relation, batch_size: 1000, &block)` - Process
|
|
117
|
+
* `skip!(reason)` - Skip script execution
|
|
118
|
+
* `log(message, level: :info)` - Log a message
|
|
119
|
+
* `log_progress(current, total)` - Log progress percentage
|
|
120
|
+
* `process_in_batches(relation, batch_size: 1000, &block)` - Process in batches
|
|
225
121
|
|
|
226
122
|
### ScriptTracker:: ExecutedScript
|
|
227
123
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
**Scopes:**
|
|
231
|
-
|
|
232
|
-
* `successful` - Scripts that completed successfully
|
|
233
|
-
* `failed` - Scripts that failed
|
|
234
|
-
* `running` - Scripts currently running
|
|
235
|
-
* `skipped` - Scripts that were skipped
|
|
236
|
-
* `completed` - Scripts that finished (success or failed)
|
|
237
|
-
* `ordered` - Order by execution time ascending
|
|
238
|
-
* `recent_first` - Order by execution time descending
|
|
124
|
+
**Scopes:** `successful` , `failed` , `running` , `skipped` , `completed` , `ordered` , `recent_first`
|
|
239
125
|
|
|
240
126
|
**Class Methods:**
|
|
241
|
-
|
|
242
|
-
* `executed?(filename)` - Check if a script has been executed
|
|
243
|
-
* `mark_as_running(filename)` - Mark a script as running
|
|
127
|
+
* `executed?(filename)` - Check if script has been executed
|
|
244
128
|
* `cleanup_stale_running_scripts(older_than: 1.hour.ago)` - Clean up stale scripts
|
|
245
129
|
|
|
246
|
-
**Instance Methods:**
|
|
247
|
-
|
|
248
|
-
* `mark_success!(output, duration)` - Mark as successful
|
|
249
|
-
* `mark_failed!(error, duration)` - Mark as failed
|
|
250
|
-
* `mark_skipped!(output, duration)` - Mark as skipped
|
|
251
|
-
* `success?`, `failed?`, `running?`, `skipped?` - Status predicates
|
|
252
|
-
* `formatted_duration` - Human-readable duration
|
|
253
|
-
* `formatted_output` - Truncated output text
|
|
254
|
-
* `timeout_seconds` - Get timeout value
|
|
255
|
-
* `timed_out?` - Check if script has timed out
|
|
256
|
-
|
|
257
130
|
## Rake Tasks
|
|
258
131
|
|
|
259
132
|
* `rake scripts:create[description]` - Create a new script
|
|
@@ -262,68 +135,33 @@ ActiveRecord model for tracking script execution.
|
|
|
262
135
|
* `rake scripts:rollback[filename]` - Rollback a script
|
|
263
136
|
* `rake scripts:cleanup` - Cleanup stale running scripts
|
|
264
137
|
|
|
265
|
-
##
|
|
266
|
-
|
|
267
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
|
|
268
|
-
|
|
269
|
-
To install this gem onto your local machine, run `bundle exec rake install` .
|
|
270
|
-
|
|
271
|
-
### Releasing a New Version
|
|
272
|
-
|
|
273
|
-
#### Option 1: GitHub Actions (Recommended)
|
|
138
|
+
## Releasing
|
|
274
139
|
|
|
275
|
-
|
|
140
|
+
### GitHub Actions (Recommended)
|
|
276
141
|
|
|
277
|
-
1. Go to
|
|
278
|
-
2.
|
|
279
|
-
3.
|
|
280
|
-
4. Enter the version number (e.g., `0.1.1`)
|
|
281
|
-
5. Click **Run workflow**
|
|
142
|
+
1. Go to **Actions** → **Release** workflow
|
|
143
|
+
2. Click **Run workflow**
|
|
144
|
+
3. Enter version number (e.g., `0.1.3`)
|
|
282
145
|
|
|
283
|
-
|
|
284
|
-
* Run all tests across multiple Ruby versions
|
|
285
|
-
* Update version and CHANGELOG
|
|
286
|
-
* Build the gem
|
|
287
|
-
* Create git tag
|
|
288
|
-
* Push to git
|
|
289
|
-
* Push to RubyGems automatically
|
|
290
|
-
|
|
291
|
-
**Required Secrets**: Set `RUBYGEMS_AUTH_TOKEN` in GitHub repository secrets.
|
|
292
|
-
|
|
293
|
-
#### Option 2: Local Release Script
|
|
294
|
-
|
|
295
|
-
To release locally, use the release script:
|
|
146
|
+
Or push a tag:
|
|
296
147
|
|
|
297
148
|
```bash
|
|
298
|
-
|
|
149
|
+
git tag -a v0.1.3 -m "Release version 0.1.3"
|
|
150
|
+
git push origin v0.1.3
|
|
299
151
|
```
|
|
300
152
|
|
|
301
|
-
|
|
302
|
-
* Run all tests to ensure everything passes
|
|
303
|
-
* Update the version in `lib/script_tracker/version.rb`
|
|
304
|
-
* Update `CHANGELOG.md` with the new version
|
|
305
|
-
* Build the gem
|
|
306
|
-
* Create a git tag
|
|
307
|
-
* Push to git (if remote configured)
|
|
308
|
-
* Push to RubyGems
|
|
309
|
-
|
|
310
|
-
**Note**: The script requires:
|
|
311
|
-
* All tests passing
|
|
312
|
-
* No uncommitted changes
|
|
313
|
-
* RubyGems credentials configured (`gem credentials`)
|
|
314
|
-
|
|
315
|
-
#### Option 3: Rake Task
|
|
153
|
+
**Required:** Set `RUBYGEMS_AUTH_TOKEN` in GitHub repository secrets.
|
|
316
154
|
|
|
317
|
-
|
|
155
|
+
### Local Release
|
|
318
156
|
|
|
319
157
|
```bash
|
|
320
|
-
|
|
158
|
+
bin/release 0.1.3
|
|
321
159
|
```
|
|
322
160
|
|
|
323
161
|
## Contributing
|
|
324
162
|
|
|
325
|
-
Bug reports and pull requests
|
|
163
|
+
Bug reports and pull requests welcome at https://github.com/a-abdellatif98/script_tracker.
|
|
326
164
|
|
|
327
165
|
## License
|
|
328
166
|
|
|
329
|
-
|
|
167
|
+
MIT License - see LICENSE file for details.
|