问题描述
如果我有一个 rake 会调用多个其他 rake.
If I have one rake which invokes multiple other rakes.
一旦我启动了父级佣金
rake myapp:main
然后在 rake 中完成的调用将为每个任务加载环境或者它只是在运行 rake myapp:main
时完成的一次活动?
Then invokes done within the rake would load environment for each task or its just one time activity done while running rake myapp:main
?
namespace :myapp do
desc "Main Run"
task :main => :environment do
Rake::Task['myapp:task1'].invoke
Rake::Task['myapp:task2'].invoke
end
task :task1 => :environment do
# Does the first task
end
task :task2 => :environment do
# Does the second task
end
end
推荐答案
在@Shadwell 的回答中添加详细信息..
Adding details to @Shadwell's answer..
=>:environment 指定 :environment
任务(由 rails 定义)是您的任务的依赖项,并且必须在您的任务之前调用.
The => :environment
is specifying that the :environment
task (defined by rails) is a dependency of your tasks and must be invoked before your tasks are.
你可以在这里看到:environment
任务的定义
You can see :environment
task's definition here
Rake 会跟踪哪些任务已经被调用,当它到达一个已经被调用的依赖项时,它知道它可以跳过它.
Rake keeps track of which tasks have invoked though and when it reaches a dependency that has already been invoked it knows it can skip it.
# Invoke all the prerequisites of a task.
def invoke_prerequisites(task_args, invocation_chain) # :nodoc:
if application.options.always_multitask
invoke_prerequisites_concurrently(task_args, invocation_chain)
else
prerequisite_tasks.each { |p|
prereq_args = task_args.new_scope(p.arg_names)
p.invoke_with_call_chain(prereq_args, invocation_chain)
}
end
end
Rake 维护一个实例变量 @already_invoked
以了解任务是否已经被调用.同样可以在下面的方法中看到
Rake maintains an intance variable @already_invoked
to know if a task has already been called. The same can be seen in the below method
def invoke_with_call_chain(task_args, invocation_chain) # :nodoc:
new_chain = InvocationChain.append(self, invocation_chain)
@lock.synchronize do
if application.options.trace
application.trace "** Invoke #{name} #{format_trace_flags}"
end
return if @already_invoked
@already_invoked = true
invoke_prerequisites(task_args, new_chain)
execute(task_args) if needed?
end
rescue Exception => ex
add_chain_to(ex, new_chain)
raise ex
end
这篇关于是否从父 rake 加载环境中为每个任务调用多个任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!