oxen_job 0.0.3 → 0.0.4

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: 7eb15de096242e2a2322ac82994109c011297cd2
4
- data.tar.gz: c19788c69c1e2aa87670f2ead57ff180fc08e37b
3
+ metadata.gz: 57f22bd37f191f701b19c5bbf295b46b46abd67d
4
+ data.tar.gz: 28753d1c88cda11bac57327d9dd16f41fb08fa3e
5
5
  SHA512:
6
- metadata.gz: 9935f5a28a609d2e08c90430f3d20b60895728e6e012a69344927dabd24c265546cf5b56179f8677b73661fc1bb742a40ea1b0c1edcab4650debf82c98b34c2a
7
- data.tar.gz: 77d19da663369625496401cfca7eaf5837d3802fbe792039a2143e1144371f6e415718968cb30521bb280e529ac10305d2cdcbf1855a1257a3e3239dece02e03
6
+ metadata.gz: 5e432e3bd1378466bc883431ffb080017d63d611fbbf573540664fce3e7a4271e19a61feb9fc0d735f7b26f9c160671f82de78265211a9a7adbd23bfb5f41dfa
7
+ data.tar.gz: 4401bf23360e20c0271cd4ff1c50127613af6c5ce9f53fc7a61db0d196b8e24b86c879ba9d7382f1373222ec7ebcac29937970cefda235150d9064eee5595ec6
data/.gitignore CHANGED
@@ -7,6 +7,7 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ *gem
10
11
  *.bundle
11
12
  *.so
12
13
  *.o
@@ -0,0 +1,18 @@
1
+ class Oxen::BackgroundJobsController < AbstractResourcesController
2
+
3
+ private
4
+ # Use callbacks to share common setup or constraints between actions.
5
+ # def set_account
6
+ # @account = Account.find(params[:id])
7
+ # end
8
+
9
+ # Never trust parameters from the scary internet, only allow the white list through.
10
+ # def account_params
11
+ # params.require(:account).permit(:name)
12
+ # end
13
+
14
+ def resource_params
15
+ params.require(:account).permit(:name, :active, :default_url, :chargeable, :last_charged_at, :free_users )
16
+ end
17
+
18
+ end
@@ -0,0 +1,7 @@
1
+ module Oxen
2
+ module BackgroundJobsHelper
3
+ def build_link_from_array arr
4
+ link_to arr[0], arr[1]
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,17 @@
1
+ class OxBackgroundJob < AbstractResource
2
+ self.table_name = 'delayed_jobs'
3
+ establish_connection ((Rails.env=="development") ? :dev_oxen_tables : :oxen_tables )
4
+
5
+ belongs_to :account
6
+
7
+ # to make show_resource_delete_icon happy
8
+ def name
9
+ 'dj'
10
+ end
11
+
12
+ def show_handler
13
+ parms = YAML::load( handler).job_data["arguments"][0]['_aj_globalid']
14
+ [parms.gsub(/^.*mgmt/,''), parms.split("/")[-2].constantize.find(parms.split("/")[-1])]
15
+ end
16
+
17
+ end
@@ -0,0 +1,51 @@
1
+ module Oxen
2
+ class BackgroundJobPolicy < AbstractResourcePolicy
3
+
4
+ class Scope < Scope
5
+ def resolve
6
+ if current_user.nil?
7
+ super
8
+ elsif current_user.admin?
9
+ scope.all
10
+ else
11
+ scope.where(id: current_user.account.id)
12
+ end
13
+ end
14
+ end
15
+
16
+
17
+
18
+ def new?
19
+ current_user.admin?
20
+ end
21
+
22
+ def index?
23
+ @current_user.admin? || @current_user.account_admin?
24
+ end
25
+
26
+ def create?
27
+ @current_user.admin?
28
+ end
29
+
30
+ def show?
31
+ @current_user.admin? or @current_user.account==@model
32
+ end
33
+
34
+ def edit?
35
+ @current_user.admin? or @current_user.account==@model
36
+ end
37
+
38
+ def update?
39
+ @current_user.admin? or @current_user.account==@model
40
+ end
41
+
42
+ def destroy?
43
+ @current_user.admin?
44
+ end
45
+
46
+ def print?
47
+ @current_user.admin? || @current_user.account_admin?
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,8 @@
1
+ %tr{ id: "tr-#{resource.id}"}
2
+ - if current_user.admin?
3
+ %td= link_to resource.account.name, resource.account
4
+ %td= resource.queue
5
+ %td= resource.attempts
6
+ %td= build_link_from_array resource.show_handler
7
+ %td= resource.run_at
8
+ %td= show_resource_delete_icon resource, '/admin/background_job'
@@ -0,0 +1,22 @@
1
+ :css
2
+ a.dropdown-button {
3
+ padding-left: 1em;
4
+ padding-right: 1em;
5
+ }
6
+
7
+ - unless params[:action] == 'print'
8
+ = paginate resources
9
+
10
+ %thead
11
+ %tr
12
+ - if current_user.admin?
13
+ %th= t('.system')
14
+ %th= t('.queue')
15
+ %th= t('.attempts')
16
+ %th= t('.job_handler')
17
+ %th= t('.next_run')
18
+ %th= t(:delete)
19
+
20
+ %tbody.bordered.responsive-table.striped.hoverable.page.resources_list
21
+ - resources.each do |background_job|
22
+ = render partial: 'background_job', locals: { resource: background_job }
data/bin/delayed_job ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
4
+ require 'delayed/command'
5
+ Delayed::Command.new(ARGV).daemonize
data/config/routes.rb ADDED
@@ -0,0 +1,6 @@
1
+ Rails.application.routes.draw do
2
+
3
+ scope '/admin' do
4
+ resources :background_jobs
5
+ end
6
+ end
@@ -0,0 +1,29 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module OxenJob
5
+ module Generators
6
+ class JobGenerator < Rails::Generators::Base
7
+ include Rails::Generators::Migration
8
+ def self.source_root
9
+ @source_root ||= File.join(File.dirname(__FILE__), 'templates')
10
+ end
11
+
12
+ def self.next_migration_number(dirname)
13
+ if ActiveRecord::Base.timestamped_migrations
14
+ Time.new.utc.strftime("%Y%m%d%H%M%S")
15
+ else
16
+ "%.3d" % (current_migration_number(dirname) + 1)
17
+ end
18
+ end
19
+
20
+
21
+ def create_migration_file
22
+ migration_template 'create_jobs.rb', 'db/migrate/create_jobs.rb'
23
+ # allow the clock to advance 1sec
24
+ # sleep 1
25
+ # migration_template 'user_permission.rb', 'db/migrate/create_user_permissions.rb'
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,19 @@
1
+ class CreateJobs < ActiveRecord::Migration
2
+ def change
3
+ create_table :delayed_jobs, force: true do |table|
4
+ table.references :account, null: false, index: true
5
+ table.integer :priority, default: 0, null: false # Allows some jobs to jump to the front of the queue
6
+ table.integer :attempts, default: 0, null: false # Provides for retries, but still fail eventually.
7
+ table.text :handler, null: false # YAML-encoded string of the object that will do work
8
+ table.text :last_error # reason for last failure (See Note below)
9
+ table.datetime :run_at # When to run. Could be Time.zone.now for immediately, or sometime in the future.
10
+ table.datetime :locked_at # Set when a client is working on this object
11
+ table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead)
12
+ table.string :locked_by # Who is working on this object (if locked)
13
+ table.string :queue # The name of the queue this job is in
14
+ table.timestamps null: true
15
+ end
16
+
17
+ add_index :delayed_jobs, [:priority, :run_at], name: "delayed_jobs_priority"
18
+ end
19
+ end
data/lib/ox_job.rb ADDED
@@ -0,0 +1,107 @@
1
+ require 'delayed_job'
2
+ require 'delayed_job_active_record'
3
+ #
4
+ # defining a hook on the enqueue
5
+ module ActiveJob
6
+ module QueueAdapters
7
+ class DelayedJobAdapter
8
+ class JobWrapper
9
+
10
+ def max_attempts
11
+ 3
12
+ end
13
+
14
+ def enqueue args
15
+ args.account_id = YAML::load( args.handler).job_data["arguments"][1]["account_id"]
16
+ end
17
+
18
+ # def before(job)
19
+ # Rails.logger.info "-------------> before"
20
+ # end
21
+ #
22
+ # def after(job)
23
+ # Rails.logger.info "-------------> after"
24
+ # end
25
+
26
+ def success(job)
27
+ jd = YAML::load( job.handler).job_data
28
+ Rails.logger.info "OXEN (%s) executed successfully by DelayedJobAdapter on ActiveJob! Job was %s" % [jd['job_class'], jd['arguments'][0]["_aj_globalid"]]
29
+ end
30
+
31
+
32
+ # If delayed_job runs into errors or exceptions it will hook/callback to the error method.
33
+ # cycle_error will be called and the state of the PrintJob will be changed.
34
+ def error(job, exception)
35
+ begin
36
+ jd = YAML::load( job.handler).job_data
37
+ Rails.logger.error "OXEN ERROR (%s) executed unsuccessfully by DelayedJobAdapter on ActiveJob! Message is: #{exception.message}" % jd['job_class']
38
+ parms=jd['arguments'][0]['_aj_globalid'].split("/")
39
+ parms[-2].constantize.find(parms[-1]).update_column( :state, "job encounters an exception - #{exception.message}")
40
+ rescue
41
+ # logger.error("[OXEN] #{Time.now} [error] %s %s" % [I18n.oxt('print_job_error'),exception.message])
42
+ Rails.logger.error "OXEN ERROR (unknown) executed unsuccessfully by DelayedJobAdapter on ActiveJob! Message is: #{exception.message}"
43
+ end
44
+ end
45
+
46
+
47
+ # If delayed_job has used up all its attempts and is unable to render and print the job, the PrintJob will fail and delayed_job will hook/callback to the method failure
48
+ # Look also in delayed_job_config.rb to see the configuration for delayed_job.
49
+ # Failed jobs will not be deleted, which is normally the default configuration.
50
+ def failure(job)
51
+ begin
52
+ jd = YAML::load( job.handler).job_data
53
+ Rails.logger.error "OXEN ERROR FAILURE (%s) executed unsuccessfully by DelayedJobAdapter on ActiveJob and gave up! Message is: #{exception.message}" % jd['job_class']
54
+ parms=jd['arguments'][0]['_aj_globalid'].split("/")
55
+ parms[-2].constantize.find(parms[-1]).update_column( :state, "job encounters an exception - #{exception.message}")
56
+ rescue
57
+ # logger.error("[OXEN] #{Time.now} [error] %s %s" % [I18n.oxt('print_job_error'),exception.message])
58
+ Rails.logger.error "OXEN ERROR FAILURE (unknown) executed unsuccessfully by DelayedJobAdapter on ActiveJob and gave up! Message is: #{exception.message}"
59
+ end
60
+
61
+ #Airbrake.notify(exception)
62
+ # cycle_error
63
+ #logger.debug "Printeren svarer ikke!"
64
+ end
65
+
66
+
67
+
68
+ end
69
+ end
70
+ end
71
+ end
72
+ #
73
+ #
74
+ class OxJob < ActiveJob::Base
75
+ queue_as :default
76
+
77
+ # at least implement this method on your job classes inherited from OxJob
78
+ #
79
+ def perform
80
+ raise "You need to implement perform on your job class!"
81
+ end
82
+
83
+ # hooks
84
+ #
85
+ before_enqueue do |job|
86
+ job.arguments.first.update_column :state, I18n.t( "#{job.arguments.first.class.to_s.underscore.pluralize}.enqueueing")
87
+ job.arguments << {account_id: job.arguments.first.account_id}
88
+ end
89
+ #
90
+ # around_enqueue
91
+ after_enqueue do |job|
92
+ job.arguments.first.update_column :state, I18n.t( "#{job.arguments.first.class.to_s.underscore.pluralize}.enqueued")
93
+ end
94
+ #
95
+ before_perform do |job|
96
+ job.arguments.first.update_column :state, I18n.t( "#{job.arguments.first.class.to_s.underscore.pluralize}.prepare_to_perform")
97
+ end
98
+ #
99
+ # around_perform do |job|
100
+ #
101
+ # end
102
+ after_perform do |job|
103
+ job.arguments.first.update_column :state, I18n.t( "#{job.arguments.first.class.to_s.underscore.pluralize}.done_performing")
104
+ end
105
+ #
106
+
107
+ end
@@ -0,0 +1,7 @@
1
+ module OxenJob
2
+ class Engine < ::Rails::Engine
3
+ config.active_job do |g|
4
+ g.queue_adapter = :delayed_job
5
+ end
6
+ end
7
+ end
@@ -1,3 +1,3 @@
1
1
  module OxenJob
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oxen_job
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Walther H Diechmann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-27 00:00:00.000000000 Z
11
+ date: 2015-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: delayed_job
@@ -69,7 +69,8 @@ dependencies:
69
69
  description: 'Provides interfaces to DelayedJob via the ActiveJob '
70
70
  email:
71
71
  - walther@diechmann.net
72
- executables: []
72
+ executables:
73
+ - delayed_job
73
74
  extensions: []
74
75
  extra_rdoc_files: []
75
76
  files:
@@ -78,7 +79,19 @@ files:
78
79
  - LICENSE.txt
79
80
  - README.md
80
81
  - Rakefile
82
+ - app/controllers/oxen/background_jobs_controller.rb
83
+ - app/helpers/oxen/background_jobs_helpers.rb
84
+ - app/models/ox_background_job.rb
85
+ - app/policies/oxen/background_job_policy.rb
86
+ - app/views/oxen/background_jobs/_background_job.html.haml
87
+ - app/views/oxen/background_jobs/_background_jobs.html.haml
88
+ - bin/delayed_job
89
+ - config/routes.rb
90
+ - lib/generators/oxen_job/job_generator.rb
91
+ - lib/generators/oxen_job/templates/create_jobs.rb
92
+ - lib/ox_job.rb
81
93
  - lib/oxen_job.rb
94
+ - lib/oxen_job/engine.rb
82
95
  - lib/oxen_job/version.rb
83
96
  - oxen_job.gemspec
84
97
  homepage: ''