concussion 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e9d9bd8e22789958ac0069c9598e7a1f737ca63a
4
+ data.tar.gz: bbe82e8fcf8dfc58bd9f071cb14e622b52d9a582
5
+ SHA512:
6
+ metadata.gz: 84a4d186967d0abe66d815fd516a3383204cb1600721e7a0d6a2fc422acd2064f91cf27985eee4746d13f6f0579ad0c8ab2513a8bbacfbe17182ec52d6493442
7
+ data.tar.gz: 85008ec840632da4c4beb66409a302673d49575ef7cdca67260ce8f372419f18bcb0a42ce910e4e97b92e31c25aac51a6783ba1537771889dce1393426a9a94f
@@ -0,0 +1,33 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /vendor/bundle
26
+ /lib/bundler/man/
27
+
28
+ Gemfile.lock
29
+ .ruby-version
30
+ .ruby-gemset
31
+
32
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
33
+ .rvmrc
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.3
4
+ before_install: gem install bundler -v 1.10.5
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in concussion.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 chemica
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Ben Dunkley
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,94 @@
1
+ # Concussion
2
+
3
+ Sucker Punch is an awesome gem which allows in-process background jobs. If you want to run jobs at a particular time,
4
+ however, there is a downside. Jobs are only held in memory, so restarting the process will kill any pending jobs.
5
+
6
+ Most web apps will be using some kind of data store however, so Concussion allows them to be persisted.
7
+
8
+ ## WARNING
9
+
10
+ Concussion is currently untested code. It has not been used in production and has no automated test suite. Use at your
11
+ own risk. I cannot be held responsible for any loss of data, sanity or clients that may result from use of this code.
12
+
13
+ The interface may also change radically in the following versions.
14
+
15
+ Currently only Redis can be used as a data store.
16
+
17
+ The gem has been published mainly as a placeholder.
18
+
19
+ You have been warned.
20
+
21
+ ## Installation
22
+
23
+ Add this line to your application's Gemfile:
24
+
25
+ ```ruby
26
+ gem 'concussion'
27
+ ```
28
+
29
+ And then execute:
30
+
31
+ $ bundle
32
+
33
+ Or install it yourself as:
34
+
35
+ $ gem install concussion
36
+
37
+ ## Usage
38
+
39
+ Concussion won't do much on its own. It needs to connect to a persistent storage, and for that it needs an adapter. For
40
+ now it comes bundled with a Redis adapter, but that will be extracted into its own gem in due course.
41
+
42
+ The adapter is a class with a simple interface, described in the redis class adapter class comments.
43
+
44
+ To use Concussion in a Rails project, add an initializer containing something along the lines of the following code:
45
+
46
+ ```ruby
47
+ # after_initialize is used to allow Redis to initialise first
48
+ Rails.application.config.after_initialize do
49
+ namespace = "concussion-persistence"
50
+ redis = Redis.new(:host => "localhost", :port => 6379)
51
+ Concussion.store = Concussion::RedisAdapter.new(redis: redis, namespace: namespace)
52
+ Concussion.init
53
+ end
54
+ ```
55
+
56
+ Jobs any jobs which are set to run while the server is offline will run immediately on the initializer being run. For
57
+ this reason the initializer must be wrapped in an 'after_initialize' block to ensure all other components of the
58
+ application are ready for use.
59
+
60
+ When defining a job, use the following form:
61
+
62
+ ```ruby
63
+ class DoSomethingJob
64
+ include SuckerPunch::Job
65
+ prepend Concussion::Persist
66
+
67
+ def perform(opts = {})
68
+ MyThingDoer.new(opts).do_thing
69
+ end
70
+
71
+ end
72
+ ```
73
+
74
+ And call it with:
75
+
76
+ ```ruby
77
+ run_at = Time.now + 5.hours
78
+ DoSomethingJob.new.async.later run_at, opts
79
+ ```
80
+
81
+ All done.
82
+
83
+
84
+ ## Contributing
85
+
86
+ Bug reports and pull requests are welcome on GitHub at https://github.com/chemica/concussion. This project is
87
+ intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the
88
+ [Contributor Covenant](contributor-covenant.org) code of conduct. Or something. These words were automatically
89
+ added to this gem, but they are good words so they can stay.
90
+
91
+
92
+ ## License
93
+
94
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "concussion"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,41 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'concussion/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "concussion"
8
+ spec.version = Concussion::VERSION
9
+ spec.authors = ["Benjamin Randles-Dunkley"]
10
+ spec.email = ["ben@chemica.co.uk"]
11
+
12
+ spec.summary = %q{An addition to the Suckerpunch gem to allow delayed jobs that survive a server reset.}
13
+ spec.description = %q{Sucker Punch is an awesome gem which allows background tasks to be run from the current
14
+ process. They can be set to run in the future, but they will disappear and not get run if the
15
+ server or process running the jobs is stopped or restarted. Concussion uses the Ruby 2.x
16
+ 'prepend' feature to provide a thin wrapper around Suckerpunch job objects, persisting them to
17
+ an external storage system of your choice. When the server is restarted, any jobs unprocessed
18
+ jobs will be run immediately while future jobs will be reinstated to be run at the
19
+ appropriate time.}
20
+ spec.homepage = "https://github.com/chemica/concussion"
21
+ spec.license = "MIT"
22
+
23
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
24
+ # delete this section to allow pushing this gem to any host.
25
+ if spec.respond_to?(:metadata)
26
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
27
+ else
28
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
29
+ end
30
+
31
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
+ spec.bindir = "exe"
33
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
34
+ spec.require_paths = ["lib"]
35
+
36
+ spec.add_dependency "sucker_punch", "~> 1.0"
37
+
38
+ spec.add_development_dependency "bundler", "~> 1.10"
39
+ spec.add_development_dependency "rake", "~> 10.0"
40
+ spec.add_development_dependency "minitest"
41
+ end
@@ -0,0 +1,24 @@
1
+ require "concussion/version"
2
+
3
+ module Concussion
4
+
5
+ class << self
6
+ attr_accessor :store
7
+ end
8
+
9
+ def self.persist(klass, guid, time, *args)
10
+ store.set guid, {klass: klass.name, time: time, args: args}
11
+ end
12
+
13
+ def self.retire(guid)
14
+ store.del guid
15
+ end
16
+
17
+ def self.init
18
+ store.find_each do |guid, data|
19
+ retire guid
20
+ Object.const_get(data[:klass]).new.later data[:time], *(data[:args])
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,20 @@
1
+ module Concussion
2
+ module Persist
3
+ def perform(*args)
4
+ begin
5
+ super *args
6
+ ensure
7
+ Concussion.retire @guid
8
+ end
9
+ end
10
+
11
+ def later(time, *args)
12
+ time = Time.now if time < Time.now
13
+ seconds = (time - Time.now).to_i
14
+ @guid = SecureRandom.uuid
15
+ Concussion.persist(self.class, @guid, time, *args)
16
+
17
+ after(seconds) { perform(*args) }
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,79 @@
1
+ # An adapter class must create an instance object which responds to:
2
+ #
3
+ # get([key]) # returns data as an object (any ruby construct that can be Marshaled)
4
+
5
+ # set([key], [data]) # sets data (a Marshalable object) against a text key.
6
+ # Returns data if successful, or raises an exception.
7
+
8
+ # del([key]) # Deletes data against [key] (String)
9
+
10
+ # find_each(opts) # Returns an enumerator or can be passed a block.
11
+ # Enumerator iterates through all stored keys and yields the key
12
+ # and the associated data (as a Ruby object)
13
+ # find_each may take options as a hash (opts).
14
+ # Currently it should only expect one option: :batch_size
15
+ # which takes an integer.
16
+ #
17
+ # An adapter class instance must also provide a 'namespace' accessor, which can optionally be set to allow
18
+ # multiple stores on the same storage device. In the case of Redis, this is added to the hash key.
19
+
20
+ # An adapter class must allow 'namespace' to be set using the options on the 'initialize' method.
21
+ #
22
+
23
+ # This adapter uses a Redis hash, as providing a list of normal keys matching a pattern is problematic.
24
+ # We manage key/value expiration outside Redis, to avoid jobs disappearing while the server is inactive.
25
+
26
+ module Concussion
27
+ class RedisAdapter
28
+
29
+ attr_accessor :redis, :namespace
30
+
31
+ KEY = "--concussion-redis-adapter-store"
32
+ DEFAULT_BATCH_SIZE = 1000
33
+
34
+ def initialize(opts = {})
35
+ self.redis = opts.fetch(:redis) { Redis.new }
36
+ self.namespace = opts.fetch(:namespace) { "" }
37
+ end
38
+
39
+ def namespaced_key
40
+ "#{namespace}#{KEY}"
41
+ end
42
+
43
+ def get(key)
44
+ decode_from_redis redis.hget(namespaced_key, key)
45
+ end
46
+
47
+ def set(key, data)
48
+ redis.hset(namespaced_key, key, encode_for_redis(data))
49
+ data
50
+ end
51
+
52
+ def del(key)
53
+ redis.hdel(namespaced_key, key)
54
+ end
55
+
56
+ def find_each(opts = {})
57
+ return enum_for(:find_each, opts) unless block_given?
58
+ batch_size = opts.fetch(:batch_size) { DEFAULT_BATCH_SIZE }
59
+ keys = redis.hkeys namespaced_key
60
+
61
+ keys.each_slice(batch_size).each do |key_slice|
62
+ Hash[key_slice.zip(redis.hmget(namespaced_key, key_slice))].each do |key, data|
63
+ yield key, decode_from_redis(data)
64
+ end
65
+ end
66
+ end
67
+
68
+ private
69
+
70
+ def decode_from_redis(str)
71
+ val = str.to_s.force_encoding('iso-8859-1')
72
+ val.present? ? Marshal.restore(val) : nil
73
+ end
74
+
75
+ def encode_for_redis(data)
76
+ Marshal.dump(data)
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,3 @@
1
+ module Concussion
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: concussion
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Benjamin Randles-Dunkley
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sucker_punch
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '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: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
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
+ description: |-
70
+ Sucker Punch is an awesome gem which allows background tasks to be run from the current
71
+ process. They can be set to run in the future, but they will disappear and not get run if the
72
+ server or process running the jobs is stopped or restarted. Concussion uses the Ruby 2.x
73
+ 'prepend' feature to provide a thin wrapper around Suckerpunch job objects, persisting them to
74
+ an external storage system of your choice. When the server is restarted, any jobs unprocessed
75
+ jobs will be run immediately while future jobs will be reinstated to be run at the
76
+ appropriate time.
77
+ email:
78
+ - ben@chemica.co.uk
79
+ executables: []
80
+ extensions: []
81
+ extra_rdoc_files: []
82
+ files:
83
+ - ".gitignore"
84
+ - ".travis.yml"
85
+ - CODE_OF_CONDUCT.md
86
+ - Gemfile
87
+ - LICENSE
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - bin/console
92
+ - bin/setup
93
+ - concussion.gemspec
94
+ - lib/concussion.rb
95
+ - lib/concussion/persist.rb
96
+ - lib/concussion/redis_adapter.rb
97
+ - lib/concussion/version.rb
98
+ homepage: https://github.com/chemica/concussion
99
+ licenses:
100
+ - MIT
101
+ metadata:
102
+ allowed_push_host: https://rubygems.org
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.4.8
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: An addition to the Suckerpunch gem to allow delayed jobs that survive a server
123
+ reset.
124
+ test_files: []