# /lib daemon folder: how do i add a module into the daemon?

**URL:** <https://www.ruby-forum.com/t/lib-daemon-folder-how-do-i-add-a-module-into-the-daemon/164903>\
**Category:** Rails\
**Created:** [April 7, 2009, 8:53am UTC](https://www.ruby-forum.com/t/lib-daemon-folder-how-do-i-add-a-module-into-the-daemon/164903 "2009-04-07T08:53:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tyanlion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@tyanlion](https://www.ruby-forum.com/u/tyanlion)\
**Post date:** [April 7, 2009, 8:53am UTC](https://www.ruby-forum.com/t/lib-daemon-folder-how-do-i-add-a-module-into-the-daemon/164903/1 "2009-04-07T08:53:26Z")

</div>

I am using the daemons plugin.

I have just one problem how do i add modules from the /lib folder to  
the /lib/daemons/daemon.rb file

eg. /lib/basic\_functions.rb

this is the file i want to load in daemon.rb

contents daemon.rb:

#!/usr/bin/env ruby

# You might want to change this

ENV[“RAILS\_ENV”] ||= “production”

require File.dirname( **FILE** ) + “/…/…/config/environment”  
#require File.dirname( **FILE** ) + “/…/…/lib/basic\_functions.rb”(this  
didn’t work)  
#require ‘…/basic\_functions.rb’ (this didn’t work)

$running = true  
Signal.trap(“TERM”) do  
$running = false  
end

while($running) do

# Replace this with your code

get\_current\_time  
ActiveRecord::Base.logger.info “This daemons is still running at #  
{@current\_time}.\n”  
sleep 5

end

contents of basic\_functions

module BasicFunctions

def get\_current\_time  
#@current\_time = Time.now.beginning\_of\_month + 27.days  
#@current\_time = Time.now.beginning\_of\_month + 13.days  
@current\_time = Time.now  
end

def get\_time\_period  
get\_current\_time  
@absmid = @current\_time.beginning\_of\_month + 14.days  
@midperiod = @absmid - 2.days  
if @current\_time \>= @absmid  
@begin\_bid\_time = @absmid  
@end\_bid\_time = @current\_time.next\_month.beginning\_of\_month -  
2.days  
@ad\_window\_begin = @current\_time.next\_month.beginning\_of\_month  
@ad\_window\_end = @current\_time.next\_month.beginning\_of\_month +  
15.days  
else  
@begin\_bid\_time = @current\_time.beginning\_of\_month  
@end\_bid\_time = @midperiod  
@ad\_window\_begin = @absmid  
@ad\_window\_end = @current\_time.next\_month.beginning\_of\_month  
end  
end

end

---

<div class="post-metadata">

**Author:** ![tyanlion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@tyanlion](https://www.ruby-forum.com/u/tyanlion)\
**Post date:** [April 7, 2009, 9:47am UTC](https://www.ruby-forum.com/t/lib-daemon-folder-how-do-i-add-a-module-into-the-daemon/164903/2 "2009-04-07T09:47:22Z")

</div>

On Apr 7, 7:52 am, tyliong [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> #!/usr/bin/env ruby
> 
> # You might want to change this
> 
> ENV[“RAILS\_ENV”] ||= “production”
> 
> require File.dirname( **FILE** ) + “/…/…/config/environment”  
> #require File.dirname( **FILE** ) + “/…/…/lib/basic\_functions.rb”(this  
> didn’t work)  
> #require ‘…/basic\_functions.rb’ (this didn’t work)

requiring that file just means that the module BasicFunctions is now  
loaded. you haven’t done anything with it yet (include BasicFunctions  
somewhere might be a good starting point)

Fred

---

<div class="post-metadata">

**Author:** ![tyanlion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@tyanlion](https://www.ruby-forum.com/u/tyanlion)\
**Post date:** [April 7, 2009, 9:58am UTC](https://www.ruby-forum.com/t/lib-daemon-folder-how-do-i-add-a-module-into-the-daemon/164903/3 "2009-04-07T09:58:32Z")

</div>

On Apr 7, 9:03 am, “Tan YL” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks Fred.
> 
> I tried what u said but I wasn’t able to get it going. This time I didn’t  
> require the file I dumped the module inside the daemons.rb file but I just  
> couldn’t get it to work. I know it must be something to do with the syntax I  
> used.

where the module is on disk is somewhat irrelevant.

> include Basic\_functions

That must match the name of the module

Fred

---

<div class="post-metadata">

**Author:** ![tyanlion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@tyanlion](https://www.ruby-forum.com/u/tyanlion)\
**Post date:** [April 7, 2009, 9:52am UTC](https://www.ruby-forum.com/t/lib-daemon-folder-how-do-i-add-a-module-into-the-daemon/164903/4 "2009-04-07T09:52:05Z")

</div>

Thanks Fred.

I tried what u said but I wasn’t able to get it going. This time I  
didn’t  
require the file I dumped the module inside the daemons.rb file but I  
just  
couldn’t get it to work. I know it must be something to do with the  
syntax I  
used.

#!/usr/bin/env ruby

# You might want to change this

ENV[“RAILS\_ENV”] ||= “development”  
#require ‘basic\_functions.rb’

require File.dirname( **FILE** ) + “/…/…/config/environment”  
module BasicFunctions

def get\_current\_time  
#@current\_time = Time.now.beginning\_of\_month + 27.days  
#@current\_time = Time.now.beginning\_of\_month + 13.days  
@current\_time = Time.now  
end

def get\_time\_period  
get\_current\_time  
@absmid = @current\_time.beginning\_of\_month + 14.days  
@midperiod = @absmid - 2.days  
if @current\_time \>= @absmid  
@begin\_bid\_time = @absmid  
@end\_bid\_time =  
@current\_time.next\_month.beginning\_of\_month - 2.days  
@ad\_window\_begin =  
@current\_time.next\_month.beginning\_of\_month  
@ad\_window\_end =  
@current\_time.next\_month.beginning\_of\_month + 15.days  
else  
@begin\_bid\_time =  
@current\_time.beginning\_of\_month  
@end\_bid\_time = @midperiod  
@ad\_window\_begin = @absmid  
@ad\_window\_end =  
@current\_time.next\_month.beginning\_of\_month  
end  
end

end

include Basic\_functions  
$running = true  
Signal.trap(“TERM”) do  
$running = false  
end

while($running) do

# Replace this with your code

get\_current\_time  
ActiveRecord::Base.logger.info “This daemons is still running at  
#{@current\_time}.\n”  
sleep 5

end

---

<div class="post-metadata">

**Author:** ![tyanlion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@tyanlion](https://www.ruby-forum.com/u/tyanlion)\
**Post date:** [April 7, 2009, 10:03am UTC](https://www.ruby-forum.com/t/lib-daemon-folder-how-do-i-add-a-module-into-the-daemon/164903/5 "2009-04-07T10:03:48Z")

</div>

Thank you fred very very much.
