arask 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e0ccff09799aceb6f771f22a6d9d981ee0edcfcd
4
- data.tar.gz: 015acd74815a762ef5f837e0388e268613519952
3
+ metadata.gz: 655fe5d9cc2a34ed951492948dfb6074d5d2dc78
4
+ data.tar.gz: 96e999e71c7da10bd302a2e531ac03c151a6cf29
5
5
  SHA512:
6
- metadata.gz: b599908a948826e65cbfc794a1c94ca245199a47a75dad79711fe5880c8a9f912265b4b07342d84017ee7646612bdb701653d94bbe2600151f24df28910e476e
7
- data.tar.gz: d8a2fc39027e99da66b37be2fe442449f8c8257f6d61cd27a6752c3f7ebe924135237316a99fdf810f3816528f5c20b02e07c3944000155b4633bb9229201fe0
6
+ metadata.gz: 33142e354cdd8bacefa017f47bfded3bc1b2c91d4a48b3895e10e99dc0cb37906fd13ec9d4fc84faeea02744c5e362cc451fa1937248b6d0aee10ac58b21b63d
7
+ data.tar.gz: ec4ba50c19be1c1baabb22612bffff7649a765f8bcf019b747b3c0262eb93665b3ee2ae66803cddf5175c7b23d5402479ce9a0d78a8df307738ccfbdbf8f82c8
data/README.md CHANGED
@@ -1,7 +1,10 @@
1
1
  # Arask
