Skip to content

Commit

Permalink
[~] save progress method
Browse files Browse the repository at this point in the history
  • Loading branch information
skcc321 committed Oct 17, 2024
1 parent 924f5ed commit de269eb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ To use a different formatting value than the default, you need to specify it exp

## Resumable Tasks

When working with long-running code that processes a large dataset, it makes sense to store progress in the database to allow resuming the task later. You can do this by using the `DeployPin::Task` instance methods: `#progress` and `#increment_progress!(num)`.
When working with long-running code that processes a large dataset, it makes sense to store progress in the database to allow resuming the task later. You can do this by using the `DeployPin::Task` instance methods: `#progress`, `#save_progress!(num)` and `#increment_progress!(num)`.

Here is an example of how to use these methods:

Expand All @@ -191,6 +191,7 @@ Here is an example of how to use these methods:
Users.where(id: progress..).find_each do |user|
# Do some work
increment_progress!(1) # Increment progress by 1 and store it in the database so you can resume the task from this point
# or save_progress!(user.id) # Save the progress as the user id
end
```

Expand Down
6 changes: 6 additions & 0 deletions lib/deploy_pin/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ def increment_progress!(incrementor)
record.increment!(:progress, incrementor)
end

def save_progress!(value)
raise NotImplementedError, 'Recurring tasks do not support progress yet.' if recurring

record.update(progress: value)
end

def done?
return if recurring
return unless record
Expand Down
2 changes: 1 addition & 1 deletion lib/deploy_pin/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module DeployPin
VERSION = '1.7.1'
VERSION = '1.7.2'
end
11 changes: 11 additions & 0 deletions test/lib/deploy_pin/task_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ class DeployPin::Task::Test < ActiveSupport::TestCase
assert_equal @task.progress, 0
assert_nothing_raised { @task.increment_progress!(77) }
assert_equal @task.progress, 77
assert_nothing_raised { @task.increment_progress!(13) }
assert_equal @task.progress, 90
end

test 'save_progress!' do
@task.prepare
assert_equal @task.progress, 0
assert_nothing_raised { @task.save_progress!(13) }
assert_equal @task.progress, 13
assert_nothing_raised { @task.save_progress!(1) }
assert_equal @task.progress, 1
end

test 'parse' do
Expand Down

0 comments on commit de269eb

Please sign in to comment.