ruboty-cron 0.0.9

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: e2f7ab993c9ab719e8d1e61a5a7cba4df0c5cc9f
4
+ data.tar.gz: 19c050dbdc81bf51f0845f21e25980ac24ecdc3e
5
+ SHA512:
6
+ metadata.gz: 8567af9b2af80bdeb77ca3845a009f727761ef4d79a14c15fda8b24c7a1d12de358d032a54a8fdc9318268fe45ae6f13fe83e9c47f2ea6fb057965faaaf9c95d
7
+ data.tar.gz: fb7fe40f368ff3746616f05ef4cfa40c13fb79fd134d0a31161df4fdaae16c497f645483666fa43c514587ec4a4f8ed9aabd37b562497c8ba5421483e1f8b1bc
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/CHANGELOG.md ADDED
@@ -0,0 +1,27 @@
1
+ ## 0.0.9
2
+ * Rename: Ellen -> Ruboty
3
+
4
+ ## 0.0.8
5
+ * Fix loaded modules
6
+
7
+ ## 0.0.7
8
+ * Format message if adapter supports :code option
9
+
10
+ ## 0.0.6
11
+ * Store & Rebuild all message attributes
12
+
13
+ ## 0.0.5
14
+ * Says where job is registered on
15
+
16
+ ## 0.0.4
17
+ * Support Ruboty v0.2.0
18
+
19
+ ## 0.0.3
20
+ * Remember registered jobs after initialized
21
+ * Stop running job before its deletion
22
+
23
+ ## 0.0.2
24
+ * Support the new Brain interface
25
+
26
+ ## 0.0.1
27
+ * 1st Release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruboty-cron.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ryo Nakamura
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,13 @@
1
+ # Ruboty::Cron
2
+ Mount cron system to [Ruboty]("https://github.com/r7kamura/ruboty") to schedule messages on a specific time.
3
+
4
+ ## Usage
5
+ You can use any [Chrono](https://github.com/r7kamura/chrono/) compatible cron syntax to schedule messages.
6
+
7
+ ```
8
+ @ruboty add job "<cron syntax>" <message> - Add a new cron job
9
+ @ruboty delete job <id> - Delete a cron job
10
+ @ruboty list jobs - List all cron jobs
11
+ ```
12
+
13
+ ![](https://raw.githubusercontent.com/r7kamura/ruboty-cron/master/images/screenshot.png)
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
Binary file
@@ -0,0 +1,9 @@
1
+ require "active_support/core_ext/hash/except"
2
+ require "active_support/core_ext/hash/keys"
3
+ require "chrono"
4
+ require "json"
5
+
6
+ require "ruboty"
7
+ require "ruboty/handlers/cron"
8
+ require "ruboty/cron/job"
9
+ require "ruboty/cron/version"
@@ -0,0 +1,53 @@
1
+ module Ruboty
2
+ module Cron
3
+ class Job
4
+ attr_reader :attributes, :thread
5
+
6
+ def initialize(attributes)
7
+ @attributes = attributes.stringify_keys
8
+ end
9
+
10
+ def start(robot)
11
+ @thread = Thread.new do
12
+ Chrono::Trigger.new(schedule) do
13
+ Message.new(
14
+ attributes.symbolize_keys.except(:body, :id, :schedule).merge(robot: robot)
15
+ ).reply(body)
16
+ end.run
17
+ end
18
+ end
19
+
20
+ def to_hash
21
+ attributes
22
+ end
23
+
24
+ def stop
25
+ thread.kill
26
+ end
27
+
28
+ def description
29
+ %<%5s: "%s" %s> % [id, schedule, body]
30
+ end
31
+
32
+ def id
33
+ attributes["id"]
34
+ end
35
+
36
+ def schedule
37
+ attributes["schedule"]
38
+ end
39
+
40
+ def body
41
+ attributes["body"]
42
+ end
43
+
44
+ def from
45
+ attributes["from"]
46
+ end
47
+
48
+ def to
49
+ attributes["to"]
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,5 @@
1
+ module Ruboty
2
+ module Cron
3
+ VERSION = "0.0.9"
4
+ end
5
+ end
@@ -0,0 +1,98 @@
1
+ module Ruboty
2
+ module Handlers
3
+ class Cron < Base
4
+ NAMESPACE = "cron"
5
+
6
+ on(/add job "(?<schedule>.+)" (?<body>.+)/, name: "add", description: "Add a new cron job")
7
+
8
+ on(/delete job (?<id>\d+)/, name: "delete", description: "Delete a cron job")
9
+
10
+ on(/list jobs\z/, name: "list", description: "List all cron jobs")
11
+
12
+ attr_writer :jobs
13
+
14
+ def initialize(*args)
15
+ super
16
+ remember
17
+ end
18
+
19
+ def add(message)
20
+ job = create(message)
21
+ message.reply("Job #{job.id} created")
22
+ end
23
+
24
+ def delete(message)
25
+ id = message[:id].to_i
26
+ if jobs.has_key?(id)
27
+ jobs.delete(id)
28
+ running_jobs[id].stop
29
+ running_jobs.delete(id)
30
+ message.reply("Job #{id} deleted")
31
+ else
32
+ message.reply("Job #{id} does not exist")
33
+ end
34
+ end
35
+
36
+ def list(message)
37
+ message.reply(summary, code: true)
38
+ end
39
+
40
+ private
41
+
42
+ def remember
43
+ jobs.each do |id, attributes|
44
+ job = Ruboty::Cron::Job.new(attributes)
45
+ running_jobs[id] = job
46
+ job.start(robot)
47
+ end
48
+ end
49
+
50
+ def jobs
51
+ robot.brain.data[NAMESPACE] ||= {}
52
+ end
53
+
54
+ def create(message)
55
+ job = Ruboty::Cron::Job.new(
56
+ message.original.except(:robot).merge(
57
+ body: message[:body],
58
+ id: generate_id,
59
+ schedule: message[:schedule],
60
+ ),
61
+ )
62
+ jobs[job.id] = job.to_hash
63
+ job.start(robot)
64
+ running_jobs[job.id] = job
65
+ job
66
+ end
67
+
68
+ def summary
69
+ if jobs.empty?
70
+ empty_message
71
+ else
72
+ job_descriptions
73
+ end
74
+ end
75
+
76
+ def empty_message
77
+ "Job not found"
78
+ end
79
+
80
+ def job_descriptions
81
+ jobs.values.map do |attributes|
82
+ Ruboty::Cron::Job.new(attributes).description
83
+ end.join("\n")
84
+ end
85
+
86
+ def generate_id
87
+ loop do
88
+ id = rand(10000)
89
+ break id unless jobs.has_key?(id)
90
+ end
91
+ end
92
+
93
+ def running_jobs
94
+ @running_jobs ||= {}
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,24 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "ruboty/cron/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "ruboty-cron"
7
+ spec.version = Ruboty::Cron::VERSION
8
+ spec.authors = ["Ryo Nakamura"]
9
+ spec.email = ["r7kamura@gmail.com"]
10
+ spec.summary = "Mount cron system to Ruboty to schedule messages on a specific time."
11
+ spec.homepage = "https://github.com/r7kamura/ruboty-cron"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_dependency "activesupport"
20
+ spec.add_dependency "chrono"
21
+ spec.add_dependency "ruboty", ">= 0.2.0"
22
+ spec.add_development_dependency "bundler", "~> 1.6"
23
+ spec.add_development_dependency "rake"
24
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruboty-cron
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.9
5
+ platform: ruby
6
+ authors:
7
+ - Ryo Nakamura
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
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: chrono
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: ruboty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.2.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.2.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.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.6'
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:
84
+ email:
85
+ - r7kamura@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - CHANGELOG.md
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - images/screenshot.png
97
+ - lib/ruboty/cron.rb
98
+ - lib/ruboty/cron/job.rb
99
+ - lib/ruboty/cron/version.rb
100
+ - lib/ruboty/handlers/cron.rb
101
+ - ruboty-cron.gemspec
102
+ homepage: https://github.com/r7kamura/ruboty-cron
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.2.2
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Mount cron system to Ruboty to schedule messages on a specific time.
126
+ test_files: []
127
+ has_rdoc: