sync_machine 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: 594e4e0870147957bb2f36f65b6b028433bf5ee3
4
+ data.tar.gz: 58a8af1ae9acd3b64fdff79414d6fcc3e7499332
5
+ SHA512:
6
+ metadata.gz: da5e1182ccf5d8623190e170826e122bc6b60a819a707c4f16c61167009d90140e35263ce9f634f95bf66d17065c047cbffa940320e68a475a0939b2ca48ee80
7
+ data.tar.gz: 0a94e81f19b4096e233fa457946c59ee5a0e669e95e908fe9e80ba9efa49b359f8a8b7ccf3517ebb7e839789057bf2c8e8b8a1b684cabaf6938653ee25f15921
@@ -0,0 +1,69 @@
1
+ # Ruby CircleCI 2.0 configuration file
2
+ #
3
+ # Check https://circleci.com/docs/2.0/language-ruby/ for more details
4
+ #
5
+ version: 2
6
+ jobs:
7
+ build:
8
+ docker:
9
+ # specify the version you desire here
10
+ - image: circleci/ruby:2.4.1-node-browsers
11
+ - image: circleci/mongo:3.6.9
12
+ - image: redis:3.2.11
13
+
14
+ # Specify service dependencies here if necessary
15
+ # CircleCI maintains a library of pre-built images
16
+ # documented at https://circleci.com/docs/2.0/circleci-images/
17
+ # - image: circleci/postgres:9.4
18
+
19
+ working_directory: ~/repo
20
+
21
+ steps:
22
+ - checkout
23
+
24
+ # Download and cache dependencies
25
+ - restore_cache:
26
+ keys:
27
+ - v1-dependencies-{{ checksum "Gemfile.lock" }}
28
+ # fallback to using the latest cache if no exact match is found
29
+ - v1-dependencies-
30
+
31
+ - run:
32
+ name: Configure Bundler
33
+ command: |
34
+ echo 'export BUNDLER_VERSION=$(cat Gemfile.lock | tail -1 | tr -d " ")' >> $BASH_ENV
35
+ source $BASH_ENV
36
+ gem install bundler
37
+
38
+ - run:
39
+ name: install dependencies
40
+ command: |
41
+ bundle install --jobs=4 --retry=3 --path vendor/bundle
42
+
43
+ - save_cache:
44
+ paths:
45
+ - ./vendor/bundle
46
+ key: v1-dependencies-{{ checksum "Gemfile.lock" }}
47
+
48
+ # run tests!
49
+ - run:
50
+ name: run tests
51
+ command: |
52
+ mkdir /tmp/test-results
53
+ TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | \
54
+ circleci tests split --split-by=timings)"
55
+
56
+ bundle exec rspec \
57
+ --format progress \
58
+ -r rspec_junit_formatter \
59
+ --format RspecJunitFormatter \
60
+ --out /tmp/test-results/rspec/results.xml \
61
+ --format progress \
62
+ $TEST_FILES
63
+
64
+ # collect reports
65
+ - store_test_results:
66
+ path: /tmp/test-results
67
+ - store_artifacts:
68
+ path: /tmp/test-results
69
+ destination: test-results
data/.codeclimate.yml ADDED
@@ -0,0 +1,38 @@
1
+ version: "2" # required to adjust maintainability checks
2
+ checks:
3
+ argument-count:
4
+ config:
5
+ threshold: 4
6
+ complex-logic:
7
+ config:
8
+ threshold: 4
9
+ file-lines:
10
+ config:
11
+ threshold: 250
12
+ method-complexity:
13
+ config:
14
+ threshold: 5
15
+ method-count:
16
+ config:
17
+ threshold: 20
18
+ method-lines:
19
+ config:
20
+ threshold: 25
21
+ nested-control-flow:
22
+ config:
23
+ threshold: 4
24
+ return-statements:
25
+ config:
26
+ threshold: 4
27
+ similar-code:
28
+ config:
29
+ threshold: 150
30
+ identical-code:
31
+ config:
32
+ threshold: 75
33
+ plugins:
34
+ reek:
35
+ enabled: true
36
+ rubocop:
37
+ enabled: true
38
+
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.reek.yml ADDED
@@ -0,0 +1,9 @@
1
+ ---
2
+ detectors:
3
+ DuplicateMethodCall:
4
+ max_calls: 3
5
+ UtilityFunction:
6
+ public_methods_only: true
7
+
8
+ exclude_paths:
9
+ - spec
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,55 @@
1
+ Layout/ExtraSpacing:
2
+ Enabled: false
3
+
4
+ Layout/SpaceAroundOperators:
5
+ Enabled: false
6
+
7
+ Layout/SpaceInsideHashLiteralBraces:
8
+ Enabled: false
9
+
10
+ Layout/TrailingBlankLines:
11
+ Enabled: false
12
+
13
+ Metrics/BlockLength:
14
+ Exclude:
15
+ - 'spec/**/*'
16
+ - 'sync_machine.gemspec'
17
+
18
+ Metrics/LineLength:
19
+ Max: 100
20
+
21
+ # Handled by Reek
22
+ Metrics/MethodLength:
23
+ Enabled: false
24
+
25
+ Style/BlockDelimiters:
26
+ Enabled: false
27
+
28
+ Style/EmptyMethod:
29
+ Exclude:
30
+ - 'spec/**/*'
31
+
32
+ Style/ExpandPathArguments:
33
+ Enabled: false
34
+
35
+ Style/MethodMissing:
36
+ Enabled: false
37
+
38
+ Style/PercentLiteralDelimiters:
39
+ Enabled: false
40
+
41
+ Style/RandomWithOffset:
42
+ Enabled: false
43
+
44
+ Style/StringLiterals:
45
+ Enabled: false
46
+
47
+ Style/SymbolArray:
48
+ Enabled: false
49
+
50
+ Style/SymbolProc:
51
+ Exclude:
52
+ - 'spec/**/*'
53
+
54
+ Style/UnneededPercentQ:
55
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.2
7
+ before_install: gem install bundler -v 2.0.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at sera@fhwang.net. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in sync_machine.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,80 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ sync_machine (0.1.0)
5
+ activesupport
6
+ fhwang-wisper-mongoid
7
+ mongoid
8
+ sidekiq
9
+ wisper
10
+
11
+ GEM
12
+ remote: https://rubygems.org/
13
+ specs:
14
+ activemodel (5.2.1)
15
+ activesupport (= 5.2.1)
16
+ activesupport (5.2.1)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ bson (4.3.0)
22
+ concurrent-ruby (1.0.5)
23
+ connection_pool (2.2.2)
24
+ diff-lcs (1.3)
25
+ factory_bot (5.0.2)
26
+ activesupport (>= 4.2.0)
27
+ fhwang-wisper-mongoid (0.2.0)
28
+ mongoid (>= 6)
29
+ wisper
30
+ i18n (1.1.1)
31
+ concurrent-ruby (~> 1.0)
32
+ minitest (5.11.3)
33
+ mongo (2.6.2)
34
+ bson (>= 4.3.0, < 5.0.0)
35
+ mongoid (6.4.2)
36
+ activemodel (>= 5.1, < 6.0.0)
37
+ mongo (>= 2.5.1, < 3.0.0)
38
+ rack (2.0.6)
39
+ rack-protection (2.0.4)
40
+ rack
41
+ rake (10.4.2)
42
+ redis (4.0.2)
43
+ rspec (3.6.0)
44
+ rspec-core (~> 3.6.0)
45
+ rspec-expectations (~> 3.6.0)
46
+ rspec-mocks (~> 3.6.0)
47
+ rspec-core (3.6.0)
48
+ rspec-support (~> 3.6.0)
49
+ rspec-expectations (3.6.0)
50
+ diff-lcs (>= 1.2.0, < 2.0)
51
+ rspec-support (~> 3.6.0)
52
+ rspec-mocks (3.6.0)
53
+ diff-lcs (>= 1.2.0, < 2.0)
54
+ rspec-support (~> 3.6.0)
55
+ rspec-support (3.6.0)
56
+ rspec_junit_formatter (0.4.1)
57
+ rspec-core (>= 2, < 4, != 2.12.0)
58
+ sidekiq (5.0.5)
59
+ concurrent-ruby (~> 1.0)
60
+ connection_pool (~> 2.2, >= 2.2.0)
61
+ rack-protection (>= 1.5.0)
62
+ redis (>= 3.3.4, < 5)
63
+ thread_safe (0.3.6)
64
+ tzinfo (1.2.5)
65
+ thread_safe (~> 0.1)
66
+ wisper (2.0.0)
67
+
68
+ PLATFORMS
69
+ ruby
70
+
71
+ DEPENDENCIES
72
+ bundler (~> 2.0)
73
+ factory_bot
74
+ rake (~> 10.0)
75
+ rspec (~> 3.0)
76
+ rspec_junit_formatter
77
+ sync_machine!
78
+
79
+ BUNDLED WITH
80
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Francis Hwang
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.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # SyncMachine
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/sync_machine`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'sync_machine'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install sync_machine
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/sync_machine. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the SyncMachine project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/sync_machine/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sync_machine"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,57 @@
1
+ require "wisper"
2
+
3
+ module SyncMachine
4
+ # Listen to changes in any model that could result in a change to the
5
+ # published document, and enqueue a FindSubjectsWorker job when the change
6
+ # occurs.
7
+ class ChangeListener
8
+ def self.inherited(base)
9
+ base.cattr_accessor :model_syms
10
+ end
11
+
12
+ def self.listen_to_models(*model_syms)
13
+ self.model_syms = model_syms
14
+ model_syms.each do |model_sym|
15
+ send(
16
+ :alias_method,
17
+ "update_#{model_sym}_successful".to_sym,
18
+ :after_subject_saved
19
+ )
20
+ end
21
+ end
22
+
23
+ def self.subscribe
24
+ Wisper.subscribe(new)
25
+ end
26
+
27
+ def after_create(subject)
28
+ model_sym = subject.class.name.underscore.to_sym
29
+ return unless self.class.model_syms.include?(model_sym)
30
+ after_subject_saved(subject)
31
+ end
32
+
33
+ def after_subject_saved(subject)
34
+ return unless changed_keys(subject).present?
35
+ sync_module = SyncMachine.sync_module(self.class)
36
+ finder_class = sync_module.const_get('FindSubjectsWorker')
37
+ finder_class.perform_async(
38
+ subject.class.name,
39
+ subject.id.to_s,
40
+ changed_keys(subject),
41
+ Time.now.iso8601
42
+ )
43
+ end
44
+
45
+ private
46
+
47
+ def changed_keys(subject)
48
+ changed_keys = subject.changes.keys
49
+ subject.class.reflect_on_all_associations.each do |assoc|
50
+ if assoc.macro == :embeds_one && subject.send(assoc.name).try(:changed?)
51
+ changed_keys << assoc.name
52
+ end
53
+ end
54
+ changed_keys
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,46 @@
1
+ module SyncMachine
2
+ class EnsurePublication
3
+ # Ensures that the current job is not:
4
+ # 1. Currently being run by another worker
5
+ # 2. Rendered unnecessary by the fact another job with the same subject was
6
+ # completed since the time this job was requested.
7
+ class Deduper
8
+ def initialize(enqueue_time_str:, last_job_finished_at:, job_class:, subject_id:)
9
+ @enqueue_time_str = enqueue_time_str
10
+ @job_class = job_class
11
+ @last_job_finished_at = last_job_finished_at
12
+ @subject_id = subject_id
13
+ end
14
+
15
+ def dedupe
16
+ if acquire_lock
17
+ begin
18
+ yield unless performed_since_enqueue_time?
19
+ ensure
20
+ Redis.current.del(redis_lock)
21
+ end
22
+ else
23
+ @job_class.perform_in(1 + rand(10), @subject_id, @enqueue_time_str)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def acquire_lock
30
+ Redis.current.set(redis_lock, "true", nx: true, ex: 10.minutes)
31
+ end
32
+
33
+ def enqueue_time
34
+ @enqueue_time ||= Time.parse(@enqueue_time_str)
35
+ end
36
+
37
+ def performed_since_enqueue_time?
38
+ @last_job_finished_at && @last_job_finished_at > enqueue_time
39
+ end
40
+
41
+ def redis_lock
42
+ "#{@job_class.name}:#{@subject_id}"
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,54 @@
1
+ module SyncMachine
2
+ class EnsurePublication
3
+ # Manage logic around whether the document was previously published at all,
4
+ # and maintaining history for future jobs.
5
+ class PublicationHistory
6
+ def initialize(subject_id:, sync_module:)
7
+ @subject_id = subject_id
8
+ @sync_module = sync_module
9
+ end
10
+
11
+ def last_publish_equals?(payload_body)
12
+ return false unless previous_payload
13
+ HashWithIndifferentAccess.new(previous_payload.body) ==
14
+ HashWithIndifferentAccess.new(payload_body)
15
+ end
16
+
17
+ def last_job_finished_at
18
+ previous_payload.try(:generated_at)
19
+ end
20
+
21
+ def record_generation_time
22
+ previous_payload.update_attribute(:generated_at, Time.now)
23
+ end
24
+
25
+ def update(payload_body)
26
+ if previous_payload
27
+ previous_payload.update_attributes(
28
+ body: payload_body, generated_at: Time.now
29
+ )
30
+ else
31
+ create_payload_record(payload_body)
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def create_payload_record(payload_body)
38
+ payload_class.create!(
39
+ body: payload_body,
40
+ generated_at: Time.now,
41
+ subject_id: @subject_id.to_s
42
+ )
43
+ end
44
+
45
+ def payload_class
46
+ @payload_class ||= @sync_module.const_get('Payload')
47
+ end
48
+
49
+ def previous_payload
50
+ @previous_payload ||= payload_class.where(subject_id: @subject_id).first
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,62 @@
1
+ module SyncMachine
2
+ # Orchestrate logic around a change notification and whether to publish a new
3
+ # document to the service.
4
+ class EnsurePublication
5
+ def initialize(enqueue_time_str:, hooks:, subject:, sync_module:)
6
+ @enqueue_time_str = enqueue_time_str
7
+ @hooks = hooks
8
+ @subject = subject
9
+ @sync_module = sync_module
10
+ end
11
+
12
+ def run
13
+ dedupe { run_deduped }
14
+ end
15
+
16
+ private
17
+
18
+ def call_after_publish
19
+ return unless (after_publish_hook = hook(:after_publish))
20
+ after_publish_hook.call(@subject)
21
+ end
22
+
23
+ def dedupe
24
+ deduper = Deduper.new(
25
+ job_class: @sync_module.const_get('EnsurePublicationWorker'),
26
+ last_job_finished_at: publication_history.last_job_finished_at,
27
+ subject_id: subject_id,
28
+ enqueue_time_str: @enqueue_time_str
29
+ )
30
+ deduper.dedupe { yield }
31
+ end
32
+
33
+ def hook(hook_sym)
34
+ @hooks[hook_sym]
35
+ end
36
+
37
+ def payload_body
38
+ @payload_body ||= hook(:build).call(@subject)
39
+ end
40
+
41
+ def publication_history
42
+ @publication_history ||= PublicationHistory.new(
43
+ subject_id: subject_id, sync_module: @sync_module
44
+ )
45
+ end
46
+
47
+ def run_deduped
48
+ return unless hook(:check_publishable).call(@subject)
49
+ if publication_history.last_publish_equals?(payload_body)
50
+ publication_history.record_generation_time
51
+ else
52
+ hook(:publish).call(@subject, payload_body)
53
+ publication_history.update(payload_body)
54
+ end
55
+ call_after_publish
56
+ end
57
+
58
+ def subject_id
59
+ @subject.id.to_s
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,39 @@
1
+ require "active_support/core_ext/class"
2
+ require "sidekiq"
3
+
4
+ module SyncMachine
5
+ # Call EnsurePublication service via a Sidekiq job.
6
+ class EnsurePublicationWorker
7
+ include Sidekiq::Worker
8
+
9
+ class_attribute :hooks
10
+
11
+ [:build, :check_publishable, :publish, :after_publish].each do |hook|
12
+ define_singleton_method(hook) do |&block|
13
+ hooks[hook] = block
14
+ end
15
+ end
16
+
17
+ def self.inherited(subclass)
18
+ subclass.hooks = {}
19
+ end
20
+
21
+ def perform(subject_id, enqueue_time_str)
22
+ subject = find_subject(subject_id)
23
+ EnsurePublication.new(
24
+ enqueue_time_str: enqueue_time_str,
25
+ hooks: self.class.hooks,
26
+ subject: subject,
27
+ sync_module: SyncMachine.sync_module(self.class)
28
+ ).run
29
+ end
30
+
31
+ private
32
+
33
+ def find_subject(subject_id)
34
+ sync_module = SyncMachine.sync_module(self.class)
35
+ subject_class = sync_module.subject_class
36
+ subject_class.find(subject_id)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,60 @@
1
+ require "sidekiq"
2
+
3
+ module SyncMachine
4
+ # After one observed model has changed, find all relevant subjects whose
5
+ # documents may have changed. Enqueues one EnsurePublicationWorker job for
6
+ # each relevant subject.
7
+ class FindSubjectsWorker
8
+ include Sidekiq::Worker
9
+
10
+ class_attribute :hooks
11
+
12
+ def self.inherited(subclass)
13
+ subclass.hooks = {}
14
+ end
15
+
16
+ def self.method_missing(meth, *args, &block)
17
+ if meth.to_s =~ /^subject_ids_from_.*/
18
+ hooks[meth] = Hook.new(block)
19
+ else
20
+ super
21
+ end
22
+ end
23
+
24
+ # :reek:LongParameterList is unavoidable here since this is a Sidekiq
25
+ # worker
26
+ def perform(record_class_name, record_id, changed_keys, enqueue_time)
27
+ record = record_class_name.constantize.find(record_id)
28
+ source_ids = find_source_ids(record, changed_keys)
29
+ (source_ids || []).each do |source_id|
30
+ self.class.parent.const_get('EnsurePublicationWorker').perform_async(
31
+ source_id, enqueue_time
32
+ )
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def find_source_ids(record, changed_keys)
39
+ hook_name = (
40
+ "subject_ids_from_" + record.class.name.gsub(/::/, '').underscore).to_sym
41
+ self.class.hooks[hook_name].call(record, changed_keys)
42
+ end
43
+
44
+ # Wrap a "subject_ids_from_*" block.
45
+ class Hook
46
+ def initialize(block)
47
+ @block = block
48
+ end
49
+
50
+ def call(record, changed_keys)
51
+ raw_source_ids = if @block.arity == 2
52
+ @block.call(record, changed_keys)
53
+ else
54
+ @block.call(record)
55
+ end
56
+ Array.wrap(raw_source_ids).map(&:to_s)
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module SyncMachine
2
+ VERSION = "0.1.0".freeze
3
+ end
@@ -0,0 +1,56 @@
1
+ require "active_support/core_ext/class"
2
+ require "active_support/core_ext/string"
3
+ require "mongoid"
4
+ require "sync_machine/change_listener"
5
+ require "sync_machine/ensure_publication"
6
+ require "sync_machine/ensure_publication/deduper"
7
+ require "sync_machine/ensure_publication/publication_history"
8
+ require "sync_machine/ensure_publication_worker"
9
+ require "sync_machine/find_subjects_worker"
10
+ require "sync_machine/version"
11
+ require "wisper/mongoid"
12
+
13
+ # A mini-framework for intelligently publishing complex model changes to an
14
+ # external API..
15
+ module SyncMachine
16
+ def self.extended(base)
17
+ base.mattr_accessor :subject_sym
18
+ end
19
+
20
+ def self.sync_module(child_const)
21
+ child_const.name.split(/::/).first.constantize
22
+ end
23
+
24
+ def subject(subject_sym)
25
+ self.subject_sym = subject_sym
26
+ end
27
+
28
+ def subject_class
29
+ subject_sym.to_s.camelize.constantize
30
+ end
31
+
32
+ def setup
33
+ define_payload_class unless const_defined?('Payload')
34
+ const_get('ChangeListener').subscribe
35
+ end
36
+
37
+ # :reek:TooManyStatements is unavoidable with this sort of dynamic class
38
+ # definition
39
+ def define_payload_class
40
+ collection_name = name.underscore + '_payloads'
41
+ payload_class = Class.new do
42
+ include Mongoid::Document
43
+ store_in collection: collection_name
44
+
45
+ field :body, type: Hash
46
+ field :generated_at, type: Time
47
+ field :subject_id, type: String
48
+
49
+ validates :generated_at, presence: true
50
+ validates :subject_id, presence: true, uniqueness: true
51
+ end
52
+ const_set('Payload', payload_class)
53
+ end
54
+ end
55
+
56
+ Wisper::Mongoid.extend_all
@@ -0,0 +1,49 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "sync_machine/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sync_machine"
8
+ spec.version = SyncMachine::VERSION
9
+ spec.authors = ["Francis Hwang"]
10
+ spec.email = ["sera@fhwang.net"]
11
+
12
+ spec.summary =
13
+ %q{A mini-framework for intelligently publishing complex model changes to an external API.}
14
+ spec.description =
15
+ %q{A mini-framework for intelligently publishing complex model changes to an external API.}
16
+ spec.homepage = "https://github.com/fhwang/sync_machine"
17
+ spec.license = "MIT"
18
+
19
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
20
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
21
+ if spec.respond_to?(:metadata)
22
+ spec.metadata["homepage_uri"] = spec.homepage
23
+ spec.metadata["source_code_uri"] = "https://github.com/fhwang/sync_machine"
24
+ else
25
+ raise "RubyGems 2.0 or newer is required to protect against " \
26
+ "public gem pushes."
27
+ end
28
+
29
+ # Specify which files should be added to the gem when it is released.
30
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
31
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
32
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
33
+ end
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ spec.add_runtime_dependency "activesupport"
39
+ spec.add_runtime_dependency "fhwang-wisper-mongoid"
40
+ spec.add_runtime_dependency "mongoid"
41
+ spec.add_runtime_dependency "sidekiq"
42
+ spec.add_runtime_dependency "wisper"
43
+
44
+ spec.add_development_dependency "bundler", "~> 2.0"
45
+ spec.add_development_dependency "factory_bot"
46
+ spec.add_development_dependency "rake", "~> 10.0"
47
+ spec.add_development_dependency "rspec", "~> 3.0"
48
+ spec.add_development_dependency "rspec_junit_formatter"
49
+ end
metadata ADDED
@@ -0,0 +1,212 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sync_machine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Francis Hwang
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-03-26 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: fhwang-wisper-mongoid
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: mongoid
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: sidekiq
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: wisper
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: '2.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: factory_bot
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
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '10.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '10.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '3.0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '3.0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rspec_junit_formatter
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: A mini-framework for intelligently publishing complex model changes to
154
+ an external API.
155
+ email:
156
+ - sera@fhwang.net
157
+ executables: []
158
+ extensions: []
159
+ extra_rdoc_files: []
160
+ files:
161
+ - ".circleci/config.yml"
162
+ - ".codeclimate.yml"
163
+ - ".gitignore"
164
+ - ".reek.yml"
165
+ - ".rspec"
166
+ - ".rubocop.yml"
167
+ - ".travis.yml"
168
+ - CODE_OF_CONDUCT.md
169
+ - Gemfile
170
+ - Gemfile.lock
171
+ - LICENSE.txt
172
+ - README.md
173
+ - Rakefile
174
+ - bin/console
175
+ - bin/setup
176
+ - lib/sync_machine.rb
177
+ - lib/sync_machine/change_listener.rb
178
+ - lib/sync_machine/ensure_publication.rb
179
+ - lib/sync_machine/ensure_publication/deduper.rb
180
+ - lib/sync_machine/ensure_publication/publication_history.rb
181
+ - lib/sync_machine/ensure_publication_worker.rb
182
+ - lib/sync_machine/find_subjects_worker.rb
183
+ - lib/sync_machine/version.rb
184
+ - sync_machine.gemspec
185
+ homepage: https://github.com/fhwang/sync_machine
186
+ licenses:
187
+ - MIT
188
+ metadata:
189
+ homepage_uri: https://github.com/fhwang/sync_machine
190
+ source_code_uri: https://github.com/fhwang/sync_machine
191
+ post_install_message:
192
+ rdoc_options: []
193
+ require_paths:
194
+ - lib
195
+ required_ruby_version: !ruby/object:Gem::Requirement
196
+ requirements:
197
+ - - ">="
198
+ - !ruby/object:Gem::Version
199
+ version: '0'
200
+ required_rubygems_version: !ruby/object:Gem::Requirement
201
+ requirements:
202
+ - - ">="
203
+ - !ruby/object:Gem::Version
204
+ version: '0'
205
+ requirements: []
206
+ rubyforge_project:
207
+ rubygems_version: 2.6.14
208
+ signing_key:
209
+ specification_version: 4
210
+ summary: A mini-framework for intelligently publishing complex model changes to an
211
+ external API.
212
+ test_files: []