arask 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ad49ba051c8cfa4ab0b8c3328bfd37bba6f284ae
4
+ data.tar.gz: 5eeefd82e1a06a6a81fd063c3ef8fc38220aadde
5
+ SHA512:
6
+ metadata.gz: e54575c4eb12a57d78ebdd7ea408b034869f96bf032dfb2865e984b280a681ff066262232aed03da147cc5768096d87c7511c32fed43adbc961da60b2ddef43e
7
+ data.tar.gz: edbc56ec38e0373fb80085a63dce63ecb619ede93940c6ea6ae246adef394759ff3b4b8250c58df427488fd96db14359275d267da049480712b2f5112826c91f
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018 Esben Damgaard
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Arask
2
+ Automatic RAils taSKs.
3
+
4
+ Beware that these tasks are only run when ActionController has successfully rendered an action. The interval specified is the _least_ time that will go since last run.
5
+
6
+ ## Usage
7
+ After installation, you can edit config/initializers/arask.rb with your tasks.
8
+
9
+ ### Examples
10
+ ```ruby
11
+ arask.create script: 'puts "IM ALIVE!"', interval: :daily, run_first_time: true
12
+ arask.create script: 'Attachment.process_new', interval: 5.hours
13
+ # Run rake task:
14
+ arask.create task: 'my:awesome_task', interval: :hourly
15
+ ```
16
+
17
+ ## Installation
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'arask'
22
+ ```
23
+
24
+ And then execute:
25
+ ```bash
26
+ $ bundle
27
+ ```
28
+
29
+ Then install the initializer template and migration:
30
+ ```bash
31
+ $ rails generate arask:install
32
+ ```
33
+
34
+ Setup your tasks in config/initializers/arask.rb.
35
+
36
+ ## Contributing
37
+ Please use https://github.com/Ebbe/arask
38
+
39
+ ## License
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Arask'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,20 @@
1
+ module Arask
2
+ require 'rake'
3
+
4
+ class AraskJob < ActiveRecord::Base
5
+ def run
6
+ self.update_attribute(:execute_at, self.interval.seconds.from_now)
7
+ begin
8
+ if self.job.start_with?('Rake::Task')
9
+ Rake.load_rakefile Rails.root.join( 'Rakefile' )
10
+ end
11
+ eval(self.job)
12
+ rescue Exception => e
13
+ # TODO: Alert broken job
14
+ puts 'Arask: Job failed'
15
+ p self
16
+ puts e.message
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,7 @@
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
5
+ # Run rake task:
6
+ #arask.create task: 'my:awesome_task', interval: :hourly
7
+ end
@@ -0,0 +1,4 @@
1
+ module Arask
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module Arask
2
+ VERSION = '0.1.1'
3
+ end
data/lib/arask.rb ADDED
@@ -0,0 +1,77 @@
1
+ require "arask/railtie"
2
+ require "arask/arask_job"
3
+
4
+ module Arask
5
+ class << self; attr_accessor :time_cache; end
6
+
7
+ def self.setup
8
+ @jobs_touched = []
9
+ yield Arask
10
+ begin
11
+ AraskJob.all.where.not(id: @jobs_touched).delete_all
12
+ rescue
13
+ end
14
+ end
15
+
16
+ def self.create(script: nil, task: nil, interval:, run_first_time: false)
17
+ case interval
18
+ when :hourly
19
+ interval = 1.hour
20
+ when :daily
21
+ interval = 1.day
22
+ when :monthly
23
+ interval = 1.month
24
+ end
25
+ interval = interval.to_s.to_i
26
+ unless task.nil?
27
+ script = "Rake::Task['#{task}'].invoke"
28
+ end
29
+ if script.nil?
30
+ puts 'Arask: You did not specify a script or task to run!'
31
+ return
32
+ end
33
+ begin
34
+ job = AraskJob.where(job: script, interval: interval).first
35
+ if job
36
+ @jobs_touched << job.id
37
+ else
38
+ job = AraskJob.create(job: script, interval: interval, execute_at: interval.seconds.from_now)
39
+ @jobs_touched << job.id
40
+ if run_first_time===true
41
+ job.run
42
+ end
43
+ end
44
+ rescue ActiveRecord::StatementInvalid => e
45
+ puts 'Could not create arask job! Looks like the database is not migrated? Remember to run `rails generate arask:install` and `rails db:migrate`'
46
+ end
47
+ end
48
+
49
+
50
+ class RunJobs < ActiveJob::Base
51
+ queue_as :default
52
+
53
+ def perform
54
+ AraskJob.transaction do
55
+ AraskJob.where('execute_at < ?', DateTime.now).lock.each do |job|
56
+ job.run
57
+ end
58
+ end
59
+ next_job = AraskJob.order(execute_at: :desc).first
60
+ if next_job
61
+ Arask.time_cache = next_job.interval.seconds.from_now
62
+ else
63
+ Arask.time_cache = 5.minutes.from_now
64
+ end
65
+ end
66
+ end
67
+
68
+ ActionController::Base.instance_eval do
69
+ after_action do
70
+ Arask.time_cache ||= Time.now
71
+ if Arask.time_cache <= Time.now
72
+ Arask.time_cache = 5.minutes.from_now
73
+ RunJobs.perform_later
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,13 @@
1
+ require 'rails/generators'
2
+ require "rails/generators/active_record"
3
+
4
+ class Arask::InstallGenerator < Rails::Generators::Base
5
+ def initialize(*args, &block)
6
+ super
7
+
8
+ Arask::InstallGenerator.source_root File.expand_path('../../arask', __FILE__)
9
+ copy_file '../../arask/initialize.rb', 'config/initializers/arask.rb'
10
+
11
+ generate 'migration', 'create_arask_jobs job:string execute_at:datetime:index interval:integer'
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: arask
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Esben Damgaard
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-06-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 5.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 5.1.0
27
+ description: With minimal setup, be able to regularly run tasks.
28
+ email:
29
+ - esben@hvemder.dk
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - lib/arask.rb
38
+ - lib/arask/arask_job.rb
39
+ - lib/arask/initialize.rb
40
+ - lib/arask/railtie.rb
41
+ - lib/arask/version.rb
42
+ - lib/generators/arask/install_generator.rb
43
+ homepage: http://github.com/Ebbe/arask
44
+ licenses:
45
+ - MIT
46
+ metadata: {}
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 2.6.11
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: Automatic RAils taSKs
67
+ test_files: []