sqs_image_processor 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c4e61adf1c5d48f2606df7964d4ed1a0ba765089
4
+ data.tar.gz: 852021f5d287a612a5b8e24fc28c27c37ed511e7
5
+ SHA512:
6
+ metadata.gz: 36d80e0c3b592d91bd82a75594f0c1ee12ae92a0fdc84efb0c14db71017b7d9c604a3207bb8b7e09ee03862d29a11154dd8eec60651436dd196c1a7b01403506
7
+ data.tar.gz: ad3f55a9626ca737834dc0d773623bfc446a92ff40611af0dd6e3fbb3294c4c5e977151950b5859328efbf66e8fe111656a5d40c6b5b3c4bb549eb66342e46c9
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rvmrc
19
+ /sqs_image_processor_config.yml
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sqs_image_processor.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Neil Turner
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # Sqs Image Processor
2
+
3
+ Queued image processing which utilizes AWS::S3 for storage and AWS::SQS for queueing.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'sqs_image_processor'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install sqs_image_processor
18
+
19
+ ## Usage
20
+
21
+ Configuration is via a simple YAML file. Run the following command to generate an example in your current working directory.
22
+
23
+ $ sqs_image_processor generate_config
24
+
25
+ Edit the settings as required, and when you're ready to start the processing use the following command.
26
+
27
+ $ sqs_image_processor start
28
+
29
+ Add the -d flag to run the process in the background.
30
+
31
+ SQS Image Processor will search your current working directory for a configuration file. If you'd like to specify a different location use the -c option.
32
+
33
+ $ sqs_image_processor start -c path/to/config.yml
34
+
35
+ To stop the processing use the 'stop' command.
36
+
37
+ $ sqs_image_processor stop
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it ( http://github.com/<my-github-username>/sqs_image_processor/fork )
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'sqs_image_processor'
4
+
5
+ # Entry-point to the application.
6
+ SqsImageProcessor::Manager.start(ARGV)
@@ -0,0 +1,10 @@
1
+ require 'yaml'
2
+ require 'recursive-open-struct'
3
+
4
+ module SqsImageProcessor
5
+ class Config
6
+ def self.load( config_path )
7
+ RecursiveOpenStruct.new( YAML.load_file(config_path) )
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,19 @@
1
+ require 'fileutils'
2
+
3
+ module SqsImageProcessor
4
+ module Generators
5
+ def self.config_generator
6
+ current_directory = File.dirname(__FILE__)
7
+ working_directory = File.expand_path(Dir.pwd)
8
+ target_path = File.join(working_directory, 'sqs_image_processor_config.yml')
9
+
10
+ if File.exists?(target_path)
11
+ puts "Config file already exists at this location."
12
+ else
13
+ template = File.join(current_directory, 'templates', 'sqs_image_processor_config.yml')
14
+ FileUtils.cp(template, target_path)
15
+ puts "Successfully generated config file in current working directory."
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,21 @@
1
+ aws:
2
+ sqs:
3
+ access_key_id: YOUR-AWS-ACCESS-KEY
4
+ secret_access_key: YOUR-AWS-SECRET-ACCESS-KEY
5
+ region: eu-west-1
6
+ queue_name: image-queue
7
+ s3:
8
+ access_key_id: YOUR-AWS-ACCESS-KEY
9
+ secret_access_key: YOUR-AWS-SECRET-ACCESS-KEY
10
+ region: eu-west-1
11
+ bucket: your-image-bucket
12
+ versions:
13
+ default:
14
+ width: 1000
15
+ height: 1000
16
+ thumbnail:
17
+ width: 300
18
+ height: 300
19
+ web:
20
+ width: 600
21
+ height: 600
@@ -0,0 +1,62 @@
1
+ require "thor"
2
+
3
+ # :stopdoc:
4
+ # SQS Image Processor offers a minimalist interface to make using it
5
+ # as easy as we can. The command line interface is implemented using
6
+ # Thor but this could easily be standard ruby if we want to strip out
7
+ # dependencies and streamline the gem.
8
+ # :startdoc:
9
+ module SqsImageProcessor
10
+
11
+ # Main interface to the application. Provides 4 commands to the user.
12
+ #
13
+ # - start
14
+ # - stop
15
+ # - status
16
+ # - generate_config
17
+ class Manager < Thor
18
+
19
+ desc "start", "Start the SQS Image Processor."
20
+ method_option :c, :type => :string, :default => File.join(Dir.pwd, 'sqs_image_processor_config.yml'), :required => false
21
+ method_option :d, :type => :boolean, :default => false, :required => false
22
+ def start
23
+ config_path = options[:c]
24
+ daemonize = options[:d]
25
+
26
+ if !File.exists?(config_path)
27
+ puts "Error: Config file not found."
28
+ elsif SqsImageProcessor::ProcessManager.is_running?
29
+ puts "Error: An instance of SqsImageProcessor is already running."
30
+ else
31
+ Process.daemon if daemonize
32
+ SqsImageProcessor::ProcessManager.generate_pid_file
33
+ config = SqsImageProcessor::Config.load( config_path )
34
+ puts "Loaded config file at #{config_path}."
35
+ puts "Starting SQS Image Processor."
36
+ SqsImageProcessor::Worker.start( config )
37
+ end
38
+ end
39
+
40
+ desc "stop", "Stop the SQS Image Processor."
41
+
42
+ def stop
43
+ SqsImageProcessor::ProcessManager.kill
44
+ end
45
+
46
+ desc "status", "Check whether SQS Image Processor is running."
47
+
48
+ def status
49
+ if SqsImageProcessor::ProcessManager.is_running?
50
+ puts "Running with PID #{SqsImageProcessor::ProcessManager.get_pid}."
51
+ else
52
+ puts "Not running."
53
+ end
54
+ end
55
+
56
+ desc "generate_config", "Generate an example SQS Image Processor configuration file in the current wokring directory."
57
+
58
+ def generate_config
59
+ SqsImageProcessor::Generators.config_generator
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,41 @@
1
+ module SqsImageProcessor
2
+ module ProcessManager
3
+ def self.pid_is_running?( pid )
4
+ begin
5
+ Process.getpgid( pid.to_i )
6
+ true
7
+ rescue Errno::ESRCH
8
+ false
9
+ end
10
+ end
11
+
12
+ def self.kill
13
+ if self.is_running?
14
+ pid = File.open("/tmp/sqs_image_processor_manager.pid", 'a+') {|f| f.read }.to_i
15
+ Process.kill("KILL", pid)
16
+ puts "Stopped instance of SqsImageProcessor."
17
+ else
18
+ puts "Error: SqsImageProcessor isn't running."
19
+ end
20
+ end
21
+
22
+ def self.is_running?
23
+ pid = File.open("/tmp/sqs_image_processor_manager.pid", 'a+') {|f| f.read }.to_i
24
+ if pid == 0 || !self.pid_is_running?(pid)
25
+ false
26
+ else
27
+ true
28
+ end
29
+ end
30
+
31
+ def self.generate_pid_file
32
+ File.open("/tmp/sqs_image_processor_manager.pid", 'w') {|f|
33
+ f.write(Process.pid)
34
+ }
35
+ end
36
+
37
+ def self.get_pid
38
+ pid = File.open("/tmp/sqs_image_processor_manager.pid", 'a+') {|f| f.read }.to_i
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module SqsImageProcessor
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,69 @@
1
+ require 'aws-sdk-core'
2
+ require 'uri'
3
+
4
+ module SqsImageProcessor
5
+ module Worker
6
+ def self.start( config )
7
+ continue_processing = true
8
+
9
+ sqs_client = Aws::SQS::Client.new(
10
+ credentials: Aws::Credentials.new(config.aws.sqs.access_key_id, config.aws.sqs.secret_access_key),
11
+ region: config.aws.sqs.region
12
+ )
13
+ s3_client = Aws::S3::Client.new(
14
+ credentials: Aws::Credentials.new(config.aws.s3.access_key_id, config.aws.s3.secret_access_key),
15
+ region: config.aws.sqs.region
16
+ )
17
+ queue = sqs_client.get_queue_url({queue_name: config.aws.sqs.queue_name})
18
+
19
+ while continue_processing
20
+ begin
21
+ resp = sqs_client.receive_message( {queue_url: queue.queue_url} )
22
+ if resp.messages != nil
23
+ url = "http://#{config.aws.s3.bucket}.s3.amazonaws.com/#{resp.messages[0].body}"
24
+ puts "Converting #{url}"
25
+ system("wget -O /tmp/#{File.basename(resp.messages[0].body)} #{url} --no-cache > /dev/null")
26
+ config.versions.to_h.each do |k, v|
27
+ version_name = k
28
+ width = v['width']
29
+ height = v['height']
30
+ filename = File.basename(resp.messages[0].body)
31
+ version_filename = "#{filename.chomp(File.extname(filename))}_#{version_name}#{File.extname(filename)}"
32
+ version_path = "#{resp.messages[0].body.chomp(filename)}#{version_filename}}"
33
+ system("gm convert /tmp/#{filename} -resize '#{width}x#{height}' +profile \"*\" /tmp/#{version_filename} > /dev/null")
34
+
35
+ # Upload to S3
36
+ s3_resp = s3_client.put_object(
37
+ acl: "public-read",
38
+ body: File.open("/tmp/#{version_filename}"),
39
+ bucket: config.aws.s3.bucket,
40
+ key: version_path
41
+ )
42
+
43
+ puts 'SAVED: ' + config.aws.s3.bucket + '/' + version_path
44
+
45
+ end
46
+ sqs_client.delete_message(
47
+ queue_url: queue.queue_url,
48
+ receipt_handle: resp.messages[0].receipt_handle
49
+ )
50
+ end
51
+ rescue
52
+ # Immediately return this item to the queue for processing.
53
+ begin
54
+ if resp && resp.messages != nil
55
+ sqs_client.change_message_visibility(
56
+ queue_url: queue.queue_url,
57
+ receipt_handle: resp.messages[0].receipt_handle,
58
+ visibility_timeout: 0
59
+ )
60
+ end
61
+ rescue
62
+ # Just continue anyway
63
+ end
64
+ end
65
+ sleep 0.1
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,9 @@
1
+ require "sqs_image_processor/version"
2
+ require "sqs_image_processor/manager"
3
+ require "sqs_image_processor/config"
4
+ require "sqs_image_processor/process_manager"
5
+ require "sqs_image_processor/worker"
6
+ require "sqs_image_processor/generators/config"
7
+
8
+ module SqsImageProcessor
9
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sqs_image_processor/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sqs_image_processor"
8
+ spec.version = SqsImageProcessor::VERSION
9
+ spec.authors = ["Neil Turner"]
10
+ spec.email = ["neil@neilturner.me"]
11
+ spec.summary = %q{Write a short summary. Required.}
12
+ spec.description = %q{Write a longer description. Optional.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ #spec.executables = [spec.files.grep(%r{^bin/}) { |f| File.basename(f) }]
18
+ spec.executables = ["sqs_image_processor"]
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "thor", "~> 0.19.1"
23
+ spec.add_dependency "recursive-open-struct", "~> 0.5.0"
24
+ spec.add_dependency "aws-sdk-core" #, "~> 2.0"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.5"
27
+ spec.add_development_dependency "rake"
28
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sqs_image_processor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Neil Turner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.19.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.19.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: recursive-open-struct
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.5.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.5.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: aws-sdk-core
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Write a longer description. Optional.
84
+ email:
85
+ - neil@neilturner.me
86
+ executables:
87
+ - sqs_image_processor
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - bin/sqs_image_processor
97
+ - lib/sqs_image_processor.rb
98
+ - lib/sqs_image_processor/config.rb
99
+ - lib/sqs_image_processor/generators/config.rb
100
+ - lib/sqs_image_processor/generators/templates/sqs_image_processor_config.yml
101
+ - lib/sqs_image_processor/manager.rb
102
+ - lib/sqs_image_processor/process_manager.rb
103
+ - lib/sqs_image_processor/version.rb
104
+ - lib/sqs_image_processor/worker.rb
105
+ - sqs_image_processor.gemspec
106
+ homepage: ''
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.2.2
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Write a short summary. Required.
130
+ test_files: []