hardcode 0.0.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: 5127cde3129d52731e40cd2079ffbd8d5fe01ecb
4
+ data.tar.gz: 3fedee971f787fc479abf0af1dfbb3e9c18018a8
5
+ SHA512:
6
+ metadata.gz: 33b928212ee43de7f5a8b39976287e75755096e6216e61b74d7eac98117d7af2c15efc755c1cf3d613411d865dda490444ff433762d09782a285a910c995d386
7
+ data.tar.gz: 96284c72af24de8eacdc2f3bdb6ada085ac58d408abb24b9b863b57cb9f88d0c8dd0626ea5620fea64e480a50cfec62de6c1b710074efdeed16d882f35694698
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hardcode.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 niwo
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/Procfile ADDED
@@ -0,0 +1 @@
1
+ worker: bin/hardcode run_worker
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # Hardcode
2
+
3
+ (stack-encode)[https://github.com/swisstxt/stack-encode] on steroids (using a rabbitmq worker queue)
4
+
5
+ ## Installation
6
+
7
+ Install the gem:
8
+
9
+ $ gem install hardcode
10
+
11
+ ## Dependencies
12
+
13
+ - ruby >= 2.0
14
+ - RabbitMQ
15
+ - lsof
16
+
17
+ ## Usage
18
+
19
+ run `harcode help` for instructions
20
+
21
+ ### Example: Enqueue Encoding Jobs to RabbitMQ
22
+
23
+ ## Running the worker in production
24
+
25
+ Put the following systemd configuration under /usr/lib/systemd/system/hardcode.service (for RHEL/CentOS 7) and adapt it to your needs:
26
+
27
+ ```
28
+ [Unit]
29
+ Description=Hardcode Worker
30
+ After=syslog.target
31
+ After=network.target
32
+
33
+ [Service]
34
+ Type=simple
35
+ User=root
36
+ Group=root
37
+ ExecStart=hardcode work
38
+
39
+ # Give a reasonable amount of time for the workers to start up/shut down
40
+ TimeoutSec=300
41
+
42
+ [Install]
43
+ WantedBy=multi-user.target
44
+ ```
45
+
46
+ ## Contributing
47
+
48
+ 1. Fork it ( https://github.com/[my-github-username]/hardcode/fork )
49
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
50
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
51
+ 4. Push to the branch (`git push origin my-new-feature`)
52
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/gem_tasks"
2
+ require 'bundler'
3
+ Bundler::GemHelper.install_tasks
data/bin/hardcode ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'hardcode'
4
+
5
+ Hardcode::Cli.start(ARGV)
data/hardcode.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hardcode/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hardcode"
8
+ spec.version = Hardcode::VERSION
9
+ spec.authors = ["niwo"]
10
+ spec.email = ["nik.wolfgramm@gmail.com"]
11
+ spec.summary = %q{stack-encode on steroids (using a rabbitmq worker queue)}
12
+ spec.description = %q{stack-encode on steroids (using a rabbitmq worker queue)}
13
+ spec.homepage = "https://github.com/swisstxt/hardcode"
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.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "thor"
22
+ spec.add_dependency "sneakers"
23
+ spec.add_dependency "bunny", ">= 0.9.1"
24
+ spec.add_dependency "json"
25
+ spec.add_dependency "stack-encode"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.6"
28
+ spec.add_development_dependency "rake"
29
+ end
data/lib/hardcode.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "hardcode/version"
2
+ require "hardcode/worker"
3
+ require "hardcode/cli"
@@ -0,0 +1,97 @@
1
+ module Hardcode
2
+ require 'thor'
3
+ require 'fileutils'
4
+ require 'bunny'
5
+ require 'json'
6
+ require 'logger'
7
+ require 'sneakers/runner'
8
+
9
+ LOCK_FILE='/var/run/hardcode.lock'
10
+
11
+ class Cli < Thor
12
+ include Thor::Actions
13
+
14
+ def self.exit_on_failure?
15
+ true
16
+ end
17
+
18
+ # catch control-c and exit
19
+ trap("SIGINT") {
20
+ puts " bye"
21
+ exit!
22
+ }
23
+
24
+ package_name "hardcode"
25
+ map %w(-v --version) => :version
26
+
27
+ desc "version", "Outputs the version number"
28
+ def version
29
+ say "hardcode v#{Hardcode::VERSION}"
30
+ end
31
+
32
+ desc "enqueue DIR", "Scans a source directory, moves the files to tmp and enqueues transcoding jobs to rabbitmq"
33
+ option :destination,
34
+ desc: "destination directory",
35
+ aliases: '-d',
36
+ default: '/var/www/'
37
+ option :tmp_dir,
38
+ desc: "temporary directory",
39
+ aliases: '-t',
40
+ default: '/tmp'
41
+ def enqueue(source_dir)
42
+ if File.exists? LOCK_FILE
43
+ puts "Lockfile present: #{LOCK_FILE}"
44
+ puts "Schedule the job to run in 2 minutes."
45
+ %x[echo #{File.expand_path(__FILE__)} | at now + 2 minutes]
46
+ exit $?.exitstatus
47
+ end
48
+
49
+ begin
50
+ FileUtils.touch LOCK_FILE
51
+ conn = Bunny.new
52
+ conn.start
53
+ ch = conn.create_channel
54
+ q = ch.queue('stack_encode', durable: true)
55
+ Dir.glob(File.join(source_dir, "*.*")) do |source_file|
56
+ # wait until the file is fully written and not uploaded anymore
57
+ while system %Q[lsof '#{source_file}']
58
+ sleep 1
59
+ end
60
+ FileUtils.mv(source_file, options[:tmp_dir], verbose: true)
61
+ ch.default_exchange.publish(
62
+ {
63
+ source: File.join(options[:tmp_dir], File.basename(source_file)),
64
+ dest_dir: options[:destination]
65
+ }.to_json,
66
+ routing_key: q.name,
67
+ persistent: true
68
+ )
69
+ end
70
+ rescue => e
71
+ puts "ERROR: #{e.message}"
72
+ ensure
73
+ FileUtils.rm(LOCK_FILE) if File.exists?(LOCK_FILE)
74
+ end
75
+ end
76
+
77
+ desc "work", "Start the sneakers based workers"
78
+ option :ampq_url,
79
+ desc: "AMPQ URL",
80
+ default: 'amqp://guest:guest@localhost:5672'
81
+ option :debug,
82
+ desc: "Enable debug output",
83
+ type: :boolean
84
+ def work
85
+ Sneakers.configure(
86
+ amqp: options[:ampq_url],
87
+ daemonize: false,
88
+ log: STDOUT,
89
+ metrics: Sneakers::Metrics::LoggingMetrics.new
90
+ )
91
+ Sneakers.logger.level = options[:debug] ? Logger::DEBUG : Logger::INFO
92
+ r = Sneakers::Runner.new([ Hardcode::Worker ])
93
+ r.run
94
+ end
95
+
96
+ end # class
97
+ end # module
@@ -0,0 +1,3 @@
1
+ module Hardcode
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,40 @@
1
+ module Hardcode
2
+ require 'sneakers'
3
+ require 'sneakers/metrics/logging_metrics'
4
+ require 'json'
5
+ require 'fileutils'
6
+
7
+ STACK_ENCODE_LOG='/var/log/hardcode.log'
8
+
9
+ class Worker
10
+ include Sneakers::Worker
11
+ from_queue 'stack_encode',
12
+ timeout_job_after: 600
13
+
14
+ def work(msg)
15
+ begin
16
+ job = JSON.parse(msg)
17
+ source_file = job['source']
18
+ if File.extname(source_file).match("^\.(mp4|mp3)$") != nil
19
+ FileUtils.mv(source_file, job['dest_dir'], verbose: true)
20
+ else
21
+ puts output = %x[stack-encode encode --no-progress -l #{STACK_ENCODE_LOG} '#{source_file}']
22
+ if $?.success?
23
+ puts filename = output[/.*>\s(.*)$/, 1]
24
+ puts "Transcoding successful, deleting source file."
25
+ FileUtils.mv(File.join(File.dirname(source_file), filename), job['dest_dir'], verbose: true)
26
+ FileUtils.rm(source_file, verbose: true)
27
+ else
28
+ puts "Error: Transcoding failed."
29
+ end
30
+ end
31
+ rescue => e
32
+ worker_trace message = "Error: #{e.backtrace}"
33
+ raise message
34
+ end
35
+ worker_trace "Finished: #{job.to_s}"
36
+ ack!
37
+ end
38
+
39
+ end # class
40
+ end # module
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hardcode
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - niwo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-03 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'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sneakers
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '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'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bunny
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.9.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 0.9.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: stack-encode
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.6'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.6'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: stack-encode on steroids (using a rabbitmq worker queue)
112
+ email:
113
+ - nik.wolfgramm@gmail.com
114
+ executables:
115
+ - hardcode
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - Procfile
123
+ - README.md
124
+ - Rakefile
125
+ - bin/hardcode
126
+ - hardcode.gemspec
127
+ - lib/hardcode.rb
128
+ - lib/hardcode/cli.rb
129
+ - lib/hardcode/version.rb
130
+ - lib/hardcode/worker.rb
131
+ homepage: https://github.com/swisstxt/hardcode
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.0.3
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: stack-encode on steroids (using a rabbitmq worker queue)
155
+ test_files: []