Class: ShopifyCLI::Theme::Extension::Syncer::ExtensionServeJob

Inherits:
ShopifyCLI::ThreadPool::Job show all
Defined in:
lib/shopify_cli/theme/extension/syncer/extension_serve_job.rb

Constant Summary collapse

POLL_FREQUENCY =

second

0.5
PUSH_INTERVAL =

seconds

5
RESPONSE_FIELD =
%w(data extensionUpdateDraft)
VERSION_FIELD =
"extensionVersion"
USER_ERRORS_FIELD =
"userErrors"
ERROR_FILE_REGEX =
/\[([^\]\[]*)\]/

Instance Attribute Summary

Attributes inherited from ShopifyCLI::ThreadPool::Job

#error, #interval

Instance Method Summary collapse

Methods inherited from ShopifyCLI::ThreadPool::Job

#call, #error?, #recurring?, #success?

Constructor Details

#initialize(ctx, syncer:, extension:, project:, specification_handler:) ⇒ ExtensionServeJob

Returns a new instance of ExtensionServeJob.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/shopify_cli/theme/extension/syncer/extension_serve_job.rb', line 20

def initialize(ctx, syncer:, extension:, project:, specification_handler:)
  super(POLL_FREQUENCY)

  @ctx = ctx
  @extension = extension
  @project = project
  @specification_handler = specification_handler

  @syncer = syncer
  @syncer_mutex = Mutex.new

  @job_in_progress = false
  @job_in_progress_mutex = Mutex.new
end

Instance Method Details

#perform!Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/shopify_cli/theme/extension/syncer/extension_serve_job.rb', line 35

def perform!
  return unless @syncer.any_operation?
  return if job_in_progress?
  return if recently_synced? && !@syncer.any_blocking_operation?

  job_in_progress!

  input = {
    api_key: @project.app.api_key,
    registration_id: @project.registration_id,
    config: JSON.generate(@specification_handler.config(@ctx)),
    extension_context: @specification_handler.extension_context(@ctx),
  }
  response = ShopifyCLI::PartnersAPI.query(@ctx, "extension_update_draft", **input).dig(*RESPONSE_FIELD)
  user_errors = response.dig(USER_ERRORS_FIELD)

  if user_errors
    @ctx.puts(error_message(@project.title))
    error_files = erroneous_files(user_errors)
    print_items(error_files)
  else
    @ctx.puts(success_message(@project.title))
    print_items({}.freeze)
    ::Extension::Tasks::Converters::VersionConverter.from_hash(@ctx, response.dig(VERSION_FIELD))
  end

  @syncer_mutex.synchronize do
    @syncer.pending_operations.clear
    @syncer.latest_sync = Time.now
  end

ensure
  job_in_progress!(false)
end