2
+ [![Build Status](https://travis-ci.org/Ebbe/arask.svg?branch=master)](https://travis-ci.org/Ebbe/arask)
3
+ [![Gem Version](https://badge.fury.io/rb/arask.svg)](https://badge.fury.io/rb/arask)
4
+
2
5
  Automatic RAils taSKs (with minimal setup).
3
6
 
4
- No need to setup anything outside of Rails. If Rails is running, so is Arask.
7
+ No need to setup anything outside of Rails. If Rails is running, so is Arask. If Rails has been stopped, the next time rails starts, Arask will go through overdue jobs and perform them.
5
8
 
6
9
  Use cron syntax or simply define the interval.
7
10
 
@@ -10,13 +13,25 @@ After installation, you can edit config/initializers/arask.rb with your tasks.
10
13
 
11
14
  ### Examples of jobs in the initializer
12
15
  ```ruby
13
- arask.create script: 'puts "IM ALIVE!"', interval: :daily, run_first_time: true
14
- arask.create script: 'Attachment.process_new', interval: 5.hours if Rails.env.production?
16
+ # Rake tasks with cron syntax
15
17
  arask.create task: 'send:logs', cron: '0 2 * * *' # At 02:00 every day
16
18
  arask.create task: 'update:cache', cron: '*/5 * * * *' # Every 5 minutes
17
- # Run rake task:
19
+
20
+ # Scripts with interval (when time of day or month etc doesn't matter)
21
+ arask.create script: 'puts "IM ALIVE!"', interval: :daily
18
22
  arask.create task: 'my:awesome_task', interval: :hourly
19
- # On exceptions, send email
23
+ arask.create task: 'my:awesome_task', interval: 3.minutes
24
+
25
+ # Run an ActiveJob.
26
+ arask.create job: 'ImportCurrenciesJob', interval: 1.month
27
+
28
+ # Only run on production
29
+ arask.create script: 'Attachment.process_new', interval: 5.hours if Rails.env.production?
30
+
31
+ # Run first time. If the job didn't exist already when starting rails, run it:
32
+ arask.create script: 'Attachment.process_new', interval: 5.hours, run_first_time: true
33
+
34
+ # On exceptions, send email with details
20
35
  arask.on_exception email: 'errors@example.com'
21
36
  ```
22
37
 
@@ -45,7 +60,6 @@ Setup your tasks in config/initializers/arask.rb. Initially it looks [like this]
45
60
 
46
61
  ## Todos
47
62
  * Have a "try again" feature. For instance `arask.create script: 'raise "I failed"', interval: :daily, fail_retry: 5.minutes, retry_at_most: 2`
48
- * Tests
49
63
 
50
64
  ## Setup for Heroku
51
65
  None. But if you use a hobby dyno and it falls to sleep, so will Arask. As soon as the dyno wakes up, Arask will run any pending jobs.
data/Rakefile CHANGED
@@ -22,6 +22,7 @@ Rake::TestTask.new(:test) do |t|
22
22
  t.libs << 'test'
23
23
  t.pattern = 'test/**/*_test.rb'
24
24
  t.verbose = false
25
+ t.warning = false
25
26
  end
26
27
 
27
28
  task default: :test
@@ -1,11 +1,24 @@
1
1
  Arask.setup do |arask|
2
- # Examples
3
- #arask.create script: 'puts "IM ALIVE!"', interval: :daily, run_first_time: true
4
- #arask.create script: 'Attachment.process_new', interval: 5.hours if Rails.env.production?
2
+ ## Examples
3
+
4
+ # Rake tasks with cron syntax
5
5
  #arask.create task: 'send:logs', cron: '0 2 * * *' # At 02:00 every day
6
6
  #arask.create task: 'update:cache', cron: '*/5 * * * *' # Every 5 minutes
7
- # Run rake task:
7
+
8
+ # Scripts with interval (when time of day or month etc doesn't matter)
9
+ #arask.create script: 'puts "IM ALIVE!"', interval: :daily
8
10
  #arask.create task: 'my:awesome_task', interval: :hourly
9
- # On exceptions, send email
11
+ #arask.create task: 'my:awesome_task', interval: 3.minutes
12
+
13
+ # Run an ActiveJob.
14
+ #arask.create job: 'ImportCurrenciesJob', interval: 1.month
15
+
16
+ # Only run on production
17
+ #arask.create script: 'Attachment.process_new', interval: 5.hours if Rails.env.production?
18
+
19
+ # Run first time. If the job didn't exist already when starting rails, run it:
20
+ #arask.create script: 'Attachment.process_new', interval: 5.hours, run_first_time: true
21
+
22
+ # On exceptions, send email with details
10
23
  #arask.on_exception email: 'errors@example.com'
11
24
  end
data/lib/arask/setup.rb CHANGED
@@ -6,21 +6,8 @@ module Arask
6
6
  puts "Arask could not parse parameter for on_exception!" unless email.class == String
7
7
  end
8
8
 
9
- def self.create(script: nil, task: nil, interval: nil, cron: nil, run_first_time: false)
10
- unless interval.nil?
11
- case interval
12
- when :hourly
13
- interval = 1.hour
14
- when :daily
15
- interval = 1.day
16
- when :monthly
17
- interval = 1.month
18
- end
19
- interval = interval.to_s.to_i
20
- end
21
- unless cron.nil?
22
- interval = 'cron: ' + cron
23
- end
9
+ def self.create(script: nil, task: nil, job: nil, interval: nil, cron: nil, run_first_time: false)
10
+ interval = parse_interval_or_cron(interval, cron)
24
11
  if interval.nil?
25
12
  puts 'Arask: You did not specify either cron: or interval:! When should the task run?'
26
13
  return
@@ -28,6 +15,9 @@ module Arask
28
15
  unless task.nil?
29
16
  script = "Rake::Task['#{task}'].invoke"
30
17
  end
18
+ unless job.nil?
19
+ script = "#{job}.perform_now"
20
+ end
31
21
  if script.nil?
32
22
  puts 'Arask: You did not specify a script or task to run!'
33
23
  return
@@ -40,12 +30,32 @@ module Arask
40
30
  job = AraskJob.create(job: script, interval: interval)
41
31
  Arask.jobs_touched << job.id
42
32
  if run_first_time===true
43
- job.run
33
+ job.update_attribute(:execute_at, Time.now)
44
34
  end
45
35
  end
46
36
  rescue ActiveRecord::StatementInvalid => e
47
37
  puts 'Could not create arask job! Looks like the database is not migrated? Remember to run `rails generate arask:install` and `rails db:migrate`'
48
38
  end
49
39
  end
40
+
41
+ private
42
+ def self.parse_interval_or_cron(interval, cron)
43
+ unless interval.nil?
44
+ case interval
45
+ when :hourly
46
+ interval = 1.hour
47
+ when :daily
48
+ interval = 1.day
49
+ when :monthly
50
+ interval = 1.month
51
+ end
52
+ interval = interval.to_s.to_i
53
+ end
54
+ unless cron.nil?
55
+ interval = 'cron: ' + cron
56
+ end
57
+ return interval
58
+ end
59
+
50
60
  end
51
61
  end
data/lib/arask/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Arask
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
data/lib/arask.rb CHANGED
@@ -8,6 +8,8 @@ module Arask
8
8
  class << self; attr_accessor :jobs_touched, :exception_email, :exception_email_from; end
9
9
 
10
10
  def self.setup
11
+ # Make sure we only run setup if Rails is actually run as a server or testing.
12
+ return unless defined?(Rails::Server) or Rails.env.test?
11
13
  Arask.jobs_touched = []
12
14
  yield Setup
13
15
  begin
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arask
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Esben Damgaard
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-14 00:00:00.000000000 Z
11
+ date: 2018-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -52,20 +52,6 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: rspec
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
55
  description: With minimal setup, be able to regularly run tasks in Rails. Either user
70
56
  cron syntax or simply define an interval.
71
57
  email: