Module: Cosmos
- Defined in:
- lib/cosmos/logs.rb,
lib/cosmos/system.rb,
lib/cosmos/api/api.rb,
lib/cosmos/version.rb,
lib/cosmos/io/stderr.rb,
lib/cosmos/io/stdout.rb,
lib/cosmos/top_level.rb,
lib/cosmos/utilities.rb,
lib/cosmos/interfaces.rb,
lib/cosmos/processors.rb,
lib/cosmos/api/cmd_api.rb,
lib/cosmos/api/tlm_api.rb,
lib/cosmos/conversions.rb,
lib/cosmos/io/json_drb.rb,
lib/cosmos/io/json_rpc.rb,
lib/cosmos/win32/excel.rb,
lib/cosmos/win32/win32.rb,
lib/cosmos/models/model.rb,
lib/cosmos/script/suite.rb,
lib/cosmos/topics/topic.rb,
lib/cosmos/utilities/s3.rb,
lib/cosmos/bridge/bridge.rb,
lib/cosmos/io/raw_logger.rb,
lib/cosmos/script/limits.rb,
lib/cosmos/script/script.rb,
lib/cosmos/system/system.rb,
lib/cosmos/system/target.rb,
lib/cosmos/utilities/crc.rb,
lib/cosmos/utilities/csv.rb,
lib/cosmos/api/config_api.rb,
lib/cosmos/api/limits_api.rb,
lib/cosmos/api/router_api.rb,
lib/cosmos/api/target_api.rb,
lib/cosmos/packets/limits.rb,
lib/cosmos/packets/packet.rb,
lib/cosmos/script/extract.rb,
lib/cosmos/script/storage.rb,
lib/cosmos/streams/stream.rb,
lib/cosmos/logs/log_writer.rb,
lib/cosmos/script/calendar.rb,
lib/cosmos/script/commands.rb,
lib/cosmos/utilities/store.rb,
lib/cosmos/api/settings_api.rb,
lib/cosmos/io/buffered_file.rb,
lib/cosmos/io/json_drb_rack.rb,
lib/cosmos/io/serial_driver.rb,
lib/cosmos/models/cvt_model.rb,
lib/cosmos/models/gem_model.rb,
lib/cosmos/packets/commands.rb,
lib/cosmos/utilities/logger.rb,
lib/cosmos/utilities/metric.rb,
lib/cosmos/win32/win32_main.rb,
lib/cosmos/api/interface_api.rb,
lib/cosmos/io/io_multiplexer.rb,
lib/cosmos/models/auth_model.rb,
lib/cosmos/models/info_model.rb,
lib/cosmos/models/ping_model.rb,
lib/cosmos/models/tool_model.rb,
lib/cosmos/packets/structure.rb,
lib/cosmos/packets/telemetry.rb,
lib/cosmos/script/api_shared.rb,
lib/cosmos/script/exceptions.rb,
lib/cosmos/utilities/sleeper.rb,
lib/cosmos/api/authorized_api.rb,
lib/cosmos/ccsds/ccsds_packet.rb,
lib/cosmos/ccsds/ccsds_parser.rb,
lib/cosmos/io/json_api_object.rb,
lib/cosmos/io/json_drb_object.rb,
lib/cosmos/io/raw_logger_pair.rb,
lib/cosmos/models/scope_model.rb,
lib/cosmos/operators/operator.rb,
lib/cosmos/models/metric_model.rb,
lib/cosmos/models/plugin_model.rb,
lib/cosmos/models/router_model.rb,
lib/cosmos/models/target_model.rb,
lib/cosmos/models/widget_model.rb,
lib/cosmos/packets/json_packet.rb,
lib/cosmos/packets/packet_item.rb,
lib/cosmos/script/suite_runner.rb,
lib/cosmos/topics/router_topic.rb,
lib/cosmos/bridge/bridge_config.rb,
lib/cosmos/config/config_parser.rb,
lib/cosmos/interfaces/interface.rb,
lib/cosmos/logs/text_log_writer.rb,
lib/cosmos/models/reducer_model.rb,
lib/cosmos/models/trigger_model.rb,
lib/cosmos/processors/processor.rb,
lib/cosmos/script/suite_results.rb,
lib/cosmos/system/system_config.rb,
lib/cosmos/topics/command_topic.rb,
lib/cosmos/utilities/quaternion.rb,
lib/cosmos/models/activity_model.rb,
lib/cosmos/models/metadata_model.rb,
lib/cosmos/models/reaction_model.rb,
lib/cosmos/models/timeline_model.rb,
lib/cosmos/packets/packet_config.rb,
lib/cosmos/streams/serial_stream.rb,
lib/cosmos/topics/calendar_topic.rb,
lib/cosmos/topics/timeline_topic.rb,
lib/cosmos/utilities/message_log.rb,
lib/cosmos/conversions/conversion.rb,
lib/cosmos/io/posix_serial_driver.rb,
lib/cosmos/io/win32_serial_driver.rb,
lib/cosmos/logs/packet_log_reader.rb,
lib/cosmos/logs/packet_log_writer.rb,
lib/cosmos/models/interface_model.rb,
lib/cosmos/models/narrative_model.rb,
lib/cosmos/packets/structure_item.rb,
lib/cosmos/tools/test_runner/test.rb,
lib/cosmos/topics/autonomic_topic.rb,
lib/cosmos/topics/interface_topic.rb,
lib/cosmos/topics/telemetry_topic.rb,
lib/cosmos/packets/binary_accessor.rb,
lib/cosmos/packets/limits_response.rb,
lib/cosmos/interfaces/udp_interface.rb,
lib/cosmos/models/environment_model.rb,
lib/cosmos/utilities/authentication.rb,
lib/cosmos/utilities/store_autoload.rb,
lib/cosmos/config/meta_config_parser.rb,
lib/cosmos/interfaces/linc_interface.rb,
lib/cosmos/logs/packet_log_constants.rb,
lib/cosmos/models/microservice_model.rb,
lib/cosmos/models/notification_model.rb,
lib/cosmos/tools/table_manager/table.rb,
lib/cosmos/topics/limits_event_topic.rb,
lib/cosmos/utilities/process_manager.rb,
lib/cosmos/microservices/microservice.rb,
lib/cosmos/models/router_status_model.rb,
lib/cosmos/models/trigger_group_model.rb,
lib/cosmos/packets/packet_item_limits.rb,
lib/cosmos/topics/command_decom_topic.rb,
lib/cosmos/topics/notifications_topic.rb,
lib/cosmos/utilities/simulated_target.rb,
lib/cosmos/bridge/bridge_router_thread.rb,
lib/cosmos/interfaces/serial_interface.rb,
lib/cosmos/interfaces/stream_interface.rb,
lib/cosmos/models/process_status_model.rb,
lib/cosmos/packets/parsers/xtce_parser.rb,
lib/cosmos/streams/tcpip_client_stream.rb,
lib/cosmos/streams/tcpip_socket_stream.rb,
lib/cosmos/packets/parsers/state_parser.rb,
lib/cosmos/topics/telemetry_decom_topic.rb,
lib/cosmos/interfaces/protocols/protocol.rb,
lib/cosmos/models/interface_status_model.rb,
lib/cosmos/packets/parsers/limits_parser.rb,
lib/cosmos/packets/parsers/packet_parser.rb,
lib/cosmos/bridge/bridge_interface_thread.rb,
lib/cosmos/conversions/generic_conversion.rb,
lib/cosmos/microservices/log_microservice.rb,
lib/cosmos/packets/parsers/xtce_converter.rb,
lib/cosmos/processors/watermark_processor.rb,
lib/cosmos/tools/table_manager/table_item.rb,
lib/cosmos/operators/microservice_operator.rb,
lib/cosmos/processors/statistics_processor.rb,
lib/cosmos/conversions/processor_conversion.rb,
lib/cosmos/conversions/unix_time_conversion.rb,
lib/cosmos/microservices/decom_microservice.rb,
lib/cosmos/models/microservice_status_model.rb,
lib/cosmos/packets/parsers/processor_parser.rb,
lib/cosmos/tools/table_manager/table_config.rb,
lib/cosmos/tools/table_manager/table_parser.rb,
lib/cosmos/conversions/polynomial_conversion.rb,
lib/cosmos/interfaces/protocols/crc_protocol.rb,
lib/cosmos/interfaces/tcpip_client_interface.rb,
lib/cosmos/interfaces/tcpip_server_interface.rb,
lib/cosmos/microservices/plugin_microservice.rb,
lib/cosmos/microservices/router_microservice.rb,
lib/cosmos/microservices/cleanup_microservice.rb,
lib/cosmos/microservices/reducer_microservice.rb,
lib/cosmos/packets/parsers/packet_item_parser.rb,
lib/cosmos/interfaces/protocols/burst_protocol.rb,
lib/cosmos/interfaces/protocols/fixed_protocol.rb,
lib/cosmos/microservices/reaction_microservice.rb,
lib/cosmos/microservices/text_log_microservice.rb,
lib/cosmos/microservices/timeline_microservice.rb,
lib/cosmos/interfaces/protocols/length_protocol.rb,
lib/cosmos/microservices/interface_microservice.rb,
lib/cosmos/packets/parsers/format_string_parser.rb,
lib/cosmos/conversions/received_count_conversion.rb,
lib/cosmos/interfaces/simulated_target_interface.rb,
lib/cosmos/tools/cmd_tlm_server/interface_thread.rb,
lib/cosmos/tools/table_manager/table_item_parser.rb,
lib/cosmos/interfaces/protocols/override_protocol.rb,
lib/cosmos/interfaces/protocols/template_protocol.rb,
lib/cosmos/packets/parsers/limits_response_parser.rb,
lib/cosmos/tools/table_manager/table_manager_core.rb,
lib/cosmos/conversions/unix_time_seconds_conversion.rb,
lib/cosmos/interfaces/protocols/terminated_protocol.rb,
lib/cosmos/microservices/trigger_group_microservice.rb,
lib/cosmos/conversions/packet_time_seconds_conversion.rb,
lib/cosmos/conversions/unix_time_formatted_conversion.rb,
lib/cosmos/tools/cmd_tlm_server/cmd_tlm_server_config.rb,
lib/cosmos/conversions/segmented_polynomial_conversion.rb,
lib/cosmos/interfaces/protocols/ignore_packet_protocol.rb,
lib/cosmos/interfaces/protocols/preidentified_protocol.rb,
lib/cosmos/conversions/packet_time_formatted_conversion.rb,
lib/cosmos/conversions/received_time_seconds_conversion.rb,
lib/cosmos/conversions/received_time_formatted_conversion.rb,
ext/cosmos/ext/crc/crc.c,
ext/cosmos/ext/structure/structure.c,
ext/cosmos/ext/telemetry/telemetry.c,
ext/cosmos/ext/buffered_file/buffered_file.c,
ext/cosmos/ext/config_parser/config_parser.c,
ext/cosmos/ext/tabbed_plots_config/tabbed_plots_config.c,
ext/cosmos/ext/polynomial_conversion/polynomial_conversion.c,
lib/cosmos/io/udp_sockets.rb
Overview
Copyright 2022 Ball Aerospace & Technologies Corp. All Rights Reserved.
This program is free software; you can modify and/or redistribute it under the terms of the GNU Affero General Public License as published by the Free Software Foundation; version 3 with attribution addendums as found in the LICENSE.txt
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
This program may also be used under the terms of a commercial or enterprise edition license of COSMOS if purchased from the copyright holder
Defined Under Namespace
Modules: Api, ApiShared, AuthorizedApi, ExcelColumnConstants, Extract, PacketLogConstants, Script, Version Classes: ActivityError, ActivityInputError, ActivityModel, ActivityOverlapError, AuthModel, AutonomicTopic, BinaryAccessor, Bridge, BridgeConfig, BridgeInterfaceThread, BridgeRouterThread, BufferedFile, BurstProtocol, CSV, CalendarTopic, CcsdsPacket, CcsdsParser, CheckError, CleanupMicroservice, CmdTlmServerConfig, CommandDecomTopic, CommandTopic, Commands, ConfigParser, Conversion, CosmosAuthentication, CosmosAuthenticationError, CosmosAuthenticationRetryableError, CosmosKeycloakAuthentication, Crc, Crc16, Crc32, Crc64, CrcProtocol, CvtModel, DecomMicroservice, EnvironmentError, EnvironmentModel, ExcelSpreadsheet, FatalError, FixedProtocol, FormatStringParser, GemModel, GenericConversion, Group, IgnorePacketProtocol, InfoModel, Interface, InterfaceCmdHandlerThread, InterfaceMicroservice, InterfaceModel, InterfaceStatusModel, InterfaceThread, InterfaceTopic, IoMultiplexer, JsonApiError, JsonApiObject, JsonDRb, JsonDRbError, JsonDRbObject, JsonDRbUnknownError, JsonDrbRack, JsonPacket, JsonRpc, JsonRpcError, JsonRpcErrorResponse, JsonRpcRequest, JsonRpcResponse, JsonRpcSuccessResponse, LengthProtocol, Limits, LimitsEventTopic, LimitsParser, LimitsResponse, LimitsResponseParser, LincHandshake, LincHandshakeCommand, LincInterface, LogMicroservice, LogWriter, Logger, MessageLog, MetaConfigParser, MetadataError, MetadataInputError, MetadataModel, MetadataOverlapError, Metric, MetricModel, Microservice, MicroserviceModel, MicroserviceOperator, MicroserviceStatusModel, Model, NarrativeError, NarrativeInputError, NarrativeModel, NarrativeOverlapError, NotificationModel, NotificationsTopic, Operator, OperatorProcess, OverrideProtocol, Packet, PacketBase, PacketConfig, PacketItem, PacketItemLimits, PacketItemParser, PacketLogReader, PacketLogWriter, PacketParser, PacketTimeFormattedConversion, PacketTimeSecondsConversion, PingModel, PluginMicroservice, PluginModel, PolynomialConversion, PosixSerialDriver, PreidentifiedProtocol, ProcessManager, ProcessManagerProcess, ProcessStatusModel, Processor, ProcessorConversion, ProcessorParser, Protocol, Quaternion, QueueBase, RawLogger, RawLoggerPair, ReactionBase, ReactionError, ReactionInputError, ReactionMicroservice, ReactionModel, ReactionShare, ReactionSnoozeManager, ReactionWorker, ReceivedCountConversion, ReceivedTimeFormattedConversion, ReceivedTimeSecondsConversion, ReducerMicroservice, ReducerModel, RouterMicroservice, RouterModel, RouterStatusModel, RouterTlmHandlerThread, RouterTopic, S3Utilities, Schedule, ScopeModel, ScriptResult, ScriptStatus, SegmentedPolynomialConversion, SerialDriver, SerialInterface, SerialStream, ServerProxy, SimulatedTarget, SimulatedTargetInterface, SkipScript, SkipTestCase, Sleeper, SnoozeBase, StateParser, StatisticsProcessor, Stderr, Stdout, StopScript, Store, Stream, StreamInterface, Structure, StructureItem, Suite, SuiteResults, SuiteRunner, System, SystemConfig, TabbedPlotsConfig, Table, TableConfig, TableItem, TableItemParser, TableManagerCore, TableParser, Target, TargetModel, TcpipClientInterface, TcpipClientStream, TcpipServerInterface, TcpipSocketStream, Telemetry, TelemetryDecomTopic, TelemetryTopic, TemplateProtocol, TerminatedProtocol, Test, TestResult, TestStatus, TestSuite, TextLogMicroservice, TextLogWriter, TimelineError, TimelineInputError, TimelineManager, TimelineMicroservice, TimelineModel, TimelineTopic, TimelineWorker, ToolModel, Topic, TriggerBase, TriggerError, TriggerGroupError, TriggerGroupInputError, TriggerGroupManager, TriggerGroupMicroservice, TriggerGroupModel, TriggerGroupShare, TriggerGroupWorker, TriggerInputError, TriggerLoopError, TriggerModel, UdpInterface, UdpReadSocket, UdpReadWriteSocket, UdpWriteSocket, UnassignedSuite, UnixTimeConversion, UnixTimeFormattedConversion, UnixTimeSecondsConversion, WatermarkProcessor, WidgetModel, Win32, Win32API, Win32SerialDriver, XtceConverter, XtceParser
Constant Summary collapse
- VERSION =
'5.0.3'
- GEM_VERSION =
'5.0.3'
- BASE_PWD =
Dir.pwd
- COSMOS_MUTEX =
Global mutex for the Cosmos module
Mutex.new
- PATH =
Path to COSMOS Gem based on location of top_level.rb
File.(File.join(File.dirname(__FILE__), '../..'))
- COSMOS_MARSHAL_HEADER =
Header to put on all marshal files created by COSMOS
"ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE} patchlevel #{RUBY_PATCHLEVEL}) [#{RUBY_PLATFORM}] COSMOS #{COSMOS_VERSION}"
- POINTER_TYPE =
Fiddle::SIZEOF_VOIDP == Fiddle::SIZEOF_LONG_LONG ? 'q*' : 'l!*'
Class Method Summary collapse
-
.add_to_search_path(path, front = true) ⇒ Object
Adds a path to the global Ruby search path.
-
.catch_fatal_exception ⇒ Object
Catch fatal exceptions within the block This is intended to catch exceptions before the GUI is available.
-
.close_socket(socket) ⇒ Object
Close a socket in a manner that ensures that any reads blocked in select will unblock across platforms.
-
.create_log_file(filename, log_dir = nil) {|file| ... } ⇒ String|nil
Opens a timestamped log file for writing.
-
.disable_warnings ⇒ Object
Disables the Ruby interpreter warnings such as when redefining a constant.
-
.handle_critical_exception(error, try_gui = true) ⇒ Object
CriticalErrors are errors that need to be brought to a user’s attention but do not cause an exit.
-
.handle_fatal_exception(error, try_gui = true) ⇒ Object
Write a message to the Logger, write an exception file, and popup a GUI window if try_gui.
-
.hash_files(filenames, additional_data = nil, hashing_algorithm = 'SHA256') ⇒ Digest::<algorithm>
Runs a hash algorithm over one or more files and returns the Digest object.
-
.kill_thread(owner, thread, graceful_timeout = 1, timeout_interval = 0.01, hard_timeout = 1) ⇒ Object
Attempt to gracefully kill a thread.
-
.marshal_dump(marshal_filename, obj) ⇒ Object
Creates a marshal file by serializing the given obj.
-
.marshal_load(marshal_filename) ⇒ Object
Loads the marshal file back into a Ruby object.
- .open_in_web_browser(filename) ⇒ Object
-
.require_class(class_name_or_class_filename, log_error = true) ⇒ Object
Require the class represented by the filename.
-
.require_file(filename, log_error = true) ⇒ Object
Requires a file with a standard error message if it fails.
-
.run_process(command) ⇒ Object
Executes the command in a new Ruby Thread.
-
.run_process_check_output(command) ⇒ Object
Executes the command in a new Ruby Thread.
-
.safe_thread(name, retry_attempts = 0) ⇒ Object
Creates a Ruby Thread to run the given block.
-
.set_working_dir(working_dir, &block) ⇒ Object
Temporarily set the working directory during a block Working directory is global, so this can make other threads wait Ruby Dir.chdir with block always throws an error if multiple threads call Dir.chdir.
-
.set_working_dir_internal(working_dir) ⇒ Object
Private helper method.
-
.write_exception_file(exception, filename = 'exception', log_dir = nil) ⇒ String|nil
Writes a log file with information about the current configuration including the Ruby version, Cosmos version, whether you are on Windows, the COSMOS path, and the Ruby path along with the exception that is passed in.
-
.write_unexpected_file(text, filename = 'unexpected', log_dir = nil) ⇒ String|nil
Writes a log file with information about unexpected output.
Class Method Details
.add_to_search_path(path, front = true) ⇒ Object
Adds a path to the global Ruby search path
79 80 81 82 83 84 85 86 87 |
# File 'lib/cosmos/top_level.rb', line 79 def self.add_to_search_path(path, front = true) path = File.(path) $:.delete(path) if front $:.unshift(path) else # Back $: << path end end |
.catch_fatal_exception ⇒ Object
Catch fatal exceptions within the block This is intended to catch exceptions before the GUI is available
347 348 349 350 351 352 353 354 |
# File 'lib/cosmos/top_level.rb', line 347 def self.catch_fatal_exception yield rescue Exception => error unless error.class == SystemExit or error.class == Interrupt Logger.level = Logger::FATAL Cosmos.handle_fatal_exception(error, false) end end |
.close_socket(socket) ⇒ Object
Close a socket in a manner that ensures that any reads blocked in select will unblock across platforms
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 |
# File 'lib/cosmos/top_level.rb', line 546 def self.close_socket(socket) if socket # Calling shutdown and then sleep seems to be required # to get select to reliably unblock on linux begin socket.shutdown(:RDWR) sleep(0) rescue Exception # Oh well we tried end begin socket.close unless socket.closed? rescue Exception # Oh well we tried end end end |
.create_log_file(filename, log_dir = nil) {|file| ... } ⇒ String|nil
Opens a timestamped log file for writing. The opened file is yielded back to the block.
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/cosmos/top_level.rb', line 225 def self.create_log_file(filename, log_dir = nil) log_file = nil begin # The following code goes inside a begin rescue because it reads the # system.txt configuration file. If this has an error we won't be able # to determine the log path but we still want to write the log. log_dir = System.instance.paths['LOGS'] unless log_dir # Make sure the log directory exists raise unless File.exist?(log_dir) rescue Exception log_dir = nil # Reset log dir since it failed above # First check for ./logs log_dir = './logs' if File.exist?('./logs') # Prefer ./outputs/logs if it exists log_dir = './outputs/logs' if File.exist?('./outputs/logs') # If all else fails just use the local directory log_dir = '.' unless log_dir end log_file = File.join(log_dir, File.([filename])) # Check for the log file existing. This could happen if this method gets # called more than once in the same second. if File.exist?(log_file) sleep 1.01 # Sleep before rebuilding the timestamp to get something unique log_file = File.join(log_dir, File.([filename])) end begin COSMOS_MUTEX.synchronize do file = File.open(log_file, 'w') yield file ensure file.close unless file.closed? File.chmod(0444, log_file) # Make file read only end rescue Exception # Ensure we always return end log_file = File.(log_file) return log_file end |
.disable_warnings ⇒ Object
Disables the Ruby interpreter warnings such as when redefining a constant
68 69 70 71 72 73 74 |
# File 'lib/cosmos/top_level.rb', line 68 def self.disable_warnings saved_verbose = $VERBOSE $VERBOSE = nil yield ensure $VERBOSE = saved_verbose end |
.handle_critical_exception(error, try_gui = true) ⇒ Object
CriticalErrors are errors that need to be brought to a user’s attention but do not cause an exit. A good example is if the packet log writer fails and can no longer write the log file. Write a message to the Logger, write an exception file, and popup a GUI window if try_gui. Ensure the GUI only comes up once so this method can be called over and over by failing code.
388 389 390 391 |
# File 'lib/cosmos/top_level.rb', line 388 def self.handle_critical_exception(error, try_gui = true) Logger.error "Critical Exception! #{error.formatted}" self.write_exception_file(error) end |
.handle_fatal_exception(error, try_gui = true) ⇒ Object
Write a message to the Logger, write an exception file, and popup a GUI window if try_gui. Finally ‘exit 1’ is called to end the calling program.
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
# File 'lib/cosmos/top_level.rb', line 361 def self.handle_fatal_exception(error, try_gui = true) unless error.class == SystemExit or error.class == Interrupt $cosmos_fatal_exception = error self.write_exception_file(error) Logger.level = Logger::FATAL Logger.fatal "Fatal Exception! Exiting..." Logger.fatal error.formatted if $stdout != STDOUT $stdout = STDOUT Logger.fatal "Fatal Exception! Exiting..." Logger.fatal error.formatted end sleep 1 # Allow the messages to be printed and then crash exit 1 else exit 0 end end |
.hash_files(filenames, additional_data = nil, hashing_algorithm = 'SHA256') ⇒ Digest::<algorithm>
Runs a hash algorithm over one or more files and returns the Digest object. Handles windows/unix new line differences but changes in whitespace will change the hash sum.
Usage:
digest = Cosmos.hash_files(files, additional_data, hashing_algorithm)
digest.digest # => the 16 bytes of digest
digest.hexdigest # => the formatted string in hex
201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/cosmos/top_level.rb', line 201 def self.hash_files(filenames, additional_data = nil, hashing_algorithm = 'SHA256') digest = Digest.const_get(hashing_algorithm).public_send('new') filenames.each do |filename| next if File.directory?(filename) # Read the file's data and add to the running hashing sum digest << File.read(filename) end digest << additional_data if additional_data digest end |
.kill_thread(owner, thread, graceful_timeout = 1, timeout_interval = 0.01, hard_timeout = 1) ⇒ Object
Attempt to gracefully kill a thread
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 |
# File 'lib/cosmos/top_level.rb', line 506 def self.kill_thread(owner, thread, graceful_timeout = 1, timeout_interval = 0.01, hard_timeout = 1) if thread if owner and owner.respond_to? :graceful_kill if Thread.current != thread owner.graceful_kill end_time = Time.now.sys + graceful_timeout while thread.alive? && ((end_time - Time.now.sys) > 0) sleep(timeout_interval) end else Logger.warn "Threads cannot graceful_kill themselves" end elsif owner Logger.info "Thread owner #{owner.class} does not support graceful_kill" end if thread.alive? # If the thread dies after alive? but before backtrace, bt will be nil. bt = thread.backtrace # Graceful failed msg = "Failed to gracefully kill thread:\n" msg << " Caller Backtrace:\n #{caller().join("\n ")}\n" msg << " \n Thread Backtrace:\n #{bt.join("\n ")}\n" if bt msg << "\n" Logger.warn msg thread.kill end_time = Time.now.sys + hard_timeout while thread.alive? && ((end_time - Time.now.sys) > 0) sleep(timeout_interval) end end if thread.alive? Logger.error "Failed to kill thread" end end end |
.marshal_dump(marshal_filename, obj) ⇒ Object
Creates a marshal file by serializing the given obj
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/cosmos/top_level.rb', line 93 def self.marshal_dump(marshal_filename, obj) File.open(marshal_filename, 'wb') do |file| file.write(COSMOS_MARSHAL_HEADER) file.write(Marshal.dump(obj)) end rescue Exception => exception begin File.delete(marshal_filename) rescue Exception # Oh well - we tried end if exception.class == TypeError and exception. =~ /Thread::Mutex/ original_backtrace = exception.backtrace exception = exception.exception("Mutex exists in a packet. Note: Packets must not be read during class initializers for Conversions, Limits Responses, etc.: #{exception}") exception.set_backtrace(original_backtrace) end self.handle_fatal_exception(exception) end |
.marshal_load(marshal_filename) ⇒ Object
Loads the marshal file back into a Ruby object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/cosmos/top_level.rb', line 115 def self.marshal_load(marshal_filename) cosmos_marshal_header = nil data = nil File.open(marshal_filename, 'rb') do |file| cosmos_marshal_header = file.read(COSMOS_MARSHAL_HEADER.length) data = file.read end if cosmos_marshal_header == COSMOS_MARSHAL_HEADER return Marshal.load(data) else Logger.warn "Marshal load failed with invalid marshal file: #{marshal_filename}" return nil end rescue Exception => exception if File.exist?(marshal_filename) Logger.error "Marshal load failed with exception: #{marshal_filename}\n#{exception.formatted}" else Logger.info "Marshal file does not exist: #{marshal_filename}" end # Try to delete the bad marshal file begin File.delete(marshal_filename) rescue Exception # Oh well - we tried end self.handle_fatal_exception(exception) if File.exist?(marshal_filename) return nil end |
.open_in_web_browser(filename) ⇒ Object
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 |
# File 'lib/cosmos/top_level.rb', line 456 def self.open_in_web_browser(filename) if filename if Kernel.is_windows? self.run_process("cmd /c \"start \"\" \"#{filename.gsub('/', '\\')}\"\"") elsif Kernel.is_mac? self.run_process("open -a Safari \"#{filename}\"") else which_firefox = `which firefox`.chomp if which_firefox =~ /Command not found/i or which_firefox =~ /no .* in/i raise "Firefox not found" else system_call = "#{which_firefox} \"#{filename}\"" end self.run_process(system_call) end end end |
.require_class(class_name_or_class_filename, log_error = true) ⇒ Object
Require the class represented by the filename. This uses the standard Ruby convention of having a single class per file where the class name is camel cased and filename is lowercase with underscores.
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
# File 'lib/cosmos/top_level.rb', line 425 def self.require_class(class_name_or_class_filename, log_error = true) if class_name_or_class_filename.downcase[-3..-1] == '.rb' or (class_name_or_class_filename[0] == class_name_or_class_filename[0].downcase) class_filename = class_name_or_class_filename class_name = class_filename.filename_to_class_name else class_name = class_name_or_class_filename class_filename = class_name.class_name_to_filename end return class_name.to_class if class_name.to_class and defined? class_name.to_class self.require_file(class_filename, log_error) klass = class_name.to_class raise "Ruby class #{class_name} not found" unless klass klass end |
.require_file(filename, log_error = true) ⇒ Object
Requires a file with a standard error message if it fails
446 447 448 449 450 451 452 453 |
# File 'lib/cosmos/top_level.rb', line 446 def self.require_file(filename, log_error = true) require filename rescue Exception => err msg = "Unable to require #{filename} due to #{err.}. "\ "Ensure #{filename} is in the COSMOS lib directory." Logger.error msg if log_error raise $!, msg, $!.backtrace end |
.run_process(command) ⇒ Object
Executes the command in a new Ruby Thread.
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/cosmos/top_level.rb', line 148 def self.run_process(command) thread = nil thread = Thread.new do system(command) end # Wait for the thread and process to start sleep 0.01 until !thread.status.nil? sleep 0.1 thread end |
.run_process_check_output(command) ⇒ Object
Executes the command in a new Ruby Thread. Will print the output if the process produces any output
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/cosmos/top_level.rb', line 163 def self.run_process_check_output(command) thread = nil thread = Thread.new do output, _ = Open3.capture2e(command) if !output.empty? # Ignore modalSession messages on Mac Mavericks new_output = '' output.each_line do |line| new_output << line if !/modalSession/.match?(line) end output = new_output if !output.empty? Logger.error output self.write_unexpected_file(output) end end end # Wait for the thread and process to start sleep 0.01 until !thread.status.nil? sleep 0.1 thread end |
.safe_thread(name, retry_attempts = 0) ⇒ Object
Creates a Ruby Thread to run the given block. Rescues any exceptions and retries the threads the given number of times before handling the thread death by calling handle_fatal_exception.
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
# File 'lib/cosmos/top_level.rb', line 400 def self.safe_thread(name, retry_attempts = 0) Thread.new do retry_count = 0 begin yield rescue => error Logger.error "#{name} thread unexpectedly died. Retries: #{retry_count} of #{retry_attempts}" Logger.error error.formatted retry_count += 1 if retry_count <= retry_attempts self.write_exception_file(error) retry end handle_fatal_exception(error) end end end |
.set_working_dir(working_dir, &block) ⇒ Object
Temporarily set the working directory during a block Working directory is global, so this can make other threads wait Ruby Dir.chdir with block always throws an error if multiple threads call Dir.chdir
479 480 481 482 483 484 485 486 487 |
# File 'lib/cosmos/top_level.rb', line 479 def self.set_working_dir(working_dir, &block) if $cosmos_chdir_mutex.owned? set_working_dir_internal(working_dir, &block) else $cosmos_chdir_mutex.synchronize do set_working_dir_internal(working_dir, &block) end end end |
.set_working_dir_internal(working_dir) ⇒ Object
Private helper method
490 491 492 493 494 495 496 497 498 |
# File 'lib/cosmos/top_level.rb', line 490 def self.set_working_dir_internal(working_dir) current_dir = Dir.pwd Dir.chdir(working_dir) begin yield ensure Dir.chdir(current_dir) end end |
.write_exception_file(exception, filename = 'exception', log_dir = nil) ⇒ String|nil
Writes a log file with information about the current configuration including the Ruby version, Cosmos version, whether you are on Windows, the COSMOS path, and the Ruby path along with the exception that is passed in.
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/cosmos/top_level.rb', line 279 def self.write_exception_file(exception, filename = 'exception', log_dir = nil) log_file = create_log_file(filename, log_dir) do |file| file.puts "Exception:" if exception file.puts exception.formatted file.puts else file.puts "No Exception Given" file.puts caller.join("\n") file.puts end file.puts "Caller Backtrace:" file.puts caller().join("\n") file.puts file.puts "Ruby Version: ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE} patchlevel #{RUBY_PATCHLEVEL}) [#{RUBY_PLATFORM}]" file.puts "Rubygems Version: #{Gem::VERSION}" file.puts "Cosmos Version: #{Cosmos::VERSION}" file.puts "Cosmos::PATH: #{Cosmos::PATH}" file.puts "" file.puts "Environment:" file.puts "RUBYOPT: #{ENV['RUBYOPT']}" file.puts "RUBYLIB: #{ENV['RUBYLIB']}" file.puts "GEM_PATH: #{ENV['GEM_PATH']}" file.puts "GEMRC: #{ENV['GEMRC']}" file.puts "RI_DEVKIT: #{ENV['RI_DEVKIT']}" file.puts "GEM_HOME: #{ENV['GEM_HOME']}" file.puts "PATH: #{ENV['PATH']}" file.puts "" file.puts "Ruby Path:\n #{$:.join("\n ")}\n\n" file.puts "Gems:" Gem.loaded_specs.values.map { |x| file.puts "#{x.name} #{x.version} #{x.platform}" } file.puts "" file.puts "All Threads Backtraces:" Thread.list.each do |thread| file.puts thread.backtrace.join("\n") file.puts end file.puts "" file.puts "" ensure file.close end return log_file end |
.write_unexpected_file(text, filename = 'unexpected', log_dir = nil) ⇒ String|nil
Writes a log file with information about unexpected output
335 336 337 338 339 340 341 342 343 |
# File 'lib/cosmos/top_level.rb', line 335 def self.write_unexpected_file(text, filename = 'unexpected', log_dir = nil) log_file = create_log_file(filename, log_dir) do |file| file.puts "Unexpected Output:\n\n" file.puts text ensure file.close end return log_file end |