zfben_rails_jobs 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/.gitignore +6 -0
  2. data/Gemfile +4 -0
  3. data/README.rdoc +21 -0
  4. data/Rakefile +14 -0
  5. data/lib/tasks.rb +37 -0
  6. data/lib/zfben_rails_jobs.rb +138 -0
  7. data/test/dummy/.gitignore +1 -0
  8. data/test/dummy/Rakefile +7 -0
  9. data/test/dummy/app/controllers/application_controller.rb +3 -0
  10. data/test/dummy/app/helpers/application_helper.rb +2 -0
  11. data/test/dummy/app/models/example.rb +12 -0
  12. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  13. data/test/dummy/config/application.rb +20 -0
  14. data/test/dummy/config/boot.rb +10 -0
  15. data/test/dummy/config/database.yml +22 -0
  16. data/test/dummy/config/environment.rb +5 -0
  17. data/test/dummy/config/environments/development.rb +23 -0
  18. data/test/dummy/config/environments/production.rb +49 -0
  19. data/test/dummy/config/environments/test.rb +25 -0
  20. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  21. data/test/dummy/config/initializers/inflections.rb +10 -0
  22. data/test/dummy/config/initializers/mime_types.rb +5 -0
  23. data/test/dummy/config/initializers/secret_token.rb +7 -0
  24. data/test/dummy/config/initializers/session_store.rb +8 -0
  25. data/test/dummy/config/locales/en.yml +5 -0
  26. data/test/dummy/config/routes.rb +58 -0
  27. data/test/dummy/config.ru +4 -0
  28. data/test/dummy/public/404.html +26 -0
  29. data/test/dummy/public/422.html +26 -0
  30. data/test/dummy/public/500.html +26 -0
  31. data/test/dummy/public/favicon.ico +0 -0
  32. data/test/dummy/public/javascripts/application.js +2 -0
  33. data/test/dummy/public/javascripts/controls.js +965 -0
  34. data/test/dummy/public/javascripts/dragdrop.js +974 -0
  35. data/test/dummy/public/javascripts/effects.js +1123 -0
  36. data/test/dummy/public/javascripts/prototype.js +6001 -0
  37. data/test/dummy/public/javascripts/rails.js +202 -0
  38. data/test/dummy/public/stylesheets/.gitkeep +0 -0
  39. data/test/dummy/script/rails +6 -0
  40. data/test/jobs_test.rb +60 -0
  41. data/zfben_rails_jobs.gemspec +33 -0
  42. metadata +132 -0
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ *.log
3
+ .bundle
4
+ sass-cache
5
+ Gemfile.lock
6
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :rubygems
2
+
3
+ # Specify your gem's dependencies in zfben_rails_assets.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,21 @@
1
+ == SUMMARY
2
+
3
+
4
+ == Getting Started
5
+
6
+ 1. adding gem to Gemfile
7
+
8
+ gem 'zfben_rails_jobs'
9
+
10
+ 2. update bundle
11
+
12
+ bundle install
13
+
14
+ == How to use
15
+
16
+ class Sth
17
+ def todo args
18
+ end
19
+ end
20
+
21
+ Jobs.new.add(Sth, :todo, args)
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env rake
2
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
3
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
4
+
5
+ require 'bundler/gem_tasks'
6
+
7
+ require 'rake/testtask'
8
+
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = false
14
+ end
data/lib/tasks.rb ADDED
@@ -0,0 +1,37 @@
1
+ namespace :jobs do
2
+ jobs_root = File.realpath(Rails.root) << '/tmp/jobs/'
3
+
4
+ desc 'Init Jobs Folders'
5
+ task :init do
6
+ FileUtils.mkdir(jobs_root) unless File.exists? jobs_root
7
+ end
8
+
9
+ desc 'Start Jobs'
10
+ task :start => :init do
11
+ if File.exists? jobs_root + '/.lock'
12
+ STDERR.print "Jobs are running, please run after they finished.\n"
13
+ exit!
14
+ else
15
+ File.open(jobs_root + '/.lock', 'w'){ |f| f.write Process.pid.to_s }
16
+ print "Starting jobs at process##{Process.pid}\n"
17
+ list = Dir[jobs_root + '*']
18
+ if list.length > 0
19
+ Rake::Task[:environment].execute
20
+ list.each do |id|
21
+ jobs = Jobs.new
22
+ jobs.import File.basename(id)
23
+ jobs.run
24
+ end
25
+ end
26
+ File.delete(jobs_root + '/.lock')
27
+ print "Finished jobs\n"
28
+ end
29
+ end
30
+
31
+ desc 'Stop Jobs'
32
+ task :stop do
33
+ if File.exists? jobs_root + '/.lock'
34
+ system 'kill `cat ' + jobs_root + '/.lock`'
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,138 @@
1
+ require 'fileutils'
2
+ require 'rainbow'
3
+ require 'uuid'
4
+ require 'yaml'
5
+
6
+ module ZfbenRailsJobs
7
+ if defined? Rails
8
+ class Railtie < Rails::Railtie
9
+ railtie_name :zfben_rails_jobs
10
+ path = File.realpath(File.dirname(__FILE__))
11
+ rake_tasks do
12
+ require File.join(path, 'tasks.rb')
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ class Jobs
19
+ def import id
20
+ if File.exists?(@path + id)
21
+ yaml = YAML::load(File.read(@path + id))
22
+ @id = yaml[:id]
23
+ @list = yaml[:list]
24
+ else
25
+ false
26
+ end
27
+ end
28
+
29
+ def add cls, method, args = nil
30
+ unless locked?
31
+ @list.push [cls.to_s, method, args]
32
+ true
33
+ else
34
+ false
35
+ end
36
+ end
37
+
38
+ def save
39
+ unless locked?
40
+ @locked = true
41
+ File.open(@path + @id, 'w'){ |f| f.write data.to_yaml }
42
+ true
43
+ else
44
+ false
45
+ end
46
+ end
47
+
48
+ def id
49
+ @id
50
+ end
51
+
52
+ def list
53
+ @list
54
+ end
55
+
56
+ def data
57
+ { id: @id, list: @list, at: @at }
58
+ end
59
+
60
+ def result
61
+ successed = 0
62
+ failed = 0
63
+ pending = 0
64
+ @list.map{ |l|
65
+ if l.length < 4
66
+ pending = pending + 1
67
+ elsif l[3]
68
+ successed = successed + 1
69
+ else
70
+ failed = failed + 1
71
+ end
72
+ }
73
+ { successed: successed, failed: failed, pending: pending }
74
+ end
75
+
76
+ def finished?
77
+ r = result
78
+ r[:failed] == 0 && r[:pending] == 0
79
+ end
80
+
81
+ def locked?
82
+ @locked
83
+ end
84
+
85
+ def run
86
+ unless @at.nil?
87
+ if Time.now < @at
88
+ return false
89
+ end
90
+ end
91
+
92
+ @locked = true
93
+
94
+ @list.each_index do |i|
95
+ if !@list[i][3]
96
+ unless Object.const_defined?(@list[i][0])
97
+ @list[i].push false, 'Class missing'
98
+ next
99
+ end
100
+
101
+ obj = Object.const_get(@list[i][0])
102
+
103
+ unless obj.respond_to?(@list[i][1])
104
+ @list[i].push false, 'Method missing'
105
+ next
106
+ end
107
+
108
+ obj.send @list[i][1], *@list[i][2]
109
+
110
+ @list[i].push true, 'Finished'
111
+ end
112
+ end
113
+
114
+ update
115
+ true
116
+ end
117
+
118
+ private
119
+
120
+ def initialize at = nil
121
+ @id = UUID.new.generate
122
+ @list = []
123
+ @path = File.realpath(Rails.root) + '/tmp/jobs/'
124
+ FileUtils.mkdir(@path) unless File.exists?(@path)
125
+ @locked = false
126
+ @at = at
127
+ end
128
+
129
+ def update
130
+ if File.exists? @path + @id
131
+ if finished?
132
+ File.delete @path + @id
133
+ else
134
+ File.open(@path + @id, 'w'){ |f| f.write data.to_yaml }
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1 @@
1
+ tmp/*
@@ -0,0 +1,7 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+ require 'rake'
6
+
7
+ Dummy::Application.load_tasks
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,12 @@
1
+ class Example
2
+ class << self
3
+ def job n
4
+ p 'job ' + n.to_s
5
+ sleep n
6
+ end
7
+
8
+ def job_0
9
+ p 'job 0'
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag :all %>
6
+ <%= javascript_include_tag :defaults %>
7
+ <%= csrf_meta_tag %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,20 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require "active_model/railtie"
4
+ require "action_controller/railtie"
5
+ require "action_view/railtie"
6
+
7
+ Bundler.require
8
+
9
+ Dir[File.dirname(__FILE__) << '/../../../']
10
+
11
+ module Dummy
12
+ class Application < Rails::Application
13
+
14
+ # Configure the default encoding used in templates for Ruby 1.9.
15
+ config.encoding = "utf-8"
16
+
17
+ # Configure sensitive parameters which will be filtered from the log file.
18
+ config.filter_parameters += [:password]
19
+ end
20
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ gemfile = File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ if File.exist?(gemfile)
5
+ ENV['BUNDLE_GEMFILE'] = gemfile
6
+ require 'bundler'
7
+ Bundler.setup
8
+ end
9
+
10
+ $:.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,22 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3
3
+ development:
4
+ adapter: sqlite3
5
+ database: db/development.sqlite3
6
+ pool: 5
7
+ timeout: 5000
8
+
9
+ # Warning: The database defined as "test" will be erased and
10
+ # re-generated from your development database when you run "rake".
11
+ # Do not set this db to the same as development or production.
12
+ test:
13
+ adapter: sqlite3
14
+ database: db/test.sqlite3
15
+ pool: 5
16
+ timeout: 5000
17
+
18
+ production:
19
+ adapter: sqlite3
20
+ database: db/production.sqlite3
21
+ pool: 5
22
+ timeout: 5000
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ Dummy::Application.initialize!
@@ -0,0 +1,23 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # In the development environment your application's code is reloaded on
5
+ # every request. This slows down response time but is perfect for development
6
+ # since you don't have to restart the webserver when you make code changes.
7
+ config.cache_classes = false
8
+
9
+ # Log error messages when you accidentally call methods on nil.
10
+ config.whiny_nils = true
11
+
12
+ # Show full error reports and disable caching
13
+ config.consider_all_requests_local = true
14
+ config.action_view.debug_rjs = true
15
+ config.action_controller.perform_caching = false
16
+
17
+ # Print deprecation notices to the Rails logger
18
+ config.active_support.deprecation = :log
19
+
20
+ # Only use best-standards-support built into browsers
21
+ config.action_dispatch.best_standards_support = :builtin
22
+ end
23
+
@@ -0,0 +1,49 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # The production environment is meant for finished, "live" apps.
5
+ # Code is not reloaded between requests
6
+ config.cache_classes = true
7
+
8
+ # Full error reports are disabled and caching is turned on
9
+ config.consider_all_requests_local = false
10
+ config.action_controller.perform_caching = true
11
+
12
+ # Specifies the header that your server uses for sending files
13
+ config.action_dispatch.x_sendfile_header = "X-Sendfile"
14
+
15
+ # For nginx:
16
+ # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
17
+
18
+ # If you have no front-end server that supports something like X-Sendfile,
19
+ # just comment this out and Rails will serve the files
20
+
21
+ # See everything in the log (default is :info)
22
+ # config.log_level = :debug
23
+
24
+ # Use a different logger for distributed setups
25
+ # config.logger = SyslogLogger.new
26
+
27
+ # Use a different cache store in production
28
+ # config.cache_store = :mem_cache_store
29
+
30
+ # Disable Rails's static asset server
31
+ # In production, Apache or nginx will already do this
32
+ config.serve_static_assets = false
33
+
34
+ # Enable serving of images, stylesheets, and javascripts from an asset server
35
+ config.action_controller.asset_host = "http://assets.com"
36
+
37
+ # Disable delivery errors, bad email addresses will be ignored
38
+ # config.action_mailer.raise_delivery_errors = false
39
+
40
+ # Enable threaded mode
41
+ # config.threadsafe!
42
+
43
+ # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
44
+ # the I18n.default_locale when a translation can not be found)
45
+ config.i18n.fallbacks = true
46
+
47
+ # Send deprecation notices to registered listeners
48
+ config.active_support.deprecation = :notify
49
+ end
@@ -0,0 +1,25 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # The test environment is used exclusively to run your application's
5
+ # test suite. You never need to work with it otherwise. Remember that
6
+ # your test database is "scratch space" for the test suite and is wiped
7
+ # and recreated between test runs. Don't rely on the data there!
8
+ config.cache_classes = true
9
+
10
+ # Log error messages when you accidentally call methods on nil.
11
+ config.whiny_nils = true
12
+
13
+ # Show full error reports and disable caching
14
+ config.consider_all_requests_local = true
15
+ config.action_controller.perform_caching = false
16
+
17
+ # Raise exceptions instead of rendering exception templates
18
+ config.action_dispatch.show_exceptions = false
19
+
20
+ # Disable request forgery protection in test environment
21
+ config.action_controller.allow_forgery_protection = false
22
+
23
+ # Print deprecation notices to the stderr
24
+ config.active_support.deprecation = :stderr
25
+ end
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
4
+ # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
5
+
6
+ # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
7
+ # Rails.backtrace_cleaner.remove_silencers!
@@ -0,0 +1,10 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new inflection rules using the following format
4
+ # (all these examples are active by default):
5
+ # ActiveSupport::Inflector.inflections do |inflect|
6
+ # inflect.plural /^(ox)$/i, '\1en'
7
+ # inflect.singular /^(ox)en/i, '\1'
8
+ # inflect.irregular 'person', 'people'
9
+ # inflect.uncountable %w( fish sheep )
10
+ # end
@@ -0,0 +1,5 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new mime types for use in respond_to blocks:
4
+ # Mime::Type.register "text/richtext", :rtf
5
+ # Mime::Type.register_alias "text/html", :iphone
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key for verifying the integrity of signed cookies.
4
+ # If you change this key, all old signed cookies will become invalid!
5
+ # Make sure the secret is at least 30 characters and all random,
6
+ # no regular words or you'll be exposed to dictionary attacks.
7
+ Dummy::Application.config.secret_token = 'ca3f29ce2033cebff511999f2fce3f4777dda12be7b8ad71e3f82bead7b95c26cd995ece00052674ee35dfd7892b7c78d0faaaffc9274fcf1b9bb49805ff0a2e'
@@ -0,0 +1,8 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ Dummy::Application.config.session_store :cookie_store, :key => '_dummy_session'
4
+
5
+ # Use the database for sessions instead of the cookie-based default,
6
+ # which shouldn't be used to store highly confidential information
7
+ # (create the session table with "rails generate session_migration")
8
+ # Dummy::Application.config.session_store :active_record_store
@@ -0,0 +1,5 @@
1
+ # Sample localization file for English. Add more files in this directory for other locales.
2
+ # See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3
+
4
+ en:
5
+ hello: "Hello world"
@@ -0,0 +1,58 @@
1
+ Dummy::Application.routes.draw do
2
+ # The priority is based upon order of creation:
3
+ # first created -> highest priority.
4
+
5
+ # Sample of regular route:
6
+ # match 'products/:id' => 'catalog#view'
7
+ # Keep in mind you can assign values other than :controller and :action
8
+
9
+ # Sample of named route:
10
+ # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
11
+ # This route can be invoked with purchase_url(:id => product.id)
12
+
13
+ # Sample resource route (maps HTTP verbs to controller actions automatically):
14
+ # resources :products
15
+
16
+ # Sample resource route with options:
17
+ # resources :products do
18
+ # member do
19
+ # get 'short'
20
+ # post 'toggle'
21
+ # end
22
+ #
23
+ # collection do
24
+ # get 'sold'
25
+ # end
26
+ # end
27
+
28
+ # Sample resource route with sub-resources:
29
+ # resources :products do
30
+ # resources :comments, :sales
31
+ # resource :seller
32
+ # end
33
+
34
+ # Sample resource route with more complex sub-resources
35
+ # resources :products do
36
+ # resources :comments
37
+ # resources :sales do
38
+ # get 'recent', :on => :collection
39
+ # end
40
+ # end
41
+
42
+ # Sample resource route within a namespace:
43
+ # namespace :admin do
44
+ # # Directs /admin/products/* to Admin::ProductsController
45
+ # # (app/controllers/admin/products_controller.rb)
46
+ # resources :products
47
+ # end
48
+
49
+ # You can have the root of your site routed with "root"
50
+ # just remember to delete public/index.html.
51
+ # root :to => "welcome#index"
52
+
53
+ # See how all your routes lay out with "rake routes"
54
+
55
+ # This is a legacy wild controller route that's not recommended for RESTful applications.
56
+ # Note: This route will make all actions in every controller accessible via GET requests.
57
+ # match ':controller(/:action(/:id(.:format)))'
58
+ end
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Dummy::Application
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/404.html -->
21
+ <div class="dialog">
22
+ <h1>The page you were looking for doesn't exist.</h1>
23
+ <p>You may have mistyped the address or the page may have moved.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/422.html -->
21
+ <div class="dialog">
22
+ <h1>The change you wanted was rejected.</h1>
23
+ <p>Maybe you tried to change something you didn't have access to.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/500.html -->
21
+ <div class="dialog">
22
+ <h1>We're sorry, but something went wrong.</h1>
23
+ <p>We've been notified about this issue and we'll take a look at it shortly.</p>
24
+ </div>
25
+ </body>
26
+ </html>
File without changes
@@ -0,0 +1,2 @@
1
+ // Place your application-specific JavaScript functions and classes here
2
+ // This file is automatically included by javascript_include_tag :defaults