Module: Fasten

Defined in:
lib/fasten.rb,
lib/fasten/task.rb,
lib/fasten/runner.rb,
lib/fasten/worker.rb,
lib/fasten/version.rb,
lib/fasten/defaults.rb,
lib/fasten/ui/curses.rb,
lib/fasten/support/ui.rb,
lib/fasten/ui/console.rb,
lib/fasten/support/yaml.rb,
lib/fasten/task_manager.rb,
lib/fasten/support/state.rb,
lib/fasten/support/stats.rb,
lib/fasten/timeout_queue.rb,
lib/fasten/support/logger.rb,
lib/fasten/std_thread_proxy.rb,
lib/fasten/support/fork_worker.rb,
lib/fasten/support/thread_worker.rb

Defined Under Namespace

Modules: Support, UI Classes: Runner, StdThreadProxy, Task, TaskManager, TimeoutQueue, Worker, WorkerError

Constant Summary collapse

VERSION =
'0.18.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loggerObject

Returns the value of attribute logger.



7
8
9
# File 'lib/fasten/support/logger.rb', line 7

def logger
  @logger
end

Class Method Details

.cleanupObject



29
30
31
# File 'lib/fasten.rb', line 29

def cleanup
  @runner = nil
end

.default_developerObject



43
44
45
# File 'lib/fasten/defaults.rb', line 43

def default_developer
  $stdin.tty? && $stdout.tty?
end

.default_fasten_dirObject



25
26
27
# File 'lib/fasten/defaults.rb', line 25

def default_fasten_dir
  'fasten'
end

.default_jobsObject



17
18
19
# File 'lib/fasten/defaults.rb', line 17

def default_jobs
  Parallel.physical_processor_count
end

.default_nameObject



5
6
7
# File 'lib/fasten/defaults.rb', line 5

def default_name
  File.basename(Dir.getwd)
end

.default_priorityObject



47
48
49
# File 'lib/fasten/defaults.rb', line 47

def default_priority
  :dependants
end

.default_statsObject



9
10
11
# File 'lib/fasten/defaults.rb', line 9

def default_stats
  true
end

.default_summaryObject



13
14
15
# File 'lib/fasten/defaults.rb', line 13

def default_summary
  false
end

.default_ui_modeObject



33
34
35
36
37
38
39
40
41
# File 'lib/fasten/defaults.rb', line 33

def default_ui_mode
  return @default_ui_mode if defined? @default_ui_mode

  require 'fasten/ui/curses'

  @default_ui_mode = $stdin.tty? && $stdout.tty? ? :curses : :console
rescue StandardError, LoadError
  @default_ui_mode = :console
end

.default_use_threadsObject



29
30
31
# File 'lib/fasten/defaults.rb', line 29

def default_use_threads
  !OS.posix?
end

.default_worker_classObject



21
22
23
# File 'lib/fasten/defaults.rb', line 21

def default_worker_class
  Worker
end

.invokeObject



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/fasten.rb', line 111

def invoke
  opt_parser.parse!

  @options[:targets] = ARGV.to_a

  runner **@options
  @load_path = Dir['fasten/*_fasten.rb'] if @load_path.empty?
  load_fasten @load_path

  show_help 1 if runner.tasks.empty?

  runner.perform
end

.load_fasten(args) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fasten.rb', line 41

def load_fasten(args)
  args.each do |path|
    if File.directory? path
      items = Dir["#{path}/*_fasten.rb"]
      items.each do |item|
        puts "Fasten: loading #{item}"
        load item
      end
    elsif File.file? path
      puts "Fasten: loading #{path}"
      load path
    else
      warn "Fasten: file/folder not found: #{path}"
      exit 1
    end
  end
end

.map(list, **options, &block) ⇒ Object



21
22
23
# File 'lib/fasten.rb', line 21

def map(list, **options, &block)
  runner(**options).map(list, &block)
end

.opt_parserObject

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/fasten.rb', line 59

def opt_parser # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  return @opt_parser if defined? @opt_parser

  @options = { developer: false }
  @load_path = []

  @opt_parser = OptionParser.new do |opts| # rubocop:disable Metrics/BlockLength
    opts.banner = "Usage: #{$PROGRAM_NAME} [options] [targets]"
    opts.separator ''
    opts.separator 'Examples:'
    opts.separator '    fasten              # load and run all task from fasten/*_fasten.rb'
    opts.separator '    fasten -f tasks.rb  # load task from ruby script'
    opts.separator '    fasten -y tasks.yml # load task from yaml file'
    opts.separator ''
    opts.separator 'Options:'

    opts.on '-n NAME', '--name NAME', String, "Change name of this runner (default: #{default_name} from current directory)" do |name|
      @options[:name] = name
    end
    opts.on '-f PATH', '--file PATH', String, 'File or folder with ruby code' do |path|
      @load_path << path
    end
    opts.on '-j JOBS', '--jobs JOBS', Numeric, "Maximum number of tasks to execute in parallel (default: #{default_jobs} on this machine)" do |jobs|
      @options[:jobs] = jobs
    end
    opts.on '-s', '--[no-]summary', TrueClass, 'Display summary at the end of execution' do |boolean|
      @options[:summary] = boolean
    end
    opts.on '--ui=UI', String, "Type of UI: curses, console. (default: #{default_ui_mode} on this machine)" do |ui_mode|
      @options[:ui_mode] = ui_mode
    end
    opts.on '-t', '--threads', "Use threads based jobs for parallel execution#{default_use_threads && ' (default on this machine)' || nil}" do
      @options[:use_threads] = true
    end
    opts.on '-p', '--processes', "Use process based jobs for parallel execution#{!default_use_threads && ' (default on this machine)' || nil}" do
      @options[:use_threads] = false
    end
    opts.on '-v', '--version', 'Display version info' do
      puts Fasten::VERSION
      exit 0
    end
    opts.on_tail '-h', '--help', 'Shows this help' do
      show_help
    end
  end
end

.reconfigure(**options) ⇒ Object



33
34
35
# File 'lib/fasten.rb', line 33

def reconfigure(**options)
  runner.reconfigure(**options)
end

.register(**options, &block) ⇒ Object



37
38
39
# File 'lib/fasten.rb', line 37

def register(**options, &block)
  runner(**options).register(&block)
end

.runner(**options) ⇒ Object



25
26
27
# File 'lib/fasten.rb', line 25

def runner(**options)
  @runner ||= Fasten::Runner.new(**options)
end

.runner_from_yaml(path, **options) ⇒ Object



14
15
16
17
18
19
# File 'lib/fasten.rb', line 14

def runner_from_yaml(path, **options)
  runner = Fasten::Runner.new(**options)
  runner.load_yaml(path)

  runner
end

.show_help(exit_code = 0) ⇒ Object



106
107
108
109
# File 'lib/fasten.rb', line 106

def show_help(exit_code = 0)
  puts opt_parser
  exit exit_code
end